#!/bin/bash
#
#  Simple script to download configuration file for topBDII.
#  Arguments:
#    webURL:  to download file from
#    destination: this must be identical to "manual_file" in /etc/glite/glite-info-update-endpoints.conf
#    minSize: minimum fileSize for downloaded file, otherwise destination will not be overwritten
#
webUrl=$1
fileDest=$2
minSize=$3
if [ -z "$webUrl" -o -z "$fileDest" -o -z "$minSize" ]; then
   echo "Please define 3 arguments: webUrl fileDest minSize"
   echo " like for example:"
   echo "  $0 http://www.some.site/top-bdii.conf /opt/glite/etc/mytop-bdii.conf 2000"
   exit 10
fi
fileTemp=${fileDest}.test

wget $webUrl -O $fileTemp
retCode=$?
if [ $retCode -ne 0 ] || [ ! -s $fileTemp ] ; then
    echo "Exiting: unable to download file from $webUrl or filesize 0"
    exit 1
fi

size=`stat --printf="%s" $fileTemp`
if [ "$size" -lt "$minSize" ]; then
    echo "Exiting: downloaded only $size bytes, seems too small"
    exit 2
fi

# If we managed to get up to here, something reasonable has been downloaded
# We can move such "something" to its final destination.

mv -f $fileTemp $fileDest
retCode=$?
if [ $retCode -ne 0 ]; then
    echo "Unable to move $fileTemp to $fileDest check file privileges"
    exit 5
fi
exit 0


