#!/usr/bin/ksh
#
# gbo_asi_rt_mirror.ksh -  Using rsync, mirror image thumbnails and
# and other products collected from the GBOs by UCalgary.
#
# Creation Date:
#
#								11 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
. /home/thmsoc/gaia2_test/soc_it_to_me.config
	. ${THMSOC}/src/config/gbo_asi_rt_mirror.config

#
## Function Definitions
#


#
## Take input arguments and build mirror directories
#
	stream=$1
	year=$2
	month=$3
	day=$4

	if [[ -z $year && -z $month && -z $day ]]
	then
		args_dir_path=""
	elif [[ -z $month && -z $day ]]
	then
		args_dir_path=${year}/
	elif [[ -z $day ]]
	then
		args_dir_path=${year}/${month}/
	else
		args_dir_path=${year}/${month}/${day}
	fi

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

	echo "$$:" >> $LOGFILE
	echo "$$:----------- Starting gbo_asi_rt_mirror at $(date) ---------" >> $LOGFILE

#
## Run rsync
#
	echo "$$:" >> $LOGFILE
	echo "$$:Running rsync on server ${REMOTE_SERVER_NAME}" >> $LOGFILE
	echo "$$:Directory path = ${stream}/${args_dir_path}" >> $LOGFILE

	start_time=$(date -u '+%Y-%m-%d %T')
	RSYNC_LOGFILE=${RSLOGFILE}_${stream}_$$

	/usr/local/bin/rsync $RSYNC_OPTIONS --rsh="ssh -i $HOME/.ssh/rsync_ucalgary_id_dsa -l soc" ${REMOTE_SERVER_NAME}/${stream}/${args_dir_path} . >> $RSYNC_LOGFILE

	rsync_status=$?
	end_time=$(date -u '+%Y-%m-%d %T')
	echo "$$:RSYNC Complete. Start=$start_time End=$end_time" >> $LOGFILE

#
## Check rsync results by seeing if any files were
## recovered or updated
#
	egrep '(>f)' $RSYNC_LOGFILE > /tmp/GBO_RT_CHECK$$
	if [[ -s /tmp/GBO_RT_CHECK$$ ]]
	then
		while read line
		do
			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##*/}
			gboname=$(echo $fileName |awk -F_ '{print $3}' | sed 's/[a-z]*/\U&/g')
			mysql_table_name=GBO_${gboname}_RT
			imageYMD=$(echo $fileName | awk -F_ '{print substr($1,1,4)"-"substr($1,5,2)"-"substr($1,7,2)}')
			imageHH=$(echo $fileName | awk -F_ '{print $2}')
			if [[ -z $imageHH ]]
      then
        imageHH="00"
      fi
			imageHHMMSS=${imageHH}:00:00
			dataTime="$imageYMD $imageHHMMSS"

			#
			## If gzip file perform gunzip test
			#
			isgzip=$(echo $path |grep .gz)
			if [[ -n $isgzip ]]
			then
				gunziptest=$(gunzip -t ${mirror_start_dir}${path} 2>&1)
				if [[ -n $gunziptest ]]
				then
					# Failed test
					echo "$$:" >> $LOGFILE
					echo "$$:WARNING: File failed gunzip test" >> $LOGFILE
					echo "$$:WARNING: ${mirror_start_dir}${path}" >> $LOGFILE
					##Removed on 25Sep09 due to over occurrence of this error
    			#printf "%s\n" "script: $0" \
    			#"WARNING: File failed gunzip test: ${mirror_start_dir}${path}" | 
					#mailx -s "THEMIS UCalgary Realtime ASI gunzip Failure" $SOC_EMAIL
					mysql_table_name=GBO_${gboname}_RT_BAD
					mv ${mirror_start_dir}${path} $UCB_MIRROR_HOME/bad_files
				fi
			fi
		
			#
			## Input file information for later database update
			#
			echo $dataTime,$fileName,$processTime,$processCode,$fileSize >> ${DB_INPUT_FILES}/$mysql_table_name
			
		done < /tmp/GBO_RT_CHECK$$
		rm /tmp/GBO_RT_CHECK$$ 
	fi

#
## Process file information into database
#
	for path_n_file in $(ls ${DB_INPUT_FILES}/GBO_*_RT* 2>/dev/null)
	do
		tablename=$(basename $path_n_file)
#		update_asi_rt_tbl.php $tablename $path_n_file
#		mv $path_n_file ${DB_INPUT_FILES}/archive
	done

#
## Process rsync statistics into database
#
	statsTest=$(grep "Number of files transferred" $RSYNC_LOGFILE)
	if [[ -n $statsTest ]]
	then
		num_files=$(grep "Number of files transferred" $RSYNC_LOGFILE | awk '{print $5}')
		tot_size=$(grep "Total file size" $RSYNC_LOGFILE | awk '{print $4}')
		xfer_rate=$(grep "bytes/sec" $RSYNC_LOGFILE | awk '{print $7}')
		fullpath=${stream}/${args_dir_path}
#		gbo_asi_rt_mirror_tbl_input_stats.php $fullpath $start_time $end_time $num_files $tot_size $xfer_rate
	fi

	if (( $rsync_status != 0 ))
	then
		echo "$$:" >> $LOGFILE
		echo "$$:WARNING: rsync did not finish properly" >> $LOGFILE
		echo "$$:WARNING: rsync code = $rsync_status" >> $LOGFILE
		##Removed on 25Sep09 due to over occurrence of this error
    #printf "%s\n" "script: $0" \
    #"WARNING: $stream error - rsync code = $rsync_status. Check $LOGFILE" | 
		#mailx -s "THEMIS RSYNC Failure - UCalgary Realtime ASI" $SOC_EMAIL
	fi
		
	echo "$$:" >> $LOGFILE
	echo "$$:----------- Ending gbo_asi_rt_mirror at $(date) ---------" >> $LOGFILE


	exit 0
