#!/bin/bash
#
# fplod 2006-06-07T13:55:07Z aedon.locean-ipsl.upmc.fr (Darwin)
# add infoupdatekwd
# fplod 2006-06-02T14:37:47Z aedon.locean-ipsl.upmc.fr (Darwin)
# take only one tar file : the most recent using modification time 
# fplod 2006-06-02T10:00:41Z aedon.locean-ipsl.upmc.fr (Darwin)
# http://forge.ipsl.jussieu.fr/saxo/download/ is the new directory reference 
# for tar file instead of ~/SAXO_SRC/ ie home of Sebatien Masson on 
# arete.lodyc.jussieu.fr
# !!! saxo@forge.ipsl.jussieu.fr password is needed
# fplod 2006-05-31T10:34:18Z aedon.locean-ipsl.upmc.fr (Darwin)
# . add creation of all_plots.html using firststeps.html
# . remplace ~/SAXO_SRC/SRC/Documentation/xmldoc/figpng by ./figpng/
# we suppose that this shell script is launched when we are in 
# <working space of saxo>/SRC/Documentation/xmldoc/
#
set -u
#
case "${1}" in
   getsaxo)
      # size of SRCand DATA with .svn
      szallsvn=$( du -sh ../../.. | awk '{print $1}' )
      szallsvn=${szallsvn%*M}
      # size of SRC with .svn
      szsrcsvn=$( du -sh ../../../SRC | awk '{print $1}' )
      szsrcsvn=${szsrcsvn%*M}
      # size of DATA with .svn
      szdatasvn=$( du -sh ../../../DATA | awk '{print $1}' )
      szdatasvn=${szdatasvn%*M}
      echo "iii : you will be asked saxo@forge.ipsl.jussieu.fr passwd two times"
      # find the most recent SAXO_SRC_yyyymmdd.tar.gz in 
      # /ipsl/forge/projets/saxo/download/
      infosrc=$(ssh saxo@forge.ipsl.jussieu.fr ls -lht /ipsl/forge/projets/saxo/download/SAXO_SRC_*.tar.gz  | head -n 1)
      filesrc=$(echo ${infosrc} | awk '{print $NF}')
      szsrc=$(echo ${infosrc} | awk '{print $5}')
      # ndate is the date in the file SAXO_SRC_*.tar.gz
      ndate=${filesrc##*_}
      ndate=${ndate%.tar.gz}
      # find the most recent SAXO_DATA_yyyymmdd.tar.gz in 
      # /ipsl/forge/projets/saxo/download/
      infodata=$(ssh saxo@forge.ipsl.jussieu.fr ls -lht /ipsl/forge/projets/saxo/download/SAXO_DATA_*.tar.gz | head -n 1)
      filedata=$(echo ${infodata} | awk '{print $NF}')
      szdata=$(echo ${infodata} | awk '{print $5}')
      # ndate2 is the date in the file SAXO_DATA_*.tar.gz
      ndate2=${filedata##*_}
      ndate2=${ndate2%.tar.gz}
   ;;
   infoupdatekwd)
      grep "^  old = \[old, .* new = \[new" ../../ForOldVersion/updatekwd.pro | \
      sed -e "s/\( old = \[old, '\)\(.*\)\('\] *& new = \[new, '\)\(.*\)\('\]\)/<row><entry colname='old'>\2<\/entry> <entry colname='new'>\4<\/entry><\/row>/" > /tmp/rows.xml
   ;;
   *)
      echo "iii : no ndate determination needed"
   ;;
esac

# figures lists
cd ./figpng

#
figsplt_lev=""
for i in tst_plt_lev_[0-9][0-9].png
do
  figsplt_lev="${figsplt_lev} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltz_lev=""
for i in tst_pltz_lev_[0-9][0-9].png
do
  figspltz_lev="${figspltz_lev} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltt_lev=""
for i in tst_pltt_lev_[0-9][0-9].png
do
  figspltt_lev="${figspltt_lev} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
#
figsplt_orca2=""
for i in tst_plt_orca2_[0-9][0-9].png
do
  figsplt_orca2="${figsplt_orca2} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltz_orca2=""
for i in tst_pltz_orca2_[0-9][0-9].png
do
  figspltz_orca2="${figspltz_orca2} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltt_orca2=""
for i in tst_pltt_orca2_[0-9][0-9].png
do
  figspltt_orca2="${figspltt_orca2} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
#
figsplt_orca05=""
for i in tst_plt_orca05_[0-9][0-9].png
do
  figsplt_orca05="${figsplt_orca05} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltz_orca05=""
for i in tst_pltz_orca05_[0-9][0-9].png
do
  figspltz_orca05="${figspltz_orca05} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltt_orca05=""
for i in tst_pltt_orca05_[0-9][0-9].png
do
  figspltt_orca05="${figspltt_orca05} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
##
#
figsplt_lev_stride=""
for i in tst_plt_lev_stride_[0-9][0-9].png
do
  figsplt_lev_stride="${figsplt_lev_stride} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltz_lev_stride=""
for i in tst_pltz_lev_stride_[0-9][0-9].png
do
  figspltz_lev_stride="${figspltz_lev_stride} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltt_lev_stride=""
for i in tst_pltt_lev_stride_[0-9][0-9].png
do
  figspltt_lev_stride="${figspltt_lev_stride} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
#
figsplt_orca2_stride=""
for i in tst_plt_orca2_stride_[0-9][0-9].png
do
  figsplt_orca2_stride="${figsplt_orca2_stride} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltz_orca2_stride=""
for i in tst_pltz_orca2_stride_[0-9][0-9].png
do
  figspltz_orca2_stride="${figspltz_orca2_stride} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltt_orca2_stride=""
