#!/usr/bin/ksh
#
# gbo_ualaska_mirror.ksh -  Using rsync, mirror the 
# Ground Magnetometer (GMAG) files that UAlaska 
# collects from the GIMA ground magnetometer chain.
#
#	Usage: gbo_ualaska_mirror.ksh [station] [year]
#
#	Arguments:
#						$1 = station
#						$2 = year
#
#									
# Creation Date:
#
#								13 August 2006 TimQuinn
#
#	

## Set the 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/gbo_ualaska_mirror.config
  . /home/thmsoc/gaia2_test/soc_it_to_me.config
  . /home/thmsoc/gaia2_test/gbo_ualaska_mirror.config

## Test Settings
#

## Function Definitions
#


## Take input arguments and build mirror directories
#
	station=$1
	year=$2
	args_dir_path=${station}/${year}/

	if [[ -z $station || -z $year ]]
	then
		echo Must set station - FtYukon or Gakona
		echo and year
		exit 1
	fi

	mirror_start_dir="$UCB_MIRROR_HOME/${args_dir_path}"
	mkdir -p $mirror_start_dir
	cd $mirror_start_dir

## Run rsync
#
	echo "$$:" >> $LOGFILE
	echo "$$:----------- Starting gbo_ualaska_mirror at $(date) ---------" >> $LOGFILE

	start_time=$(date -u '+%Y-%m-%d %T')

	filenum=0

	RSYNC_LOGFILE=${RSLOGFILE}_pid$$_${filenum}

	if [[ -n $5 ]]
	then
		count=$5
		while (( $count > 0 ))
		do
				echo "Starting rsync at directory level ${args_dir_path}" >> $LOGFILE
				echo "Starting rsync at directory level ${args_dir_path}" >> $RSYNC_LOGFILE
			#	Run rsync
				/usr/local/bin/rsync $RSYNC_OPTIONS_A --rsh="ssh -i $HOME/.ssh/rsync_ualaska -l gima" ${REMOTE_SERVER_NAME}/${args_dir_path} . >> $RSYNC_LOGFILE
				echo "RSYNC_STATUS = $?" >> $RSYNC_LOGFILE
				
			# Now increment date
	
		    dateseconds=$(date --date "${year}-${month}-${day}" +%s)
		    nextseconds=$(($dateseconds+60*60*24))
			nextday=$(date --date "1970-01-01 ${nextseconds} sec GMT" +%Y/%m/%d)

        	#deltadate doesn't work anymore
			#	nextday=$(/usr/bin/ssh juneau /disks/juneau/home/teq/ws/src/fastlib/deltaDate \
			#																${month}/${day}/${year} 1)
				month=$(echo $nextday |awk -F/ '{ printf("%02s",$2) }')
				day=$(echo $nextday |awk -F/ '{ printf("%02s",$3) }')
				year=$(echo $nextday |awk -F/ '{ printf("%s",$1) }')
				args_dir_path=$(echo $nextday |awk -F/ '{ printf("%s/%02s/%02s/",$1,$2,$3) }')
				args_dir_path=${school}/${args_dir_path}
				mirror_start_dir="$UCB_MIRROR_HOME/${args_dir_path}"
				mkdir -p $mirror_start_dir
				cd $mirror_start_dir
				count=$(( $count - 1 ))
				filenum=$(( $filenum + 1 ))
				RSYNC_LOGFILE=${RSLOGFILE}_pid$$_${filenum}
		done				
	else
			echo "Starting rsync at directory level ${args_dir_path}" >> $LOGFILE
			echo "Starting rsync at directory level ${args_dir_path}" >> $RSYNC_LOGFILE
			which rsync >> $LOGFILE
		    # Run rsync
			/usr/local/bin/rsync $RSYNC_OPTIONS_A --rsh="ssh -i $HOME/.ssh/rsync_ualaska -l gima" ${REMOTE_SERVER_NAME}/${args_dir_path} . >> $RSYNC_LOGFILE
			rsynccode=$?
			echo "RSYNC_STATUS = $rsynccode" >> $RSYNC_LOGFILE
			if (( $rsynccode != 0 ))
			then
				echo "WARNING RSYNC PROBLEM: Return code = $rsynccode" >> $LOGFILE
				##Disabled on 25Sep09 - too many messages
				#printf "%s\n" "script: $0" \
				#"Warning: UAlaska RSYNC Problem:code $rsynccode: path ${args_dir_path}" |
				#mailx -s "UAlaska RSYNC Problem" $SOC_EMAIL
			fi
	fi				

	end_time=$(date -u '+%Y-%m-%d %T')

