#!/bin/bash -x
#
# update :
# $Id$
#
# ++ one day we can add accessibility check with tidy with -access 3
# diagnostics are not so good so far because tidy (HTML Tidy for Mac OS X
# released on 1 September 2005) doesn't handle carriage returns
#
# fplod 2008-04-16T09:02:05Z aedon.locean-ipsl.upmc.fr (Darwin)
# this file is not used anymore. see makefile.
# fplod 2007-06-27T11:54:26Z aedon.locean-ipsl.upmc.fr (Darwin)
# replacing saxon by xalan because of better results on callouts
# fplod 2006-09-20T16:28:32Z aedon.locean-ipsl.upmc.fr (Darwin)
# remove ?format=raw
# fplod 2006-09-18T12:53:07Z aedon.locean-ipsl.upmc.fr (Darwin)
# add status checking in xml processing
# fplod 2006-09-07T08:10:23Z aedon.locean-ipsl.upmc.fr (Darwin)
# main is rename to websaxo.html
# fplod 2006-08-29T14:01:11Z aedon.locean-ipsl.upmc.fr (Darwin)
# add main which needs xmllint processing to resolve xinclude
# fplod 2006-08-18T13:38:31Z aedon.locean-ipsl.upmc.fr (Darwin)
# add faqsaxo which needs xmllint processing to resolve xinclude
# fplod 2006-07-21T08:25:05Z aedon.locean-ipsl.upmc.fr (Darwin)
# add prerequisites for mini_notice production
# fplod 2006-07-12T11:57:24Z aedon.locean-ipsl.upmc.fr (Darwin)
# add style.css
# fplod 2006-07-11T08:25:30Z aedon.locean-ipsl.upmc.fr (Darwin)
# use of tidy and titlepage.saxo.xml
# replace /sw/share/xml/xsl/docbook-xsl/html/docbook.xsl
# by saxo_html.xsl
# fplod 2006-07-05T13:18:56Z aedon.locean-ipsl.upmc.fr (Darwin)
# add infoupdatecm
# add sort and varname in production of infoupdatekwd
# 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.locean-ipsl.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 $( whoami ) in
    smasson)
	lgforge=smasson
    ;;
    floseb)
	lgforge=smasson
    ;;
    *)
	lgforge=saxo
    ;;
esac
#
case "${1}" in
   getsaxo)
      # size of SRC and 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 ${lgforge}@forge.ipsl.jussieu.fr passwd two times"
      # find the most recent SAXO_SRC_yyyymmddrxxx.tar.gz in
      # /ipsl/forge/projets/saxo/download/
      infosrc=$(ssh ${lgforge}@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_yyyymmddrxxx.tar.gz in
      # /ipsl/forge/projets/saxo/download/
      infodata=$(ssh ${lgforge}@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 | \
	sort | \
      sed -e "s/\( old = \[old, '\)\(.*\)\('\] *& new = \[new, '\)\(.*\)\('\]\)/<row><entry colname='old'><varname>\2<\/varname><\/entry> <entry colname='new'><varname>\4<\/varname><\/entry><\/row>/" > /tmp/rowskwd.xml
   ;;
   infoupdatecm)
      grep "IF NOT key_forgetold AND n_elements(" ../../ForOldVersion/updateold.pro | \
	sort | \
      sed -e "s/\(^.* THEN \)\(.*\)\( = \)\(.*\)/<row><entry colname='old'><varname>\2<\/varname><\/entry> <entry colname='new'><varname>\4<\/varname><\/entry><\/row>/" > /tmp/rowscm.xml
   ;;
   mini_notice)
      # production of images with frames and callouts
      # ./forxxxdoc.sh 0101
      # ./forxxxdoc.sh 0201
   ;;
   *)
      echo "iii : no prerequisites 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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&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}\"\>\&showfig\;\<\/ulink\>"
done
##
cd .. # ie <working space of saxo>/SRC/Documentation/xmldoc
#
# production of titlepage.saxo.xsl
#
# si pb reseau remplacer sur mac par la ligne suivante
titlepagexsl=http://docbook.sourceforge.net/release/xsl/current/template/titlepage.xsl
titlepagexsl=/sw/share/xml/xsl/docbook-xsl/template/titlepage.xsl
#
xsltproc \
--xinclude \
-o titlepage.saxo.xsl \
${titlepagexsl} \
titlepage.saxo.xml
status=${?}
if [ ${status} -ne 0 ]
then
 echo "eee : pb xsltproc sur titlepage.saxo.xml"
 exit 1
fi
#
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/rowskwd.xml" \
          ${1}.xml > ${1}_${$}.xml
      /bin/rm /tmp/rowskwd.xml
   ;;
   infoupdatecm)
      sed -e "/-- rows from updateold.pro --/r /tmp/rowscm.xml" \
          ${1}.xml > ${1}_${$}.xml
      /bin/rm /tmp/rowscm.xml
   ;;
   faqsaxo|websaxo)
      # resolve xinclude
      xmllint --xinclude \
              --output ${1}_${$}.xml ${1}.xml
      status=${?}
      if [ ${status} -ne 0 ]
      then
       echo "eee : pb xmllint sur ${1}.xml"
       exit 1
      fi
   ;;
   *)
      cp ${1}.xml ${1}_${$}.xml
      echo "iii : no xml transformation needed"
   ;;
esac
#
CLASSPATH=${CLASSPATH}:/usr/Xalan/xalan.jar:/usr/Xalan/xml-apis.jar:\
/usr/Xalan/xercesImpl.jar:/usr/docbook-xsl/extensions/xalan25.jar
export CLASSPATH
#
java org.apache.xalan.xslt.Process -out ${1}.html -in ${1}_${$}.xml  \
-xsl saxo_html.xsl \
-param section.autolabel 1 \
-param html.stylesheet "style.css saxo.css" \
-param css.decoration 1
status=${?}
if [ ${status} -ne 0 ]
then
 echo "eee : pb xalan sur ${1}_${$}.xml"
 exit 1
fi
#
case "${1}" in
   firststeps)
      # process all_plots
      java org.apache.xalan.xslt.Process -out all_plots_${$}.xml \
      -in  ${1}_${$}.xml  \
      -xsl all_plots_docbook.xsl
      java org.apache.xalan.xslt.Process -out all_plots.html \
      -in  all_plots_${$}.xml \
      -xsl saxo_html.xsl \
      -param section.autolabel 1 \
      -param html.stylesheet "style.css saxo.css" \
      -param css.decoration 1
      rm -f all_plots_${$}.xml
      ;;
   websaxo)
      sed -e s?"../../saxo/browser/trunk/SRC/Documentation/xmldoc/"??g \
	  -e s?"../../saxo/browser/trunk/SRC/Documentation/idldoc_html_output/"?"../idldoc_html_output/"?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
#
# use of tidy to have a W3C compliant html file
tidy -m ${1}.html
#
# clean
rm -f ${1}_${$}.xml
rm titlepage.saxo.xsl
#
# exit
exit 0