for i in tst_pltt_orca2_stride_[0-9][0-9].png
do
  figspltt_orca2_stride="${figspltt_orca2_stride} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
#
figsplt_orca05_stride=""
for i in tst_plt_orca05_stride_[0-9][0-9].png
do
  figsplt_orca05_stride="${figsplt_orca05_stride} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltz_orca05_stride=""
for i in tst_pltz_orca05_stride_[0-9][0-9].png
do
  figspltz_orca05_stride="${figspltz_orca05_stride} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
figspltt_orca05_stride=""
for i in tst_pltt_orca05_stride_[0-9][0-9].png
do
  figspltt_orca05_stride="${figspltt_orca05_stride} \<ulink url=\"figpng\/${i}?format=raw\"\>\&showfig\;\<\/ulink\>"
done
##
cd .. # ie <working space of saxo>/SRC/Documentation/xmldoc

case "${1}" in
   getsaxo)
      sed -e s/"\&date\;"/${ndate}/g \
          -e s/"\&date2\;"/${ndate2}/g \
          -e s/"\&szsrc\;"/${szsrc}/g \
          -e s/"\&szdata\;"/${szdata}/g \
          -e s/"\&szallsvn\;"/${szallsvn}/g \
          -e s/"\&szsrcsvn\;"/${szsrcsvn}/g \
          -e s/"\&szdatasvn\;"/${szdatasvn}/g \
          ${1}.xml > ${1}_${$}.xml 
   ;;
   firststeps)
      sed -e s/"\&figsplt_lev\;"/"${figsplt_lev}"/g \
          -e s/"\&figsplt_orca2\;"/"${figsplt_orca2}"/g \
          -e s/"\&figsplt_orca05\;"/"${figsplt_orca05}"/g \
          -e s/"\&figspltz_lev\;"/"${figspltz_lev}"/g \
          -e s/"\&figspltz_orca2\;"/"${figspltz_orca2}"/g \
          -e s/"\&figspltz_orca05\;"/"${figspltz_orca05}"/g \
          -e s/"\&figspltt_lev\;"/"${figspltt_lev}"/g \
          -e s/"\&figspltt_orca2\;"/"${figspltt_orca2}"/g \
          -e s/"\&figspltt_orca05\;"/"${figspltt_orca05}"/g \
          -e s/"\&figsplt_lev_stride\;"/"${figsplt_lev_stride}"/g \
          -e s/"\&figsplt_orca2_stride\;"/"${figsplt_orca2_stride}"/g \
          -e s/"\&figsplt_orca05_stride\;"/"${figsplt_orca05_stride}"/g \
          -e s/"\&figspltz_lev_stride\;"/"${figspltz_lev_stride}"/g \
          -e s/"\&figspltz_orca2_stride\;"/"${figspltz_orca2_stride}"/g \
          -e s/"\&figspltz_orca05_stride\;"/"${figspltz_orca05_stride}"/g \
          -e s/"\&figspltt_lev_stride\;"/"${figspltt_lev_stride}"/g \
          -e s/"\&figspltt_orca2_stride\;"/"${figspltt_orca2_stride}"/g \
          -e s/"\&figspltt_orca05_stride\;"/"${figspltt_orca05_stride}"/g \
          ${1}.xml > ${1}_${$}.xml 
   ;;
   infoupdatekwd)
      sed -e "/-- rows from updatekwd.pro --/r /tmp/rows.xml" \
          ${1}.xml > ${1}_${$}.xml 
      /bin/rm /tmp/rows.xml
   ;;
   *)
      cp ${1}.xml ${1}_${$}.xml 
      echo "iii : no xml transformation needed"
   ;;
esac
#
CLASSPATH="$CLASSPATH:\
/sw/share/java/saxon/saxon.jar:\
/sw/share/xml/xsl/docbook-xsl/extensions/saxon653.jar"
export CLASSPATH
#java  com.icl.saxon.StyleSheet  \
#    -o ${1}.html ${1}.xml  \
#    /sw/share/xml/xsl/docbook-xsl/html/docbook.xsl  \
#    use.extensions=1 section.autolabel=1 html.stylesheet="saxo.css" css.decoration=1
#
#    callout.graphics.path="/sw/share/xml/xsl/docbook-xsl/images/callouts/"

java  com.icl.saxon.StyleSheet -o ${1}.html ${1}_${$}.xml  \
    /sw/share/xml/xsl/docbook-xsl/html/docbook.xsl  \
   section.autolabel=1 html.stylesheet="saxo.css" css.decoration=1

case "${1}" in
   firststeps)
      java  com.icl.saxon.StyleSheet -o all_plots_${$}.xml ${1}_${$}.xml  \
      all_plots_docbook.xsl
      java com.icl.saxon.StyleSheet -o all_plots.html all_plots_${$}.xml  \
      /sw/share/xml/xsl/docbook-xsl/html/docbook.xsl  \
      section.autolabel=1 html.stylesheet="saxo.css" css.decoration=1
      rm -f all_plots_${$}.xml
# not very nice... but useful !!
      sed -e s/".png\""/".png?format=raw\""/g ${1}.html > ${1}_${$}.html
      mv ${1}_${$}.html ${1}.html
   ;;
   main)
      sed -e s?"../../saxo/browser/trunk/SRC/Documentation/xmldoc/"??g \
	  -e s?"../../saxo/browser/trunk"?"http://forge.ipsl.jussieu.fr/saxo/browser/trunk"?g \
          ${1}.html > ${1}_${$}.html 
      mv ${1}_${$}.html ${1}.html
   ;;
esac
rm -f ${1}_${$}.xml
