#!/usr/bin/ksh
#
# wget_green_gmag.ksh
#
# Author: Patrick Cruce(pcruce@igpp.ucla.edu)
#
# Based heavily on wget_nrsq_gmag.ksh and wget_maccs_gmag.ksh by Tim Quinn(teq@ssl.berkeley.edu)
#
# $1 = site
# $2 = year
# $3 = month
# $4 = day
#
## Set environment variables
#
  if [[ -z $THMSOC ]]
  then
    THMSOC=/disks/socware/thmsoc_dp_current
    export THMSOC
  fi
	. $THMSOC/src/config/soc_it_to_me.config
	. /home/thmsoc/lphilpotts_stuff/greenland_test/wget_green_gmag_test.config

#
## Handle input arguments
#
site=${1}
year=${2}
month=${3}
day=${4}

#
## Set logfiles
#
	processdate=$(date '+%y%m%d')
	#temp
	#LOGDIR=/home/thmsoc/lphilpotts_stuff/greenland_test
	#changing to one log file per process date so that I don't have a log file for every file downloaded
	LOGFILE=${LOGDIR}/webdownloads/green_gmag_log_${processdate}
	#LOGFILE=${LOGDIR}/webdownloads/green_gmag_log_${processdate}_$$
	#changing to one log file per process date per site
	WGETLOG=${LOGDIR}/webdownloads/wget_output_green_gmag_${processdate}_${site}
	#WGETLOG=${LOGDIR}/webdownloads/wget_output_green_gmag_${processdate}_$$

	echo ":$$:green_gmag_log:" >> $LOGFILE
	echo ":$$:green_gmag_log:Starting script wget_green_gmag at $(date)" >> $LOGFILE

#
## Run wget to download data from Greenland GMAG Network
#

        filename=green_${site}_${year}${month}${day}.dat
	wgetTime=$(date '+%Y-%m-%d %T')
	url=${WEBSITE}\?site=${1}\&year=${2}\&month=${3}\&day=${4}\&${WEBOPTS}
	downloadfile=$(echo ${url} |awk -F\/ '{print $5}')
	downloadfilename=${LOCALWEBMIRROR}/${downloadfile}
	outfile=${LOCALWEBMIRROR}/${filename}
	# recursive, 1 level - prob unnec, don't create directories
	# -N checks time stamping, but this doesn't work with -O (write output to the one fie listed)
	#there is no time stamping on the files anyway!
	#/usr/bin/wget -r -l 1 -nd -o $WGETLOG -O $outfile $url
	# leaving -N so that on the off chance some file with the long url name exists the new file won't get downloaded as .1
	/usr/bin/wget -nd -N -o $WGETLOG -P $LOCALWEBMIRROR $url
	# -e test is the same as -a (deprecated)
	echo ":$$:green_gmag_log: Download Complete: $site" >> $LOGFILE
	if [[ -e $downloadfilename ]]
	then
	    echo "File downloaded"
	    #if file already exists check to see if new file is different
	    if [[ -e $outfile ]]
	    then
		cmp -s $downloadfilename $outfile
		filediff=$?
		echo "File diff: $filediff"
		if [[ $filediff -ne 0 ]]
		then
		# check for errors in the download that may slip through even though file is downloaded
		    echo "$$:green_gmag_log:" >> $LOGFILE
		    echo "$$:green_gmag_log: Downloaded file different to existing file." >> $LOGFILE
		    grep -i "error" ${WGETLOG} >> /dev/null                 
		    if [[ $? -eq 0 ]]
		    then
			echo "$$:green_gmag_log:" >> $LOGFILE
			echo "$$:green_gmag_log: ${downloadfilename} download error" >> $LOGFILE
			rm -f ${downloadfilename}
			exit 1
		    fi
		    mv $downloadfilename $outfile
		    cp $outfile ${GMAGWORKDIR}/${filename}
		else
		    echo "$$:green_gmag_log:" >> $LOGFILE
		    echo "$$:green_gmag_log: Downloaded file same as existing file." >> $LOGFILE
		    rm -f $downloadfilename
		fi
	    # if file doesn't already exist just check for errors and rename
	    else
		grep -i "error" ${WGETLOG} >> /dev/null                 
		if [[ $? -eq 0 ]]
		then
		    echo "$$:green_gmag_log:" >> $LOGFILE
		    echo "$$:green_gmag_log: ${downloadfilename} download error" >> $LOGFILE
		    rm -f ${downloadfilename}
		    exit 1
		fi
		mv $downloadfilename $outfile
		cp $outfile ${GMAGWORKDIR}/${filename}
	    fi
	else
	    echo "$$:green_gmag_log:" >> $LOGFILE
	    echo "$$:green_gmag_log: ${downloadfilename} not found. " >> $LOGFILE
	    echo "$$:green_gmag_log: No file downloaded?" >> $LOGFILE
	    exit 1
	fi
	
#
## Parse through wget output to see what was downloaded
#

	# Verify file exists
	#if [[ ! -a ${outfile} ]]
	#then
    	#  echo "$$:green_gmag_log:" >> $LOGFILE
    	#  echo "$$:green_gmag_log: ${outfile} not found. " >> $LOGFILE
	#  exit 1
        #fi

        #grep -i "error" ${WGETLOG} >> /dev/null                 
        #if [[ $? -eq 0 ]]
	#then
	#  echo "$$:green_gmag_log:" >> $LOGFILE
    	#  echo "$$:green_gmag_log: ${outfile} download error" >> $LOGFILE
        #  rm -f ${outfile}
	#  exit 1
	#fi
         
        #copy file into temporary location so it will be queued for downstream processing
        #cp $outfile ${GMAGWORKDIR}/${filename}

#		fileSize=$(echo $size |awk -F/ '{print $2}')
#		fileSize=${fileSize%*\]}

		# Update database with stats
#		${PHP_SCRIPTS}/wget_nrsq_gmag.php $fileName $wgetTime $fileTime $fileSize

		# Create a file used for CDF processing
#		touch ${GMAGMIRROR}/$fileName


exit 0
