#!/sbin/ksh

echo "Configuring map products for STAR XL..."

export SHELL=`whence ksh`

USAGE="\n
$0 [[-m] [-f] [-o] [-r]| [-a]]\n
where\n
	-m cuts MetroMaps.\n
	-f cuts RegionalForecast maps.\n
	-o cuts RegionalObs maps.\n
	-r cuts Satellite/Radar maps.\n
	-a cuts All maps.\n
"



#####
# Init flags
#
MM_FLAG=n
RF_FLAG=n
RO_FLAG=n
SATRAD_FLAG=n
ALL_FLAG=n


#####
# Get args
#
if [ $# -eq 0 ]; then
	ALL_FLAG=y	#Default to cut all maps
else
	# Get opts
	while getopts mfora c
	do
		case $c in
			m)	MM_FLAG=y;;		#Cut MetroMaps
			f)	RF_FLAG=y;;		#Cut RegionalForecast maps
			o)	RO_FLAG=y;;		#Cut RegionalObs maps
			r)	SATRAD_FLAG=y;;	#Cut Satellite/Radar maps
			a)	ALL_FLAG=y;;	#Cut All maps
			\?)	echo $USAGE
				exit 2;;
		esac
	done

	shift `expr $OPTIND - 1`
fi



#####
# Cut MetroMaps
#
if [ \( $ALL_FLAG = "y" \) -o \( $MM_FLAG = "y" \) ]
then
	echo "Cutting MetroMaps basemaps."

	# CUT MMF MAPS
	for map_num in 01 02
	do

		INTERESTED=`/twc/bin/db_exp -F c_mmf_map_${map_num}_interested -s dif /twc/dif/wxl_dif`
		if [ "$INTERESTED" -ge 1 ]
		then
			SUPER_MAP=`/twc/bin/db_exp -F c_mmf_map_${map_num}_super_img -s dif /twc/dif/wxl_dif`
			BASE_MAP=`/twc/bin/db_exp -F c_mmf_map_${map_num}_map -s dif /twc/dif/wxl_dif`
			BASE_MAP_CTR_X=`/twc/bin/db_exp -F c_mmf_map_${map_num}_center_x -s dif /twc/dif/wxl_dif`
			BASE_MAP_CTR_Y=`/twc/bin/db_exp -F c_mmf_map_${map_num}_center_y -s dif /twc/dif/wxl_dif`
			BASE_MAP_WID=`/twc/bin/db_exp -F c_mmf_product_width -s dif /twc/dif/wxl_dif`
			BASE_MAP_HGT=`/twc/bin/db_exp -F c_mmf_product_height -s dif /twc/dif/wxl_dif`


			# We need to use the ".sgi" version for subsgi2wxl
			SUPER_MAP_SGI="${SUPER_MAP%.*}.sgi"

			if [ -f ${SUPER_MAP_SGI}.gz ]
			then
				# it may be a newer version or this might be the first time we are using it
				gunzip -f ${SUPER_MAP_SGI}.gz
			fi

			#create the proper sub image for the page to use
			/twc/bin/subsgi2wxl -i ${SUPER_MAP_SGI} -o ${BASE_MAP} -X ${BASE_MAP_CTR_X} -Y ${BASE_MAP_CTR_Y} -W ${BASE_MAP_WID} -H ${BASE_MAP_HGT} -a 255
		fi
	done
fi



