#!/usr/bin/ksh

pause ()
{
	if [ $PAUSE_FLAG = "y" ]
	then
		echo "Hit ENTER to continue. "
		read ANS
	else
		echo "Sleeping for 5 sec..."
		sleep 5
	fi
}


build_dif ()
{

	print "Building DIF File..."
	pause

	#####
	# Now build the DIF with the correct STAR ID
	rm -f /twc/dif/wxl_dif.dat /twc/dif/wxl_dif.idx /twc/dif/wxl_dif.idx2 /twc/dif/wxl_dif.txt
#	cp /wxl_dev/twc/install/difs/${HEADEND_ID}.txt /twc/dif/wxl_dif.txt #just to keep track of the final product
#	/twc/bin/db_imp -s dif /twc/dif/wxl_dif /wxl_dev/twc/install/difs/${HEADEND_ID}.txt
	cd /twc
	/twc/build

	#####
	# Now pull in all the product pages
#	for i in /twc/product/*.txt
#	do
#		cat ${i} >> /twc/dif/wxl_dif.txt #just to keep track of the final product
#		/twc/bin/db_imp -s dif /twc/dif/wxl_dif ${i}
#	done

#	for i in /twc/product/prerender/*.txt
#	do
#		cat ${i} >> /twc/dif/wxl_dif.txt #just to keep track of the final product
#		/twc/bin/db_imp -s dif /twc/dif/wxl_dif ${i}
#	done

	#####
	#now pull in the lookup table for the proper language
#	LOOKUP_TABLE=`/twc/bin/db_exp -F c_rule_database -s dif /twc/dif/wxl_dif`
#	cat /wxl_dev/twc/install/rules/${LOOKUP_TABLE} >> /twc/dif/wxl_dif.txt #just to keep track of the final product
#	/twc/bin/db_imp -s dif /twc/dif/wxl_dif /wxl_dev/twc/install/rules/${LOOKUP_TABLE}

	cd /twc/scripts

	print "DIF File...Done"
}


###########################################
# uncompress only the needed super_images
###########################################
uncomp_supers()
{
	# For all of the uncompressed superimage files
	for i in /twc/superimages/*.wxl
	do
		# If we have a matching compressed file
		if [ -f ${i}.Z ]
		then
			# It may be an update, so we will remove
			# the uncompressed (older) version.
			rm -f ${i}
		fi
	done

	#compress what is not a dupe
	compress /twc/superimages/*.wxl

	echo "uncompressing the following super images"
	cat /wxl_dev/twc/install/difs/${HEADEND_ID}.txt | grep "/twc/superimages/" | grep wxl | cut -f4 -d '"' | sort | sort -u

	cat /wxl_dev/twc/install/difs/${HEADEND_ID}.txt | grep "/twc/superimages/" | grep wxl | cut -f4 -d '"' | sort | sort -u | xargs uncompress
	echo "super_image decompression complete"

	echo "LATER we need to REMOVE any unused super images"
	#rm -f /twc/superimages/*.wxl

	sleep 3
}


link_icons()
{
	CRED_PAGE=`/twc/bin/db_exp -F c_credits_page -s dif /twc/dif/wxl_dif`
	ICON_LANG=`/twc/bin/db_exp -F c_icon_language -s dif /twc/dif/wxl_dif`
	ICON_LAT=`/twc/bin/db_exp -F c_moon_hemisphere -s dif /twc/dif/wxl_dif`
	MOON_SIZE=`/twc/bin/db_exp -F c_moon_size -s dif /twc/dif/wxl_dif`


	#link up icon dirs first
	if [ -n "${ICON_LANG}" ]
	then
		rm -f /twc/large_weather_icons
		rm -f /twc/medium_weather_icons
		rm -f /twc/small_weather_icons
		rm -f /twc/moon_phase_icons
		rm -f /twc/titles
		ln -s /twc/large_weather_icons_${ICON_LANG} /twc/large_weather_icons
		ln -s /twc/medium_weather_icons_${ICON_LANG} /twc/medium_weather_icons
		ln -s /twc/small_weather_icons_${ICON_LANG} /twc/small_weather_icons
		ln -s /twc/moon_phase_icons_${ICON_LAT}_${ICON_LANG}_${MOON_SIZE} /twc/moon_phase_icons
		ln -s /twc/titles_${ICON_LANG} /twc/titles
	fi
}


cut_maps ()
{

	print "Installing and Configuring Maps..."
	pause

	#####
	# Now run config_map to update the base maps.
	cd /twc/bin
	/twc/bin/config_maps
	print "Installing and Configuring Maps...Done"
	cd /twc/scripts

	#####
	# Now build the CLEAR sat loop
#	print "Generating clear movie sequence..."
#	SAT_CLIPS_PATH=`/twc/bin/db_exp -F c_satellite_clips_path -s dif /twc/dif/wxl_dif`
#	SAT_NO_DATA_MOVIE=`/twc/bin/db_exp -F c_satellite_no_data_movie -s dif /twc/dif/wxl_dif`
#	rm ${SAT_CLIPS_PATH}/*.wxl
#	rm ${SAT_CLIPS_PATH}/*.mv
#	/twc/bin/PelOrion -r ${SAT_NO_DATA_MOVIE} -v -x
	print "DONE"
}


update_timezone()
{
	#Create a proper TIMEZONE file
	/twc/scripts/mk_tz_file.sh
}




################
# MAIN
################

cd /twc/scripts

export SHELL=`whence ksh`
export WXLINST=/twc/scripts

USAGE="$0 [-h] [-p] [-s star_id]"


PAUSE_FLAG=n
HALT_FLAG=n
STAR_FLAG=n

# Get opts
#set -- `getopt hps: $*`

#if [ $? -ne 0 ]; then
#	echo $USAGE
#	pause
#	exit 2
#fi

#while [ $# -gt 0 ]; do
#	case $1 in
#	-s)
#		STAR_FLAG=y	#Install Star specific parts.
#		HEADEND_ID=$2
#		echo HEADEND_ID = [$HEADEND_ID]
#		export HEADEND_ID
#		shift 2
#		;;
#	-h)
#		HALT_FLAG=y	#Do a shutdown when complete
#		shift
#		;;
#	-p)
#		PAUSE_FLAG=y	#Do not pause for input
#		shift
#		;;
#	--)
#		shift
#		break
#		;;
#	-?)	echo $USAGE
#		pause
#		exit
#		;;
#	esac
#done

#if [ -f $WXLINST/difs/${HEADEND_ID}.txt ]
#then
#	echo Installing for headend ${HEADEND_ID}
#else
#	echo You must supply a headend ID number.
#	echo $WXLINST/difs/${HEADEND_ID}.txt does not exits.
#	exit
#fi


	cp /etc/inittab.alloff /etc/inittab
	killall -2 PelOrion
	sleep 5
	killall cal defr30 deframer
	sleep 3

	build_dif
	#uncomp_supers
	#link_icons
	cut_maps
	update_timezone

	#final cleanup
	cp /etc/inittab.allon /etc/inittab
	init q


print ""
print "STAR INSTALLATION COMPLETE!"


#####print "THE STAR XL WILL BE REBOOTED..."
#####
#####if [ $HALT_FLAG = "y" ]
#####then
#####	shutdown -y -g0 -p
#####else
#####	print "Hit ENTER to continue or ^C to abort reboot"
#####	#we don't use pause() because we allways want to stop here
#####	read ANS
#####
#####	clear
#####	reboot
#####fi


