#!/bin/csh

set data_source = /home/lund/KH/post_process

set title = 'KELVIN-HELMHOLTZ LES RESULTS'
set compare_title = 'COMPARE KH RESULTS'

# read the cases to display and plots to generate

# the following lines do not work since blank spaces in the titles fool
# the set command.  The much more elaborate code below gets around the
# problem by setting the string arrays element by element.

#set cases      = `awk -F ' : ' '{print $1}' cases.list`
#set case_title = `awk -F ' : ' '{print $2}' cases.list`
#set plots      = `awk -F : '{print $1}' plots.list`
#set plot_title = `awk -F : '{print $2}' plots.list`

set null = 'x'

set n_max = `cat cases.list | wc -l`

echo $null > blank

set n = 2
while ($n <= $n_max)
   echo $null >> blank
   @ n ++
end
   
set cases      = `cat blank`
set case_title = `cat blank`

rm blank

set n = 1
while ($n <= $n_max)
  set cases[$n]      = `tail +$n cases.list | head -1 | awk -F : '{print $1}' -`
  set case_title[$n] = `tail +$n cases.list | head -1 | awk -F : '{print $2}' -`
  @ n ++
end

set n_max = `cat plots.list | wc -l`

echo $null > blank

set n = 2
while ($n <= $n_max)
   echo $null >> blank
   @ n ++
end
   
set plots      = `cat blank`
set plot_title = `cat blank`

rm blank

set n = 1
while ($n <= $n_max)
  set plots[$n]      = `tail +$n plots.list | head -1 | awk -F : '{print $1}' -`
  set plot_title[$n] = `tail +$n plots.list | head -1 | awk -F : '{print $2}' -`
  @ n ++
end

# create a link to the plot files

if(! -e data) ln -s $data_source data

# build main index page

cat ~/public_html/templates/index.html | sed "s/title goes here/$title/" > \
index.html

# build main menu

cat ~/public_html/templates/case_menu.head > menu.html

set n=0
foreach case ($cases)
   @ n ++
   echo \<p\> >> menu.html
   echo \<a href=$case.html  target=2\>$case_title[$n]\</a\>\<br\> >> menu.html
   echo \<p\> >> menu.html
   echo ' ' >> menu.html
end

cat ~/public_html/templates/compare.fragment >> menu.html
cat ~/public_html/templates/generic.tail >> menu.html


# build main page

cat ~/public_html/templates/main.html | \
   sed "s\title goes here\$title\" > main.html

# build case pages

set n=0
foreach case ($cases)
   @ n ++
   cat ~/public_html/templates/case_page.head | \
       sed "s/subtitle goes here/$case_title[$n]/" | \
       sed "s/title goes here/$title/"  > $case.html
   set m=0
   foreach plot ($plots)
     @ m ++
     echo \<br\> >> $case.html
     echo \<img src=\"data/$case/$plot.jpeg\" >> $case.html
     echo  alt=\"$plot_title[$m]\" width=\"720\" height=\"504\"\> >> $case.html
     echo \<br\> >> $case.html
     echo ' '  >> $case.html
   end
   cat ~/public_html/templates/generic.tail >> $case.html
end


# move into the compare subdirectory

if(! -e compare ) mkdir compare
cd compare

cp ~/public_html/templates/blank.html .

# build compare main page

cat ~/public_html/templates/compare.html | \
   sed "s/title goes here/$compare_title/" > compare.html


# build compare menus

cat ~/public_html/templates/compare_menu.head > menu1.html
cat ~/public_html/templates/compare_menu.head > menu2.html

echo \<h2 align=\"center\"\>compare\</h2\> >> menu1.html
echo \<h2 align=\"center\"\>with\</h2\> >> menu2.html
echo ' ' >> menu1.html
echo ' ' >> menu2.html

set n=0
foreach case ($cases)
   @ n ++
   echo \<b\>$case_title[$n]\</b\>\<br\> >> menu1.html
   echo \<b\>$case_title[$n]\</b\>\<br\> >> menu2.html
   set m=0
   foreach plot ($plots)
      @ m ++
      echo \&nbsp\;  \<a href=\"$case.$plot.html\" target=\"view1_frame\"\> \
           $plot_title[$m]\</a\>\<br\> >> menu1.html
      echo \&nbsp\;  \<a href=\"$case.$plot.html\" target=\"view2_frame\"\> \
           $plot_title[$m]\</a\>\<br\> >> menu2.html
   end
   echo \<br\> >> menu1.html
   echo \<br\> >> menu2.html
   echo ' ' >> menu1.html
   echo ' ' >> menu2.html
end

cat ~/public_html/templates/generic.tail >> menu1.html
cat ~/public_html/templates/generic.tail >> menu2.html

# build individual plot frames for comparison

foreach case ($cases)
   foreach plot ($plots)
      if(-e ../data/$case/$plot.jpeg) then
         cat ~/public_html/templates/plot_page.html | \
          sed "s/title goes here/$case_title[$n] $plot_title[$m] results/" | \
          sed "s:plot file goes here:../data/$case/$plot.jpeg:" >> \
          $case.$plot.html
      else
        cp ~/public_html/templates/blank.html $case.$plot.html
      endif
   end
end
