#! /bin/csh
#++++++++++++++++
#.IDENTIFICATION getWDS
#.LANGUAGE       C-shell
#.AUTHOR         CDS Catalogues Service
#.ENVIRONMENT
#.KEYWORDS
#.VERSION  1.0   17-Mar-2005: from http://fuse.iap.fr/VIZIER/
#.VERSION  1.1   17-Aug-2015: use wget instead of wwwget (G.Landais)
#.PURPOSE        Get Latest FUSE
#.COMMENTS
#----------------
#
# 1) get files (wget) and save them under tmp/ subdir
# 2) compare files downloaded with current file (in current dir)
# 3) if no change then exit
# 4) print message to insert new data to run make_vsx
# NOTE: the new files are in tmp/. The script "make_vsx" use that subdir!
#
# EXIT 0: nothing new
#      1: error
#      2: updated
# ----------------------------------------------------------------------------


umask 027	# Files created can't be seen
set URL = "http://www.aavso.org/vsx/external"
set modified = 0
set wget_opt = "--quiet"

#############################################
# Get new files in tmp subdir
############################################
rm -rf tmp ; mkdir tmp; chmod 700 tmp

cd tmp
#wwwget -head $URL/ReadMe >& readme.head # Not existing any more...
#wwwget -head $URL/vsx.dat.gz >& vsx.head
#wwwget -head $URL/vsxrefs.dat.gz >& refs.head
##wwwget $URL/ReadMe | trim > readme.ori	# 11-Nov-2012 (does not exist)
#wwwget $URL/vsx.dat.gz > vsx.ori.gz
echo "get headers...."
wget --server-response $URL/ReadMe >& readme.head # Not existing any more...
wget --server-response $URL/vsx.dat.gz >& vsx.head
wget --server-response $URL/vsxrefs.dat.gz >& refs.head
#wwwget $URL/ReadMe | trim > readme.ori	# 11-Nov-2012 (does not exist)
echo "get data..."
wget $URL/vsx.dat.gz -O vsx.ori.gz $wget_opt
#if ("`file vsx.ori`" =~ *"gzip compressed"*) then
#    mv vsx.ori vsx.ori.gz
    zcat vsx.ori.gz  | trim > vsx.ori
    if ($status != 0) then
        echo "****Error"
	exit 1
    endif
    #gunzip vsx.ori.gz | trim > vsx.ori
#else
#    mv vsx.ori vsx.tmp
#    trim vsx.tmp > vsx.ori && rm vsx.tmp
#endif
#wwwget $URL/vsxrefs.dat.gz > vsxrefs.dat.gz
wget $URL/vsxrefs.dat.gz -O vsxrefs.dat.gz $wget_opt
zcat vsxrefs.dat.gz | trim > refs.ori
if ($status != 0) then
    echo "****Error"
    exit 1
endif
cd ..

#############################################
# Compare with previous version
############################################

set dv = `ls -ld --time-style=+'%Y-%m-%d' v-1|awk '{print $6}'`
#foreach f ( readme.ori vsx.ori refs.ori)
# 02-Jun-2013: Ignore readme
foreach f ( vsx.ori refs.ori)
    gawk -v f="$f" '/^Last-Mod/{ printf "%-16s %s\n", f, $0}' tmp/$f:r.head
    #echo "tmp/$f - `grep -i '^Last-Mod' tmp/$f:r.head`"
    fcat v-1/$f | cmp - tmp/$f;
    if ($status) then
        echo "#---Modified since ${dv}: $f"
	@ modified += 1
    endif
end

if ($modified == 0) then
    echo "====No changes in VSX files" | tee tmp/=log=
    exit 0
endif

#############################################
# Rebuild VSX
############################################

cat <<===ToDo | tee -a =log=
====Rebuild the VSX files with:
cdcat B/vsx
./make_vsx

===ToDo
exit 2
