#!/bin/sh

# ----------------------------------------------------------------------------
# 1) get files (wget) and save them under tmp/ subdir
# 2) for each downloaded files,compare the number of line of the file with the 
#    current version
# 3) if too much diminution of lines then exit 1
#    if the number of line are the same then there are no change and exit 0
# 4) print the message to insert new data
#
# EXIT 0: nothing new
#      1: error
#      2: updated
# ----------------------------------------------------------------------------

test -d tmp || mkdir tmp
chmod 700 tmp
#(cd tmp; wwwget http://sb9.astro.ulb.ac.be/SB9public.tar.gz | tar xvfz -)
(cd tmp; wget -O - -q http://sb9.astro.ulb.ac.be/SB9public.tar.gz | tar xvfz -)
if [ $? -ne 0 ]; then
    exit 1
fi

## Compare the file size
home=`pwd`
cd tmp
dim=0
mod=0
for f in *.dta; do
    olines=`fcat ../$f | wc -l`
    nlines=`fcat $f | wc -l`
    if test $nlines -lt $olines; then
        echo "****Diminution of $f ($nlines/$olines)"
	dim=`expr $dim + 1`
    fi
    if test $nlines -gt $olines; then
        echo "#---Change in $f ($nlines/$olines)"
	mod=`expr $mod + 1`
    fi
done

if test $dim -gt 10; then
    echo "#***Diminution of file contents -- give up!"
    exit 1
fi

if test $mod -eq 0; then
    echo "====No Change in `pwd`"
    exit 0
fi

### Create the NEW version
#rm -f ${catalog}* ; mv tmp/$catalog . ;  chmod 640 $catalog
cd $home
pwd
v=`ls -d v??|tail -1|tr -d v`
v=`expr $v + 1`

echo "====CREATION NOT COMPLETE: recreate SB files:"
echo "   cdcat B/sb9"
echo "   chmod 751 ."
echo "   #Sauvegardes to v$v first ..."
echo "   mkdir v$v; mv *.dat* notes.txt* v$v ; cp -p ReadMe v$v"
echo "   #... then copy tmp files to here ... "
echo "   mv tmp/*.dta . "
echo "   ./convert"
exit 2
###################################################################
