#!/bin/bash -x
# 
# update :
# $Id$
# fplod 2006-07-11T08:26:05Z aedon.locean-ipsl.upmc.fr (Darwin)
# commented tidy in dohtml section is moved in makehtml
# fplod 2006-07-10T08:22:53Z aedon.locean-ipsl.upmc.fr (Darwin)
# ajout de  white-space: pre; dans la css pour file_comments
# fplod 2006-07-04T13:09:19Z aedon.locean-ipsl.upmc.fr (Darwin)
# for idldoc html output 
# because preformat option doesn't seem to work well
# replace 
# div class="value"
# by
# div class="preformat"
# fplod 2006-06-19T08:46:39Z aedon.locean-ipsl.upmc.fr (Darwin)
# for idldoc html output 
#. replace
#     -e 's/\(href=".*\)\(\.html\)\("\)/\1\2?format=raw\3/g' \
#     -e 's/\(href=".*\)\(\.html\)\(#.*"\)/\1\2?format=raw\3/g' \
#     -e 's/\(src=".*\)\(\.html\)\("\)/\1\2?format=raw\3/g' \
# by 
# -e 's@\(href="[^.]*[^h]*[^t]*[^m]*[^l]*[^"]*\)\(\.html\)\("\)@\1\2?format=raw\3@g' \
# -e 's@\(href="[^.]*[^h]*[^t]*[^m]*[^l]*[^"]*\)\(\.html\)\(#.*"\)@\1\2?format=raw\3@g' \
#-e 's@\(src="[^.]*[^h]*[^t]*[^m]*[^l]*[^"]*\)\(\.html\)\("\)@\1\2?format=raw\3@g'
# in order to be able to add ?format=raw several time on one line
# . suppress .html in line like 
# a parameter from the routine ./ReadWrite/idl-NetCDF/ncdf_read.html
# in files idldoc-index*.html
# fplod 2006-06-14T12:41:37Z aedon.locean-ipsl.upmc.fr (Darwin)
# add embed paramater idldoc instructions to try to fix ticket#17
# fplod 2006-06-07T12:11:50Z aedon.locean-ipsl.upmc.fr (Darwin)
# add g option in ?format=raw sed transformation in order to fix 
# link on top of idldoc like directory-overview.html
# fplod 2006-06-07T10:57:35Z aedon.locean-ipsl.upmc.fr (Darwin)
# add ?format=raw in idldoc production for frames
# fplod 2006-06-07T08:14:15Z aedon.locean-ipsl.upmc.fr (Darwin)
# add action for idldochtml
# fplod 2006-06-02T14:28:30Z aedon.locean-ipsl.upmc.fr (Darwin)
# modification to produce tar files in /tmp/saxo_${$} when dotar=1
# modification to copy tar files on http://forge.ipsl.jussieu.fr/saxo/download/
# when doforge=1
# produce tar files in /tmp/saxo_${$} when dotar=1
set -x
set -u
#
docompileall=1
dohtml=0
dooldtest=0
doidlwavedoc=0
doidldoc_html=0
doidldoc_assistant=0
doTARsrc=0
doTARdata=0
doforge=0
doSRClodyc=0
doDTAlodyc=0
doSRCipsl=0
doDTAipsl=0
doSRCidris=0
doDTAidris=0
#
case $( whoami ) in
    smasson)
	lgforge=smasson
    ;;
    floseb)
	lgforge=smasson
    ;;
    *)
	lgforge=saxo
    ;;
esac
#
if [ ${lgforge} != smasson ]
then
    doSRClodyc=0
    doDTAlodyc=0
    doSRCipsl=0
    doDTAipsl=0
    doSRCidris=0
    doDTAidris=0
fi    
#
# get the date in a nice format
#
ndate=`date +%Y%m%d`
# memorize current directory
memopwd=$(pwd) # should be <working space>/SRC/Documentation/xmldoc
#
#------------------------------------------------------------------
if [ ${docompileall} -eq 1 ]
    then
    cat <<EOF > /tmp/compileall${$}.pro