## Process log files created by rsync run.
## Check rsync results by seeing if any files were
## recovered or updated
#
	logfilelist=$(ls -1 ${RSLOGFILE}_pid$$_*)
	for logfile in $logfilelist
	do
		# Search logfile for new or updated image files
		# and process into database if found
			egrep '(>f)' $logfile > /tmp/GBO_CHECK$$
			while [[ -s /tmp/GBO_CHECK$$ ]]
			do
				line=$(head -1 /tmp/GBO_CHECK$$)
				processCode=$(echo $line |awk -F\| '{print substr($1,3,8)}')
				path=$(echo $line |awk -F\| '{print $2}')
				processTime=$(echo $line |awk -F\| '{print $3}' | sed 's/\//-/g')
				fileSize=$(echo $line |awk -F\| '{print $4}'| sed 's/"//g')
				fileName=${path##*/}
				year=$(echo $fileName | awk -F. '{print substr($1,1,4)}')

				## The UAlaska file naming convention is non-uniform and
				## needs to be evaluated
				#
				gboName=$(echo $fileName |sed 's/[0-9][0-9]*//'|sed 's/[0-9][0-9]*//'|awk -F- '{print $1}')
				sedTemp=$(echo $fileName |sed 's/[[A-Za-z][A-Za-z]*//')
				doy=$(echo $sedTemp |awk -F. '{print substr($1,5,3)}')
				hr=$(echo $sedTemp |awk -F. '{print substr($1,9,2)}')

				## Create and fill file with year doy gboName
				## to be used for downstream processing into CDF
				#
				echo ${year}_${doy}_${gboName} >> ${GMAGMIRROR}/GIMA_Prep$$

				## Now determine dataTime
				#
				date=$(doy2date $year $doy)
				month=$(echo $date |awk -F/ '{print $2}')
				day=$(echo $date |awk -F/ '{print $3}')
				dataTime=$(echo $year $month $day $hr | awk '{printf("%s-%02s-%02s %02s:00:00",$1,$2,$3,$4)}')

				## Construct database table name
				#
				mysql_table_name=gbo_${gboName}_magfiles

				## Now insert file information into database
				#
				echo "$$:  Updating MySQL database table $mysql_table_name......." >> $LOGFILE
				echo "$$: NOT RUNNING gbo_ualaska_mirror.php $mysql_table_name $dataTime $fileName \
							$processTime $processCode $fileSize" >> $LOGFILE
				#gbo_ualaska_mirror.php $mysql_table_name \
				#	$dataTime $fileName $processTime $processCode $fileSize
				echo "$$:  ...........update complete. " >> $LOGFILE

				## Move to next line
				#
				sed '1d' /tmp/GBO_CHECK$$ > /tmp/gbo_check$$
				mv /tmp/gbo_check$$ /tmp/GBO_CHECK$$
			done
			rm -f /tmp/GBO_CHECK$$ 
		
		## Edit out duplicates in CDF processing file
		## and create file to be used by gima_netcdf2all.ksh
		## to convert netCDF to CDF and other formats
		#
			if [[ -f ${GMAGMIRROR}/GIMA_Prep$$ ]]
			then
				previous=NaN
				for line in $(cat ${GMAGMIRROR}/GIMA_Prep$$)
				do
					if [[ $line != $previous ]]
					then
						echo $line >> ${GMAGMIRROR}/tmp_GIMA_NC2ALL$$
						previous=${line}
					fi
				done
				mv ${GMAGMIRROR}/tmp_GIMA_NC2ALL$$ ${GMAGMIRROR}/GIMA_NC2ALL$$
				rm -f ${GMAGMIRROR}/GIMA_Prep$$
			fi
				
		## Process rsync statistics into database
		#
			rsync_status=$(grep RSYNC_STATUS $logfile|awk '{print $3}')
			if (( $rsync_status == 0 ))
			then
				rsync_dir=$(grep rsync $logfile |  awk '{print $6}')
				num_files=$(grep "Number of files transferred" $logfile | awk '{print $5}')
				tot_size=$(grep "Total file size" $logfile | awk '{print $4}')
				xfer_rate=$(grep "bytes/sec" $logfile | awk '{print $7}')
			#	/home/thmsoc/software_dev/php/gbo_ualaska_mirror_stats.php \
				#	$rsync_dir $start_time $end_time $num_files $tot_size $xfer_rate
			else
				echo "$$:" >> $LOGFILE
      	echo "$$:WARNING: rsync did not finish properly" >> $LOGFILE
      	echo "$$:WARNING: rsync code = $rsync_status" >> $LOGFILE
				##Disabled on 25Sep09 - too many messages
				#printf "Subject: %s\n%s\n" "RSYNC Failure" \
					#"WARNING: rsync code = $rsync_status. Check $LOGFILE" | mail $SOC_EMAIL
			fi
		rm -f $logfile
	done

	echo "$$:" >> $LOGFILE
	echo "$$:----------- Ending  gbo_ualaska_mirror at $(date) ---------" >> $LOGFILE

#
## Cleanup and exit
#
	exit 0
