#!/bin/bash
#Generates the individual records list with links to appropiate ATM 

suffix="_recind.shtml"
sub_dir="./pages/"

#Link is set to master server!! Because ATM is processed by master
#Example of ATM link (header are hard coded for now)
#http://www.physionet.org/cgi-bin/ATM?database=mitdb&signal=V1&record=203&tool=plot_waveforms


echo "Generating HTML list of records for each signal class  "

for i in `cat signal_class_acronyms.config`
do
#Print HTML template header first
cat template_header.txt > $sub_dir$i$suffix
echo "Running Signal Class $i"

#Get all rows for given class
cat MIMIC2dB_sigs | grep [[:blank:]]$i > $sub_dir$i.txt

#Set the links to ATM (hard coded for now)
cat $sub_dir$i.txt | cut -f 1 > tmp_links.txt
cat tmp_links.txt | cut -f 1 -d / | sed 's/^/<ahref="http:\/\/www.physionet.org\/cgi-bin\/ATM?database=/' > tmp_database.txt
cat tmp_links.txt | cut -f 3 -d / | sed 's/^/\&record=/' | sed 's/$/\&tool=plot_waveforms">/' > tmp_record.txt
paste tmp_database.txt tmp_record.txt | sed 's/[[:space:]]//' > tmp_links2.txt
paste tmp_links2.txt tmp_links.txt | sed 's/[[:space:]]//' | sed 's/$/<\/a>/' > tmp_links3.txt

#Concatenate links and info into html file
cat $sub_dir$i.txt | cut --complement -f 1 | paste tmp_links3.txt - | sed 's/[[:space:]]/<\/td><td>/g' | sed 's/$/<\/td><\/tr>/' | sed 's/^/<tr><td>/' | sed 's/ahref/a href/'>>  $sub_dir$i$suffix

rm tmp*
#Append HTML template footer
cat template_footer.txt >> $sub_dir$i$suffix
done




#count=`cat physiobank-index | cut -f 2 | grep $i | wc -l`
#cat physiobank-index | cut -f 2 | grep $i | wc -l >> stats.txt

#echo -ne "<tr><td>">>$i$suffix
#echo "</td></tr>">> $i$suffix