#####
# Cut RegionalForecast maps
#
if [ \( $ALL_FLAG = "y" \) -o \( $RF_FLAG = "y" \) ]
then
	echo "Cutting RegionalForecast basemaps."

	# CUT RF MAPS
	for map_num in 01 02
	do

		INTERESTED=`/twc/bin/db_exp -F c_rf_map_${map_num}_interested -s dif /twc/dif/wxl_dif`
		if [ "$INTERESTED" -ge 1 ]
		then
			SUPER_MAP=`/twc/bin/db_exp -F c_reg_forecast_map_${map_num}_super_img -s dif /twc/dif/wxl_dif`
			BASE_MAP=`/twc/bin/db_exp -F c_reg_forecast_map_${map_num}_map -s dif /twc/dif/wxl_dif`
			BASE_MAP_CTR_X=`/twc/bin/db_exp -F c_reg_forecast_map_${map_num}_center_x -s dif /twc/dif/wxl_dif`
			BASE_MAP_CTR_Y=`/twc/bin/db_exp -F c_reg_forecast_map_${map_num}_center_y -s dif /twc/dif/wxl_dif`
			BASE_MAP_WID=`/twc/bin/db_exp -F c_rf_product_width -s dif /twc/dif/wxl_dif`
			BASE_MAP_HGT=`/twc/bin/db_exp -F c_rf_product_height -s dif /twc/dif/wxl_dif`


			# We need to use the ".sgi" version for subsgi2wxl
			SUPER_MAP_SGI="${SUPER_MAP%.*}.sgi"

			if [ -f ${SUPER_MAP_SGI}.gz ]
			then
				# it may be a newer version or this might be the first time we are using it
				gunzip -f ${SUPER_MAP_SGI}.gz
			fi

			#create the proper sub image for the page to use
			/twc/bin/subsgi2wxl -i ${SUPER_MAP_SGI} -o ${BASE_MAP} -X ${BASE_MAP_CTR_X} -Y ${BASE_MAP_CTR_Y} -W ${BASE_MAP_WID} -H ${BASE_MAP_HGT} -a 255
		fi
	done
fi



#####
# Cut RegionalObs maps
#
if [ \( $ALL_FLAG = "y" \) -o \( $RO_FLAG = "y" \) ]
then
	echo "Cutting RegionalObs basemaps."

	# CUT RO MAPS
	for map_num in 01 02
	do

		INTERESTED=`/twc/bin/db_exp -F c_ro_map_${map_num}_interested -s dif /twc/dif/wxl_dif`
		if [ "$INTERESTED" -ge 1 ]
		then
			SUPER_MAP=`/twc/bin/db_exp -F c_reg_obs_map_${map_num}_super_img -s dif /twc/dif/wxl_dif`
			BASE_MAP=`/twc/bin/db_exp -F c_reg_obs_map_${map_num}_map -s dif /twc/dif/wxl_dif`
			BASE_MAP_CTR_X=`/twc/bin/db_exp -F c_reg_obs_map_${map_num}_center_x -s dif /twc/dif/wxl_dif`
			BASE_MAP_CTR_Y=`/twc/bin/db_exp -F c_reg_obs_map_${map_num}_center_y -s dif /twc/dif/wxl_dif`
			BASE_MAP_WID=`/twc/bin/db_exp -F c_ro_product_width -s dif /twc/dif/wxl_dif`
			BASE_MAP_HGT=`/twc/bin/db_exp -F c_ro_product_height -s dif /twc/dif/wxl_dif`


			# We need to use the ".sgi" version for subsgi2wxl
			SUPER_MAP_SGI="${SUPER_MAP%.*}.sgi"

			if [ -f ${SUPER_MAP_SGI}.gz ]
			then
				# it may be a newer version or this might be the first time we are using it
				gunzip -f ${SUPER_MAP_SGI}.gz
			fi

			#create the proper sub image for the page to use
			/twc/bin/subsgi2wxl -i ${SUPER_MAP_SGI} -o ${BASE_MAP} -X ${BASE_MAP_CTR_X} -Y ${BASE_MAP_CTR_Y} -W ${BASE_MAP_WID} -H ${BASE_MAP_HGT} -a 255
		fi
	done
fi