.reset_session  
;
!path = expand_path('+' + '../..') + ':' + expand_path('+' + !dir)
resolve_all, resolve_either='def_myuniquetmpdir', skip_routines = ['xxx2ps','trends', 'label_gmt']
resolve_all, resolve_either = 'find', skip_routines = ['xxx2ps','trends', 'label_gmt']
def_myuniquetmpdir
oldcm = find('oldcm_empty')
;oldcm = find('oldcm_full')
oldcm = oldcm[0]
@cm_general
file_copy, oldcm, myuniquetmpdir + 'oldcm_used.pro', /overwrite
IF !journal NE 0 THEN journal
journal, myuniquetmpdir + 'cm_demomode_used.pro'
journal   
;
resolve_all, resolve_either='keep_compatibility', skip_routines = ['xxx2ps','trends', 'label_gmt']
;
EOF
#
    for i in $( find ${memopwd}/../.. -name "*.pro" | grep -iv label_gmt )
      do
# does it contains pro or function ?
      egrep -iq '^ *	*(pro|function)' $i
      if [ $? -eq 0 ]
	  then
	  echo "print, '$( basename $i .pro ) :'" >> /tmp/compileall${$}.pro
 	  echo "resolve_all, resolve_either = '$( basename $i .pro )', skip_routines = ['xxx2ps','trends', 'label_gmt']" >> /tmp/compileall${$}.pro
      fi
    done
    echo exit >> /tmp/compileall${$}.pro
    IDL_STARTUP=/tmp/compileall${$}.pro
    ${IDL_DIR}/bin/idl &> /tmp/compileallerr_${$}
    echo
    echo
    grep -i "error" /tmp/compileallerr_${$}
#    grep -q "Compilation error(s)" /tmp/compileallerr_${$}
    if [ ${?} -ne 0 ] 
    then
     echo "ALL SAXO files are OK..."
     rm -f /tmp/compileallerr_${$} /tmp/compileall${$}.pro
    else
     grep "Compilation error(s)" /tmp/compileallerr_${$}
     echo "eee : see /tmp/compileallerr_${$}"
     more /tmp/compileallerr_${$}
     exit 1
    fi
    echo
    echo
fi
#------------------------------------------------------------------
if [ ${dohtml} -eq 1 ]
then
#
# redo the html file
#
    cd ${memopwd}
    for docxml in firststeps.xml getsaxo.xml mailing.xml main.xml tiplink.xml updatesaxo.xml whatissaxo.xml whatsnew.xml
    do
      ./makehtml.sh $(basename ${docxml} .xml)
    done
    exit 0
fi
#------------------------------------------------------------------
if [ ${dooldtest} -eq 1 ]
then
# make the old test
#
    cd ${memopwd}/../../Tests/
    ./makeold.sh
    exit 0
fi
#------------------------------------------------------------------
if  [ ${doidlwavedoc} -eq 1 ]
then
#
# redo idlwave doc
#
    cd ${memopwd}/../..
    Documentation/idlwave_catalog -v -f saxo
    exit 0
fi
#------------------------------------------------------------------
if  [ ${doidldoc_html} -eq 1 ]
then
#
    # build a idl file to launch idldoc
    output=../idldoc_html_output/
    log="/tmp/idldochtlm_${$}.log"
    pro="/tmp/idldochtlm_${$}.pro"
    cat <<EOF > ${pro}
