#!/usr/bin/ksh
#
## list_asi_allsite_summaries.ksh 
##
##	This script makes file lists of the type: 
##
##     summary.2008.12.30.gif
##     keogram.2008.12.30.gif
##     average.2008.12.30.gif
##
## These files are found at the following location:
##
##     /disks/themisdata/thg/l0/asi/yyyy/mm
##
## $1 = yyyy (4 digit year)
## $2 = mm (2 digit month)
#


#
## 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

#
## 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)}')

		for type in average keograms summary orbit themis_foot themis_south
		do
			rm -f ${LOCAL}/asi_summary_list_${type}_${yearMonth}.txt
			find ${L0ASIDIR}/${year}/${month} -name ${type}\*  -exec /bin/basename {} \; >> ${LOCAL}/asi_summary_list_${type}_${yearMonth}.txt
			mv -f ${LOCAL}/asi_summary_list_${type}_${yearMonth}.txt ${FILELISTS}
		done
	done

#
## Cleanup
#
	exit 0
