#!/usr/bin/ksh
#
## list_asi_site_summaries.ksh 
##
## Make lists of low rate asi summary files. These are
## the numerous gif files like GILL.2008.12.30.06.41.gif
## found at directories like:
##
##		/disks/themisdata/thg/l0/asi/2008/12/30/GILL/
##
## $1 = yyyy (4 digit year)
## $2 = mm (2 digit month)
## $3 = SITE (ATHA,WHIT,MOSA,etc)

#
## 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/astraea2_test/soc_it_to_me.config
	. ${THMSOC}/src/config/ground_asi.config
	#. /home/thmsoc/astraea2_test/ground_asi_test.config - no need as I need it
	# to read the files from production area, no asi files in test area
	# file lists produced are saved to test area

#
## Check if arguments passed
#
	thisYear=$1
	thisMonth=$2
	oneSite=$3
	if [[ -z $thisYear ]]
	then
		thisYear=$(date -u '+%Y')
		thisMonth=$(date -u '+%m')
	fi
	if [[ -z $thisMonth ]]
	then
		echo Please pas 2 digit month also
		exit 1
	fi
	integer ithisMonth=$thisMonth
	integer ilastMonth=$(( ithisMonth - 1 ))

  if (( $ilastMonth <= 0 ))
  then
    lastMonth=12
    lastYear=$(( $thisYear - 1 ))
  else
    lastYear=$thisYear
    lastMonth=$(echo $ilastMonth | awk '{printf("%02d",$1)}')
  fi

	thisYearMonth=${thisYear}${thisMonth}
	lastYearMonth=${lastYear}${lastMonth}

#
## Generate new site files
#
	for yearMonth in $thisYearMonth $lastYearMonth
	do
		year=$(echo $yearMonth | awk '{print substr($1,1,4)}')
		month=$(echo $yearMonth | awk '{print substr($1,5,2)}')
		if [[ -z $oneSite ]]
		then
			for site in $(cat ${CONFIGDIR}/uc_asi_site_list.txt)
			do
				find ${L0ASIDIR}/${year}/${month}/*/${site}/ -name \*${site}\*  -exec /bin/basename {} \; >> ${LOCAL}/asi_summary_list_${site}_${yearMonth}.txt
				mv -f ${LOCAL}/asi_summary_list_${site}_${yearMonth}.txt ${FILELISTS}/asi_summary_list_${site}_${yearMonth}.txt
			done
		else
			find ${L0ASIDIR}/${year}/${month}/*/${oneSite}/ -name \*${oneSite}\*  -exec /bin/basename {} \; >> ${LOCAL}/asi_summary_list_${oneSite}_${yearMonth}.txt
			mv -f ${LOCAL}/asi_summary_list_${oneSite}_${yearMonth}.txt ${FILELISTS}/asi_summary_list_${oneSite}_${yearMonth}.txt
		fi	
	done

#
## Cleanup
#
	exit 0
