#!/usr/bin/ksh
#
# wget_maccs_gmag.ksh 
#
# Download gmag data from MACCS data server, located
# at Augsburg College
#
#			Author: Tim Quinn
# 		Created: Apr 2009
#
#			Arguments:	
#
#			$1 = site
#			$2 = year
#
#			Updates:
#


#
## Set environment variables
#
  if [[ -z $THMSOC ]]
  then
    THMSOC=/disks/socware/thmsoc_dp_current
    export THMSOC
  fi
#	. $THMSOC/src/config/soc_it_to_me.config
#	. $THMSOC/src/config/wget_maccs_gmag.config
    . /home/thmsoc/gaia2_test/soc_it_to_me.config
    # no change to this config file:
    . $THMSOC/src/config/wget_maccs_gmag.config 
#
## Handle input arguments
#
	site=$1
	year=$2

#
## Set logfiles
#
	processdate=$(date '+%y%m%d')
	LOGFILE=${LOGDIR}/webdownloads/maccs_gmag_log_${processdate}_$$
	WGETLOG=${LOGDIR}/webdownloads/wget_output_maccs_gmag_${processdate}_$$

	echo ":$$:maccs_gmag_log:" >> $LOGFILE
	echo ":$$:maccs_gmag_log:Starting script wget_maccs_gmag at $(date)" >> $LOGFILE

#
## Run wget to download data from the MACCS server at Augsburg College.
## 
#
	remoteLocation=${WEBSITE}/${site}/${year}/
	wgetTime=$(date '+%Y-%m-%d %T')
	##/usr/bin/wget -nH --cut-dirs=2 -N -o $WGETLOG -P $LOCALWEBMIRROR -A '*.sec' ${remoteLocation}
	/usr/bin/wget -r -nH --cut-dirs=2 -N -o $WGETLOG -P $LOCALWEBMIRROR -A '*.sec' ${remoteLocation}

#
## Parse through wget output to see what was downloaded
#
	grep saved $WGETLOG | grep -v listing | awk '{print $6 " " $8}' > /tmp/wget_maccs_gmag$$
	while read line
	do
		path=$(echo $line |awk '{print $1}')
		size=$(echo $line |awk '{print $2}')

		fileName=$(basename $path)
		fileName=${fileName%*\'}

		# Parse date information from filename
		year=$(echo $fileName |awk -F_ '{print substr($1,4,4)}')
		mon=$(echo $fileName |awk -F_ '{print substr($1,8,2)}')
		day=$(echo $fileName |awk -F_ '{print substr($1,10,2)}')
		fileTime="${year}-${mon}-${day} 00:00:00"

		# Verify file exists
		if [[ ! -a ${LOCALWEBMIRROR}/${site}/${year}/${fileName} ]]
		then
    	echo "$$:maccs_gmag_log:" >> $LOGFILE
    	echo "$$:maccs_gmag_log: ${LOCALWEBMIRROR}/${site}/${year}/${fileName} not found. " >> $LOGFILE
    	echo "$$:maccs_gmag_log: continuing..................." >> $LOGFILE

			printf "%s\n" "script: $0" \
			"Warning: ${LOCALWEBMIRROR}/${site}/${year}/${fileName} not found." \
			"Check $LOGFILE" |
			mailx -s "MACCS Download Discrepancy" $SOC_EMAIL
		fi

		fileSize=${size%\]}
		fileSize=${fileSize#\[}

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

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

	done < /tmp/wget_maccs_gmag$$

#
## Cleanup
#
	rm -f /tmp/wget_maccs_gmag$$

exit 0
