#!/bin/bash
# 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=0
doidldoc_assistant=0
doidlwavedoc=0
dotar=1
dolodyc=0
doforge=1
#
# 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 tiplink.xml updatesaxo.xml whatissaxo.xml whatsnew.xml
    do
    ./makehtml.sh $(basename ${docxml})
 #   tidy -m $(basename ${docxml}).html
    done
    exit 0
fi
#
# get the date in a nice format
#
ndate=`date +%Y%m%d`
#
#
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
#
    echo "eee : pas encore ecrit"
    exit 1
fi
if  [ ${doidldoc_assistant} -eq 1 ]
then
#
    echo "eee : pas encore ecrit"
    exit 1
fi
#
if [ ${dotar} -eq 1 ]
then
    localtarbase=/tmp/savesaxo_${$}
    mkdir  ${localtarbase}/
# prepare the source tar (without the data...)
#
    cd ${memopwd}/../..
    tar czf ${localtarbase}/SAXO_RD_${ndate}.tar.gz --exclude .svn *
#
# prepare the test_data tar
#
    cd ${memopwd}/../../DATA/
    tar czf ${localtarbase}/TestsData_${ndate}.tar.gz  --exclude .svn *
#
#   informations
    echo "iii : following files 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 [ ${dotar} -ne 1 ]
    then
       localtarbase=${1} 
    fi
    scp ${localtarbase}/*.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 [ $dolodyc -eq 1 ]
then
#
# put the src files on cerbere
#
    cd ~/SAXO_RD
    scp SAXO_RD_${ndate}.tar.gz smasson@cerbere.lodyc.jussieu.fr:.
    cd ~/IDL
    datafile=$( echo TestsData_*.tar.gz )
    cat > job_$$ << EOF
#!/bin/bash
[ -f WWW/SAXO/DATA/$datafile ] && echo 0 || echo 1
EOF
    scp job_$$ smasson@cerbere.lodyc.jussieu.fr:.
    err=$( ssh smasson@cerbere.lodyc.jussieu.fr \
    "chmod 755 job_$$ ; ./job_$$ ; rm -f job4_$$" )
    rm -f job_$$
    [ $err -ne 0 ] && scp $datafile smasson@cerbere.lodyc.jussieu.fr:.
#
# create cerbere script...
#
    cd ~/SAXO_RD
    cat <<EOF > job_$$
#!/bin/bash
rm -rf WWW/SAXO/SRC SAXO_RD
mkdir -p WWW/SAXO/SRC SAXO_RD
[ ! -d  WWW/SAXO/DATA ] && mkdir -p WWW/SAXO/DATA
# store SAXO_RD_${ndate}.tar.gz
mv -f SAXO_RD_${ndate}.tar.gz WWW/SAXO/SRC/.
# store $datafile
if [ -f $datafile ]
then
    rm -f WWW/SAXO/DATA/TestsData_*.tar.gz
    mv -f $datafile WWW/SAXO/DATA/.
fi
# redo SAXO_RD
cd SAXO_RD
ln -s ~/WWW/SAXO/SRC/SAXO_RD_${ndate}.tar.gz .
tar xvfz SAXO_RD_${ndate}.tar.gz
cd Tests
ln -s ~/WWW/SAXO/DATA/TestsData_*.tar.gz .
tar xvfz TestsData_*.tar.gz
mkdir TestsData
mv -f *.nc TestsData/.
# redo WWW/SAXO
cd /usr/home/smasson/WWW/SAXO/
tar xvfz docall.tar.gz
rm -f docall.tar.gz
#
cd
chmod -R 755 SAXO_RD WWW
# for rhodes...
scp ~/WWW/SAXO/SRC/SAXO_RD_${ndate}.tar.gz reee217@rhodes.idris.fr:.
ssh reee217@rhodes.idris.fr "rm -rf SAXO_RD ; mkdir SAXO_RD"
ssh reee217@rhodes.idris.fr "mv SAXO_RD_${ndate}.tar.gz SAXO_RD/. ; cd SAXO_RD ; gunzip SAXO_RD_${ndate}.tar.gz"
ssh reee217@rhodes.idris.fr "cd SAXO_RD ; tar xvf SAXO_RD_${ndate}.tar ; gzip SAXO_RD_${ndate}.tar"
ssh reee217@rhodes.idris.fr "chmod -R 755 SAXO_RD"
#
EOF
#
# update cerbere...
#
    scp job_$$ smasson@cerbere.lodyc.jussieu.fr:.
    ssh smasson@cerbere.lodyc.jussieu.fr "chmod 755 job_$$ ; ./job_$$ ; rm -f job_$$"
    rm -f job_$$
#
#
fi