!path = expand_path('+' + '../idldoc/')
print,!path
idldoc,root="../../",title="SAXO Documentation", $
LOG_FILE="${log}", $
output="${output}", /preformat, /embed
exit
EOF
    # launch idl with the file built above
    IDL_STARTUP=${pro};${IDL_DIR}/bin/idl
    /bin/rm ${pro}
    # In order to do not have to many revision in svn, we modify some
    # lines produced by idldoc :
    # examples :
    # <!-- Generated by IDLdoc 2.0 on Wed Jun  7 10:25:28 2006 -->
    # will be replaced by
    # <!-- Generated by IDLdoc 2.0 -->
    # <div id="tagline">Produced by IDLdoc 2.0 on Wed Jun  7 10:54:37 2006.</div
    # will be replaced by
    # <div id="tagline">Produced by IDLdoc 2.0</div>
    # <h2>Produced on Wed Jun  7 10:23:13 2006</h2>
    # will be delete 
    # other lines will be deleted because the time is the time of checkout not 
    # the time of revision.
    # the sequence of lines
    #<dl>
    #      <dt>Last modification:</dt>
    #       <dd>Fri Jun  2 11:46:24 2006</dd>
    #    </dl>
    # should also be removed ++ je ne sais pas dire en sed la ligne
    # suivant l'occurence de Last modification
    # add ?format=raw to links on html files
    list_html=$(find ${output} -name "*.html")
    for file_html in ${list_html}
    do
     sed \
     -e "s/\(Generated by IDLdoc 2.0\)\( on .*\)\( -->\)/\1\3/" \
     -e "s/\(Produced by IDLdoc 2.0\)\( on .*\)\(\.<\/div>\)/\1\3/" \
     -e "/<h2>Produced on/d" \
     -e "/Last modification:/d" \
     -e "/<dd>... ... .* ..:..:.. ....<\/dd>/d" \
     -e 's@\(href="[^.]*[^h]*[^t]*[^m]*[^l]*[^"]*\)\(\.html\)\("\)@\1\2?format=raw\3@g' \
     -e 's@\(href="[^.]*[^h]*[^t]*[^m]*[^l]*[^"]*\)\(\.html\)\(#.*"\)@\1\2?format=raw\3@g' \
     -e 's@\(src="[^.]*[^h]*[^t]*[^m]*[^l]*[^"]*\)\(\.html\)\("\)@\1\2?format=raw\3@g' \
     -e 's@<div class="value">@<div class="preformat">@' \
     -e 's/div\#file_comments {/div\#file_comments { white-space: pre;/' \
     ${file_html} > ${file_html}_modify
     mv ${file_html}_modify ${file_html}
    done
    # suppress ".html" in idldoc-index*.html in lines like
    # a parameter from the routine ./ReadWrite/idl-NetCDF/ncdf_read.html
    list_html=$(find ${output} -name "idldoc-index*.html")
    for file_html in ${list_html}
    do
     sed \
     -e "s/\(a parameter from the routine \)\(.*\)\(.html\)/\1\2/" \
     ${file_html} > ${file_html}_modify
     mv ${file_html}_modify ${file_html}
    done
    echo "iii : you can see log file in ${log}"
    echo "iii : and look ${output}/idldoc-warnings.html#undoc"
    exit 1
fi
#------------------------------------------------------------------
if  [ ${doidldoc_assistant} -eq 1 ]
then
#
    echo "eee : pas encore ecrit"
    exit 1
fi
#------------------------------------------------------------------
if [ ${doTARsrc} -eq 1 ]
then
    localtarbase=/tmp/savesaxo_${$}
    mkdir  ${localtarbase}/
# clean backup files : *~
    find ${memopwd}/../../.. -name "*~" -exec rm {} \;
# prepare the source tar (without the svn...)
#
    cd ${memopwd}/../../..
    tar czf ${localtarbase}/SAXO_SRC_${ndate}.tar.gz --exclude .svn SRC
#
#   informations
    echo "iii : following file may be copied on http://forge.ipsl.jussieu.fr/saxo/download/"
    ls -l ${localtarbase}
fi
#------------------------------------------------------------------
if [ ${doTARdata} -eq 1 ]
then
    localtarbase=/tmp/savesaxo_${$}
    mkdir  ${localtarbase}/
#
# prepare the data tar (without the svn...)
#
    cd ${memopwd}/../../..
    tar czf ${localtarbase}/SAXO_DATA_${ndate}.tar.gz  --exclude .svn DATA
#
#   informations
    echo "iii : following file may be copied on http://forge.ipsl.jussieu.fr/saxo/download/"
    ls -l ${localtarbase}
fi
#------------------------------------------------------------------
if [ ${doforge} -eq 1 ]
then
    echo "iii : you will be asked ${lgforge}@forge.ipsl.jussieu.fr passwd two times"
    if [ ${doTARsrc} -ne 1 ]
    then
       localtarbase=${1}
    fi
    scp ${localtarbase}/SAXO_*.tar.gz ${lgforge}@forge.ipsl.jussieu.fr:/ipsl/forge/projets/saxo/download/
    echo "iii : do not forget to update getsaxo.html"
    echo "iii : \$ ./makehtml.sh getsaxo"
    echo "iii : do not forget to clean ${localtarbase}"
