#!/usr/bin/ksh
#
# gbo_uc_rmd_mirror.ksh -  Using rsync, mirror the 
# Ground Magnetometer (GMAG) RMD files that UCalgary 
# collects from the GBO sites.
#
#	Usage: gbo_ucla_rmd_mirror.ksh [year] [month] [day] [duration]
#
#	Arguments:
#						$1 = year
#						$2 = month
#						$3 = day
#						$4 = duration
#
#									
# Creation Date:
#
#								22 march 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_uc_rmd_mirror.config
	. /home/thmsoc/gaia2_test/soc_it_to_me.config
	. /home/thmsoc/gaia2_test/gbo_uc_rmd_mirror.config
# Take input arguments and build mirror directories
#
	year=$1
	month=$2
	day=$3
	args_dir_path=${year}/${month}/${day}/

	if [[ -z $month && -z $day ]]
	then
		args_dir_path=${args_dir_path%%//}
	elif [[ -z $day ]]
	then
		args_dir_path=${args_dir_path%/}
	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_uc_rmd_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
				echo "Rsync Options Used: $RSYNC_OPTIONS" >> $RSYNC_LOGFILE
				echo "Rsync Path Requested: ${REMOTE_SERVER_NAME}/${args_dir_path}" >> $RSYNC_LOGFILE
			#	Run rsync
				/usr/local/bin/rsync $RSYNC_OPTIONS --rsh="ssh -i $HOME/.ssh/rsync_ucalgary_id_dsa -l soc" ${REMOTE_SERVER_NAME}/${args_dir_path} . >> $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
		    echo "Rsync Options Used: $RSYNC_OPTIONS" >> $RSYNC_LOGFILE
		    echo "Rsync Path Requested: ${REMOTE_SERVER_NAME}/${args_dir_path}" >> $RSYNC_LOGFILE
			which rsync >> $LOGFILE
		# Run rsync
				/usr/local/bin/rsync $RSYNC_OPTIONS --rsh="ssh -i $HOME/.ssh/rsync_ucalgary_id_dsa -l soc" ${REMOTE_SERVER_NAME}/${args_dir_path} . >> $RSYNC_LOGFILE
			rsynccode=$?
			if (( $rsynccode != 0 ))
			then
				echo "WARNING RSYNC PROBLEM: Return code = $rsynccode" >> $LOGFILE
				printf "%s\n" "script: $0" \
				"Warning: UCalgary RSYNC Problem:code $rsynccode: path ${args_dir_path}" |
				mailx -s "UCalgary 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##*/}
				schName=$(echo $fileName |awk -F_ '{print substr($1,1,4)}')
				year=$(echo $fileName | awk -F_ '{print "20"substr($1,5,2)}')
				month=$(echo $fileName | awk -F_ '{print substr($1,7,2)}')
				day=$(echo $fileName | awk -F_ '{print substr($1,9,2)}')
				hr=$(echo $fileName | awk -F_ '{print $3}')
				mn=$(echo $fileName | awk -F_ '{print substr($4,1,2)}')
				dataTime="${year}-${month}-${day} ${hr}:${mn}:00" 
				mysql_table_name=gbo_${schName}_rmdfiles
				echo "$$:  Updating MySQL database table $mysql_table_name......." >> $LOGFILE
				echo "$$: NOT RUNNING gbo_uc_rmd_mirror.php $mysql_table_name $dataTime $fileName \
							$processTime $processCode $fileSize" >> $LOGFILE
			#	gbo_uc_rmd_mirror.php $mysql_table_name \
				#	$dataTime $fileName $processTime $processCode $fileSize
				echo "$$:  ...........update complete. " >> $LOGFILE
				sed '1d' /tmp/GBO_CHECK$$ > /tmp/gbo_check$$
				mv /tmp/gbo_check$$ /tmp/GBO_CHECK$$
			done
			rm /tmp/GBO_CHECK$$ 

		# Process rsync statistics into database
			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}')
		#	gbo_uc_rmd_mirror_stats.php \
			#		$rsync_dir $start_time $end_time $num_files $tot_size $xfer_rate

		# Now, create a file containing just the directories
    # that have had files updated by rsync. These directories
    # will be processed to convert the RMDs to FFs
    #
      #egrep RMD $logfile | awk -F\| '{print $2}' > /tmp/GBO_RMD_Files$$
      egrep '(>f)' $logfile | awk -F\| '{print $2}' > /tmp/GBO_RMD_Files$$
      previous=NaN
      for line in $(cat /tmp/GBO_RMD_Files$$)
      do
				filename=$(basename $line)
				sub_filename=$(echo $filename |awk '{print substr($1,1,10)}')
				site=$(echo $sub_filename |awk '{print substr($1,1,4)}')
        if [[ $sub_filename != $previous ]]
        then
					if [[ $site != "PGEO" ]]
					then
          	echo $sub_filename >> $GMAGMIRROR/GBO_RMDdirs$$
          	previous=${sub_filename}
					fi
        fi
      done
      rm /tmp/GBO_RMD_Files$$

	done

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


exit 0
