#!/bin/bash
# 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
#
dohtml=0
dooldtest=0
doidldoc_html=1
doidldoc_assistant=0
doidlwavedoc=0
doTARsrc=0
doTARdata=0
doforge=1
doSRClodyc=0
doDTAlodyc=0
doSRCidris=0
doDTAidris=0
#
#
# 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 [ ${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)
 #   tidy -m $(basename ${docxml}).html
    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
    cat <<EOF > idldoc_html_${$}.pro
!path = expand_path('+' + '../idldoc/')
print,!path
idldoc,root="../../",LOG_FILE="/tmp/idldochtlm_${$}.log", $
output="../idldoc_html_output"
exit
EOF
    # launch idl with the file built above
    IDL_STARTUP=idldoc_html_${$}.pro;idl
    /bin/rm idldoc_html_${$}.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=rawx to links on html files
    list_html=$(find ../idldoc_html_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=".*\)\(\.html\)\("\)/\1\2?format=raw\3/' \
     -e 's/\(href=".*\)\(\.html\)\(#.*"\)/\1\2?format=raw\3/' \
     -e 's/\(src=".*\)\(\.html\)\("\)/\1\2?format=raw\3/' \
     ${file_html} > ${file_html}_modify
     mv ${file_html}_modify ${file_html}
    done
    echo "iii : you can see log file in /tmp/idldochtlm_${$}.log"
    echo "iii : and look ../idldoc_html_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 saxo@forge.ipsl.jussieu.fr passwd two times"
    if [ ${doTARsrc} -ne 1 ]
    then
       localtarbase=${1}
    fi
    scp ${localtarbase}/SAXO_*.tar.gz saxo@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 [[ ($doSRClodyc -eq 1) || ($doDTAidris -eq 1) ]]
then
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}')
filesrc=$( basename $filesrc )
#
    cat <<EOF > job_src_$$
#!/bin/bash
cd SAXO_DIR
#
rm -rf SRC
wget http://forge.ipsl.jussieu.fr/saxo/download/$filesrc
gunzip $filesrc
tar xf $( basename $filesrc .gz )
rm -f $( basename $filesrc .gz )
#
chmod -R 755 *
exit
#
EOF
fi
#
if [[ ($doDTAlodyc -eq 1) || ($doDTAidris -eq 1) ]]
then
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}')
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 xf $( 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  cerbere"
    scp job_src_$$ smasson@cerbere.lodyc.jussieu.fr:.
    ssh smasson@cerbere.lodyc.jussieu.fr "chmod 755 job_src_$$ ; ./job_src_$$ ; rm -f job_src_$$"
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 [ $doSRCidris -eq 1 ]
then
# update src on rhodes
    echo "iii : update src to rhodes"
    scp job_src_$$ reee217@rhodes.idris.fr:.
    ssh reee217@rhodes.idris.fr "chmod 755 job_src_$$ ; ./job_src_$$ ; rm -f job_src_$$"
fi
#------------------------------------------------------------------
if [ $doDTAidris -eq 1 ]
then
# update data on gaya
    echo "iii : update data to gaya"
    scp job_data_$$ reee217@gaya.idris.fr:.
    ssh reee217@gaya.idris.fr "chmod 755 job_data_$$ ; ./job_data_$$ ; rm -f job_data_$$"
fi
#------------------------------------------------------------------
#
[ -f job_src_$$ ] && rm -f job_src_$$
[ -f job_data_$$ ] && rm -f
#
exit
#