fi
#------------------------------------------------------------------
if [[ ($doDTAlodyc -eq 1) || ($doDTAipsl -eq 1) ]]
then
    echo "iii : you will be asked ${lgforge}@forge.ipsl.jussieu.fr passwd"
    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}')
    filedata=$(basename $filedata)
    cat <<EOF > job_data_$$
#!/bin/bash
cd SAXO_DIR
#
rm -rf DATA
wget http://forge.ipsl.jussieu.fr/saxo/download/$filedata
gunzip $filedata
tar vxf $( basename $filedata .gz )
rm -f $( basename $filedata .gz )
#
chmod -R 755 *
exit
#
EOF
fi
#------------------------------------------------------------------
if [ $doSRClodyc -eq 1 ]
then
# update src on cerbere
#
    echo "iii : update src on cerbere"
    ssh smasson@cerbere.lodyc.jussieu.fr "svn update SAXO_DIR/SRC ; chmod -R 755 SAXO_DIR"
fi
#------------------------------------------------------------------
if [ $doDTAlodyc -eq 1 ]
then
# update data on cerbere
    echo "iii : update data on cerbere"
    scp job_data_$$ smasson@cerbere.lodyc.jussieu.fr:.
    ssh smasson@cerbere.lodyc.jussieu.fr "chmod 755 job_data_$$ ; ./job_data_$$ ; rm -f job_data_$$"
fi
#------------------------------------------------------------------
if [ $doSRCipsl -eq 1 ]
then
# update src on calcul2
#
    echo "iii : update src on calcul2"
    ssh smlod@calcul2.ipsl.jussieu.fr "svn update SAXO_DIR/SRC ; chmod -R 755 SAXO_DIR"
fi
#------------------------------------------------------------------
if [ $doDTAipsl -eq 1 ]
then
# update data on cook
    echo "iii : update data on cook"
    scp job_data_$$ smlod@cook.ipsl.jussieu.fr:.
    ssh smlod@cook.ipsl.jussieu.fr "chmod 755 job_data_$$ ; ./job_data_$$ ; rm -f job_data_$$"
fi
#------------------------------------------------------------------
if [ $doSRCidris -eq 1 ]
then
# update src on rhodes
    echo "iii : update src to rhodes"
    ssh reee217@rhodes.idris.fr "/usr/local/pub/svn/svn-1.3.1/bin/svn update SAXO_DIR/SRC ; chmod -R 755 SAXO_DIR"
fi
#------------------------------------------------------------------
if [ $doDTAidris -eq 1 ]
then
# create the update script for rhodes
    if [[ ($doDTAlodyc -eq 0) && ($doDTAipsl -eq 0) ]]
	then
	echo "iii : you will be asked ${lgforge}@forge.ipsl.jussieu.fr passwd"
	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}')
	filedata=$(basename $filedata)
    fi
    cat <<EOF > job_data_idris_$$
#!/bin/ksh
cd SAXO_DIR
#
rm -rf DATA
echo "iii : you will be asked ${lgforge}@forge.ipsl.jussieu.fr passwd"
scp ${lgforge}@forge.ipsl.jussieu.fr:/ipsl/forge/projets/saxo/download/$filedata .
gunzip $filedata
tar vxf $( basename $filedata .gz )
rm -f $( basename $filedata .gz )
#
chmod -R 755 *
exit
#
EOF
# update data on gaya
    echo "iii : update data to gaya"
    scp job_data_idris_$$ reee217@gaya.idris.fr:.
    ssh reee217@gaya.idris.fr "chmod 755 job_data_idris_$$ ; ./job_data_idris_$$ ; rm -f job_data_idris_$$"
fi
#------------------------------------------------------------------
#
[ -f job_src_$$ ] && rm -f job_src_$$
[ -f job_data_$$ ] && rm -f job_data_$$
[ -f job_data_idris_$$ ] && rm -f job_data_idris_$$
#
exit
#