#####
# Cut Satellite/Radar maps
#
if [ \( $ALL_FLAG = "y" \) -o \( $SATRAD_FLAG = "y" \) ]
then
	echo "Cutting Satellite/Radar basemaps."

	# CUT SAT/RAD MAPS
	for prod_type in sat rad
	do
		for map_num in 01
		do

			INTERESTED=`/twc/bin/db_exp -F c_${prod_type}_map_${map_num}_interested -s dif /twc/dif/wxl_dif`
			if [ "$INTERESTED" -ge 1 ]
			then
				SUPER_MAP=`/twc/bin/db_exp -F c_${prod_type}_map_${map_num}_super_img -s dif /twc/dif/wxl_dif`
				BASE_MAP=`/twc/bin/db_exp -F c_${prod_type}_map_${map_num}_map -s dif /twc/dif/wxl_dif`
				BASE_MAP_CTR_X=`/twc/bin/db_exp -F c_${prod_type}_map_${map_num}_center_x -s dif /twc/dif/wxl_dif`
				BASE_MAP_CTR_Y=`/twc/bin/db_exp -F c_${prod_type}_map_${map_num}_center_y -s dif /twc/dif/wxl_dif`
				BASE_MAP_WID=`/twc/bin/db_exp -F c_${prod_type}_product_width -s dif /twc/dif/wxl_dif`
				BASE_MAP_HGT=`/twc/bin/db_exp -F c_${prod_type}_product_height -s dif /twc/dif/wxl_dif`

				BORDERS_SUPER_MAP=`/twc/bin/db_exp -F c_${prod_type}_map_${map_num}_borders_super_img -s dif /twc/dif/wxl_dif`
				BORDERS_BASE_MAP=`/twc/bin/db_exp -F c_${prod_type}_map_${map_num}_borders_map -s dif /twc/dif/wxl_dif`

				# We want to use the borders map for both and just ignore the alpha chan when creating the basemap
				if [ -n "$BORDERS_SUPER_MAP" ]
				then
					SUPER_MAP=$BORDERS_SUPER_MAP
				fi

				# We need to use the ".sgi" version for subsgi2wxl
				SUPER_MAP_SGI="${SUPER_MAP%.*}.sgi"

				if [ -f ${SUPER_MAP_SGI}.gz ]
				then
					# it may be a newer version or this might be the first time we are using it
					gunzip -f ${SUPER_MAP_SGI}.gz
				fi

				#create the proper sub image for the page to use
				echo "/twc/bin/subsgi2wxl -i ${SUPER_MAP_SGI} -o ${BASE_MAP} -X ${BASE_MAP_CTR_X} -Y ${BASE_MAP_CTR_Y} -W ${BASE_MAP_WID} -H ${BASE_MAP_HGT} -a 255"
				/twc/bin/subsgi2wxl -i ${SUPER_MAP_SGI} -o ${BASE_MAP} -X ${BASE_MAP_CTR_X} -Y ${BASE_MAP_CTR_Y} -W ${BASE_MAP_WID} -H ${BASE_MAP_HGT} -a 255
				echo "/twc/bin/subsgi2wxl -i ${SUPER_MAP_SGI} -o ${BORDERS_BASE_MAP} -X ${BASE_MAP_CTR_X} -Y ${BASE_MAP_CTR_Y} -W ${BASE_MAP_WID} -H ${BASE_MAP_HGT}"
				/twc/bin/subsgi2wxl -i ${SUPER_MAP_SGI} -o ${BORDERS_BASE_MAP} -X ${BASE_MAP_CTR_X} -Y ${BASE_MAP_CTR_Y} -W ${BASE_MAP_WID} -H ${BASE_MAP_HGT}
			fi
		done
	done
fi





#####
# CLEANUP
#
# Try and remove unnescessary superimage files (those from other regions)
# in order to conserve disk space.
# 
# We are making a few assumptions here. They should be safe though.
# The assumption are
# 1- All locations will have at least 1 RF product
# 2- The sat/rad, RO, and RF are in the same region.
# 3- The sat/rad, RO, and RF superimage files are all in the same directory.
# 4- Only superimage files reside in this directory.
# 5- We will not be moving a star from one region to another w/o resending this patch or re-cutting the drive0
#

SUPER_MAP=`/twc/bin/db_exp -F c_reg_forecast_map_01_super_img -s dif /twc/dif/wxl_dif`
BASE_DIR=`dirname $SUPER_MAP`
BASE_NAME=`basename $SUPER_MAP`

#sanity check
if [ "${BASE_DIR}" = "/twc/superimages" ]
then
	# Strip everything after the "RegionalSuper" in BASE_NAME
	# Region will be something like "US", "Alaska", "Hawaii", "PuertoRico", or "Bahamas"
	REGION=${BASE_NAME%RegionalSuper*}
	if [ -n "${REGION}" ]
	then
		# Remove all the /twc/superimages/*.gz files which are not /twc/superimages/${REGION}* files
		# This will preserve unused (*.gz) files in our REGION just in case we might need them later.
		# Although now that we have radar everywhere, there should not be unused files (at least in
		# the US) which were not uncompressed above while cutting the basemaps.
		find ${BASE_DIR} -type f -name "*.gz" -print | grep -v "/${REGION}" | xargs rm -f
	fi
fi

