#!/usr/bin/ksh
#
# gen_gmag_raw_list.ksh
#
# This script will generate a file listing what gmag
# raw file have been received


## 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_mag.config
  . /home/thmsoc/astraea2_test/ground_mag_test.config

#
## Start list file creation
#
	printf '\n\t\t\t\t\t\t  >>>>> GMAG Raw Data Availability <<<<<\n\t\t\t\t\t\t\t%s\n\n' "`date`" >> ${LOCAL}/gmag_raw_list_tmp.txt

## Process sites
#
	currentdate=$(date -u '+%Y%m%d')
	oldmonth=NaN
	for date in $(cat ${CONFIGDIR}/date_list.txt)
	do
		if [[ $date = $currentdate ]]
		then
			## Rename temporary file
			#
			mv -f ${LOCAL}/gmag_raw_list_tmp.txt ${FILELISTS}/gmag_raw_list.txt
			exit 0
		fi
		newmonth=$(echo $date | awk '{print substr($1,5,2)}')
		if [[ $newmonth != $oldmonth ]]
		then
			printf "\n=======================================================================================================================================================================================================================================================================================\n" >> ${LOCAL}/gmag_raw_list_tmp.txt
			printf "Date    |" >> ${LOCAL}/gmag_raw_list_tmp.txt
			for site in $(cat ${CONFIGDIR}/lc_site_list.txt)
			do
				printf "%s|" $site >> ${LOCAL}/gmag_raw_list_tmp.txt
			done
			printf "\n=======================================================================================================================================================================================================================================================================================\n" >> ${LOCAL}/gmag_raw_list_tmp.txt
			oldmonth=$newmonth
		fi
		printf "%s|" $date >> ${LOCAL}/gmag_raw_list_tmp.txt
		for site in $(cat ${CONFIGDIR}/lc_site_list.txt)
		do
			check=$(grep $date ${FILELISTS}/gmag_raw_*_${site}.txt 2>/dev/null)
			if [[ -z $check ]]
			then
				check=
			else
				check=X
			fi
			printf "%4s|" $check >> ${LOCAL}/gmag_raw_list_tmp.txt
		done
		printf "\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/gmag_raw_list_tmp.txt
	done

#
## Rename temporary file
#
	mv -f ${LOCAL}/gmag_raw_list_tmp.txt ${FILELISTS}/gmag_raw_list.txt

#
## Cleanup
#
	exit 0