#!/usr/bin/ksh
#
# gen_probe_l2_list.ksh
#
# This script will generate a file listing what l2
# cdfs have been produced


## 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
    
#
## Start creating file list
#
	rm -f ${LOCAL}/probe_l2_availability.txt
	printf '\nProbe Level 2 Data Availability\n>>%s<<\n' "`date`" >> ${LOCAL}/probe_l2_availability.txt

## Process sites
#
	currentdate=$(date -u '+%Y%m%d')
	oldmonth=NaN
	for date in $(cat ${CONFIGDIR}/date_list.txt)
	do
		if (( $date < 20070301 ))
		then
			continue
		fi

		if [[ $date = $currentdate ]]
		then
			## Rename temporary file list
			#
			mv -f ${LOCAL}/probe_l2_availability.txt ${FILELISTS}/probe_l2_availability.txt
			exit 0
		fi

		newmonth=$(echo $date | awk '{print substr($1,5,2)}')
		if [[ $newmonth != $oldmonth ]]
		then
			printf "\n=========================================\n" >> ${LOCAL}/probe_l2_availability.txt
			printf "Date    |PID|" >> ${LOCAL}/probe_l2_availability.txt
			for dtype in $(cat ${CONFIGDIR}/probe_l2_list.txt)
			do
				printf "%-3s|" $dtype >> ${LOCAL}/probe_l2_availability.txt
			done
			printf "\n=========================================\n" >> ${LOCAL}/probe_l2_availability.txt
			oldmonth=$newmonth
		fi
		for probe in $(cat ${CONFIGDIR}/probe_list.txt)
		do
			printf "%s|%s|" $date $probe>> ${LOCAL}/probe_l2_availability.txt
			for dtype in $(cat ${CONFIGDIR}/probe_l2_list.txt)
			do
				check=$(grep $date ${FILELISTS}/${probe}_l2_list_${dtype}.txt)
				if [[ -z $check ]]
				then
					check=
				else
					check=X
				fi
				printf "%3s|" $check >> ${LOCAL}/probe_l2_availability.txt
			done
			printf "\n-----------------------------------------\n" >> ${LOCAL}/probe_l2_availability.txt
		done
	done

#
## Rename temporary file list
#
	mv -f ${LOCAL}/probe_l2_availability.txt ${FILELISTS}/probe_l2_availability.txt

#
## Cleanup
#
	exit 0
