#!/usr/bin/ksh
#
# aari_gmag_mirror.ksh
#
# Author: Lydia Philpott (lphilpott@igpp.ucla.edu)
#
#
# This script uses rsync to download data from magnetometers from AARI (Russia)
# Data is one minute resolution variation data in IAGA2002 format.
#
# $1 = year
# $2 = month

## 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/aari_test/soc_it_to_me_test.config
	#. $THMSOC/src/config/aari_gmag.config
	. /home/thmsoc/lphilpotts_stuff/aari_test/aari_gmag.config

#
## Handle input arguments
#
year=${1}
month=${2}

#
## Set logfiles
#
	processdate=$(date '+%y%m%d')
	LOGFILE=${AARILOGDIR}/aari_gmag_mirror_log_${processdate}
	RSYNC_LOGFILE=${AARILOGDIR}/rsync_output_aari_gmag_${processdate}_$$


	echo ":$$:aari_gmag_log:" >> $LOGFILE
	echo ":$$:aari_gmag_log:Starting script aari_gmag_mirror at $(date)" >> $LOGFILE
	
	args_dir_path=${year}/${month}/
	mirror_start_dir="$UCB_MIRROR_HOME/${args_dir_path}"
	mkdir -p $mirror_start_dir
	cd $mirror_start_dir
	
	start_time=$(date -u '+%Y-%m-%d %T')

#
## Run rsync
#

	echo "Starting rsync at directory level ${args_dir_path}" >> $LOGFILE
	echo "Starting rsync at directory level ${args_dir_path}" >> $RSYNC_LOGFILE
	#	Run rsync
	/usr/bin/rsync $RSYNC_OPTIONS_A --rsh="ssh -i $HOME/.ssh/rsync_aari_id_dsa -l themis" ${REMOTE_SERVER_NAME}${args_dir_path} . >> $RSYNC_LOGFILE
	echo "RSYNC_STATUS = $?" >> $RSYNC_LOGFILE
	
	echo ":$$:aari_gmag_log: Rsync Complete: $args_dir_path" >> $LOGFILE
  
  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
#

	logfile=$RSYNC_LOGFILE
	# Search logfile for new or updated image files
	# and process into database if found
	egrep '(>f)' $logfile > /tmp/aari_gmag$$
	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##*/}
		site=$(echo $fileName |awk -F. '{print substr($1,1,3)}')
		year=$(echo $fileName | awk -F. '{print substr($1,4,4)}')
		month=$(echo $fileName | awk -F. '{print substr($1,8,2)}')
		day=$(echo $fileName | awk -F_ '{print substr($1,10,2)}')
		#dataTime="${year}-${month}-${day} 00:00:00"
		fileTime=$(echo $year $month $day | awk '{printf("%s-%02s-%02s 00:00:00", $1,$2,$3)}') 
		echo "$$:  Updating MySQL database table ......." >> $LOGFILE
		echo "$$: aari_gmag.php $fileName $processTime $fileTime $fileSize" >> $LOGFILE
		# Update database with stats
		#${PHP_SCRIPTS}/aari_gmag.php $fileName $processTime $fileTime $fileSize
		echo "$$:  ...........update complete. " >> $LOGFILE
		
		# Create a file used for CDF processing
		cp ${UCB_MIRROR_HOME}/${year}/${month}/${fileName} ${GMAGWORKDIR}/$fileName
	done < /tmp/aari_gmag$$

#
	rm -f /tmp/aari_gmag$$


exit 0
