#!/bin/sh
#
#------------------------------------------------
# FXinstall
#
#    This script performs the following: 
#    - Checks for proper version of java
#    - Checks if D2D code is installed
#    - Mounts the CD and reads FXC code
#    - Untars the FXC code
#    - Creates menu file
#    - Creates map backgrounds
#    - Creates a localization
#    - Starts the purger
#    - Exports transformation coordinates
#    - Adds a new server to menu 
#
#	Uses files:
#      FXC/fsl/linux/fxcHome
#      FXC/fsl/linux/fxcenvirons.csh
#      FXC/fsl/data/config/WFOids.txt
#      FXC/fsl/data/config/GeneralConfig.txt
#      FXC/fsl/data/config/XXX/MenusAndToolbars.txt
#      FXC/fsl/data/config/Hosts.txt
#      FXC/fsl/data/config/Localizations.txt
#      FXC/fsl/data/config/radarInfoMaster.txt
#      FXC/fsl/support/MenuMaker.java
#      FXC/fsl/support/makeLocalization
#      FXC/fsl/support/CreateLocalMaps.java
#
# Author: Herb Grote  4/26/02
#
#--------------------Global Data---------------

	# Static list of radar keys and labels
	# Ref: /awips/fxa/data/localizationDataSets/FSL/radarDepictKeys.template
	base=1073741824

	lab1="0.5 Reflect"
	lab2="0.9 Reflect"
	lab3="1.5 Reflect"
	lab4="1.8 Reflect"
	lab5="2.4 Reflect"
	lab6="3.4 Reflect"
	lab7="4.3 Reflect"
	lab8="5.3 Reflect"
	lab9="6.0 Reflect"
	lab10="7.5 Reflect"
	lab11="0.5 Velocity"
	lab12="0.9 Velocity"
	lab13="1.5 Velocity"
	lab14="1.8 Velocity"
	lab15="2.4 Velocity"
	lab16="3.4 Velocity"
	lab17="4.3 Velocity"
	lab18="5.3 Velocity"
	lab19="6.0 Velocity"
	lab20="7.5 Velocity"
	lab21="0.5 Storm Rel Vel"
	lab22="0.9 Storm Rel Vel"
	lab23="1.5 Storm Rel Vel"
	lab24="2.4 Storm Rel Vel"
	lab25="3.4 Storm Rel Vel"
	lab26="1hr Precip"
	lab27="Storm Total Precip"
	lab28="1km Composite Ref(CZ)"
	lab29="Vert Integ Liq(VIL)"
	lab30="VAD Wind Profile"

	genericKey=( 10005 10009 10015 10018 10024 10034 10043 10053 10060 10075 \
		     11005 11009 11015 11018 11024 11034 11043 11053 11060 11075 \
		     13805 13809 13815 13824 13834 50507 50111 50404 50505 50040 )

	label=( "$lab1" "$lab2" "$lab3" "$lab4" "$lab5" "$lab6" "$lab7" "$lab8" "$lab9" "$lab10" \
		"$lab11" "$lab12" "$lab13" "$lab14" "$lab15" "$lab16" "$lab17" "$lab18" "$lab19" "$lab20" \
		"$lab21" "$lab22" "$lab23" "$lab24" "$lab25" "$lab26" "$lab27" "$lab28" "$lab29" "$lab30" )

	LUT=( "REF" "REF" "REF" "REF" "REF" "REF" "REF" "REF" "REF" "REF" \
	      "VEL" "VEL" "VEL" "VEL" "VEL" "VEL" "VEL" "VEL" "VEL" "VEL" \
	      "OSFSRM" "OSFSRM" "OSFSRM" "OSFSRM" "OSFSRM" "REF" "REF" "REF" "REF" "PWIND" )

# -----------------------------------------------
# Comment and Uncomment the desired configuration.
# Default uncomment is set to ISB.
# ------------------------------------------------
#	ASDAD configuration
#	javaVersion=1.4.2_08

#	ISB configuration 
	javaVersion=1.4.2_08
	javaVersionString=`echo $javaVersion | tr "." "_"`
	configFile=/FXC/fsl/data/config/GeneralConfig.txt
	Q=\"

	# Determine installation directory 
	if [ -f FXC/fsl/linux/fxcHome ]; then
		homeDir=`more FXC/fsl/linux/fxcHome`
	elif [ -f fxcHome ]; then
		homeDir=`more fxcHome`
	fi 

	# Erase any old scratch files
	rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2 > /dev/null 2>&1

        umask 2
	clear

#------------------ Functions ---------------------

getCurrentLocalization()
{
	# This function obtains the current localization
	# to be used by FXC. Normally, this is defined
	# by an environmental variable (do not use
	# FXCLOCALIZATION in GeneralConfig.txt!).
	#
	# Output Parameter 1: "localization" (GLOBAL)
	# Output Parameter 2: "server" (GLOBAL)

	# Define local system environment
	if test -z $FXA_LOCAL_SITE; then
		printf "\nThis account does not have the proper AWIPS"
		printf "\nenvironment! Only able to run FXC client."
		printf "\nSpecify client localization (WFO ID): "
		read localization
		echo $localization > /tmp/scratch
		localization=`tr "a-z" "A-Z" < /tmp/scratch`
		rm -f /tmp/scratch > /dev/null 2>&1
		# Save in fxcenvirons.csh once file is loaded
		server=no
	else	
		# FXC server will use AWIPS localizations
		# (May be redefined in fxcenvirons.csh)
		localization=$FXA_LOCAL_SITE
		server=yes
	fi
}

getCurrentMenuName()
{
	# This function retrieves the name of the menu to be used
	# from the GeneralConfig.txt file (or WFOids.txt).
	#
	# Input Parameter 1: localization
	# Input Parameter 2: default menu
	#
	# Output Parameter 1: "menuName" (GLOBAL)

	if [ -f $homeDir$configFile ]; then
		dummy=fileFound
	else
		echo "creating config file" 
		cp $homeDir$configFile".template"  $homeDir$configFile
	fi

	# Check if GeneralConfig.txt file specifies "Default" menu
	menuName=`awk '/MENUOPTION/{print $2}' $homeDir$configFile | tr -d '\r'`

	if [ $menuName == "Default" -o $2 == "Default" ]; then 
		# Determine actual menu from WFOids.txt file
		grep "^$1 " $homeDir/FXC/fsl/data/config/WFOids.txt > /tmp/scratch
		if [ $? -ne 0 ]; then
		        echo "Localization $1 not found"
			echo " "
			rm -f /tmp/scratch /tmp/scratch
			exit 21
		else 
			# extract line where localization is in first column
			# (Find menu by using "-" field separator)
			menuName=`awk --field-separator=" - " '{print $3}' /tmp/scratch`
			rm -f /tmp/scratch > /dev/null 2>&1
		fi
	fi
}

saveAndRestore()
{
	# This script saves customized information from
	# the old FXC installation and applies them
	#to the current installation.
	#
	# Input Parameter 1:  Home directory
	# Input Parameter 2:  Action (i.e. save or restore)
	#

	getCurrentLocalization

	if [ $2 == "save" ]; then

		# Save sendToWeb script
                cp $1/FXC/fsl/linux/sendToWeb /tmp/sendToWeb > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo "Saved sendToWeb script"
		else
			echo "Unable to save sendToWeb script"
		fi

                # Save map information
                mkdir /tmp/fxcMaps > /dev/null 2>&1
                cp $1/FXC/fsl/data/config/Maps.txt /tmp/Maps.txt > /dev/null 2>&1
                cp $1/FXC/fsl/data/depictables/backgrounds/$localization/* /tmp/fxcMaps/ > /dev/null 2>&1
		if [ -f /tmp/fxcMaps/SkewT_Background.ALL ]; then
			echo "Saved map backgrounds"
		else
			echo "Unable to save map backgrounds"
		fi

                # Save color tables
                mkdir /tmp/color > /dev/null 2>&1
                ls "$1/FXC/fsl/data/color/userDefined"* > /dev/null 2>&1
                if [ $? -eq 0 ]; then
                        cp "$1/FXC/fsl/data/color/userDefined"* /tmp/color/ > /dev/null 2>&1
                        cp $1/FXC/fsl/data/state/colorTables.state /tmp/colorTables.state > /dev/null 2>&1
			echo "Saved custom color tables defined by user"
                fi 

        elif [ $2 == "restore" ]; then

                # Restore sendToWeb script
		cp /tmp/sendToWeb $1/FXC/fsl/linux/ > /dev/null 2>&1
                if [ $? -eq 0 ]; then
                        rm -f /tmp/sendToWeb
                        echo "Restored sendToWeb script"
                else
			echo "Unable to find backup sendToWeb file"
		fi

                # Restore map information 
                if [ -d $1/FXC/fsl/data/depictables/backgrounds/$localization ]; then
                        mapsFile="$1/FXC/fsl/data/config/Maps.txt"
                        # List file names of backed up files
			sed -e '/#/d' /tmp/Maps.txt | awk '{print $NF}' > /tmp/scratch
                        # From this list determine which maps need to be added
                        list=`cat /tmp/scratch`
                        for filename in $list
                        do 
                                grep $filename $mapsFile > /dev/null 2>&1
                                if [ $? -ne 0 ]; then
                                        # Map file not in current map list
                                        # Append line to active Maps.txt
					grep $filename /tmp/Maps.txt > /tmp/scratch
					more /tmp/scratch >> $mapsFile

                                        # Insert menu selector in MenuAndToolbars.txt
					#configFile="/FXC/fsl/data/config/GeneralConfig.txt"

                                        #awk '/MENUOPTION/{print $2}' $1$configFile | tr -d '\r' > /tmp/scratch
                                        getCurrentMenuName $localization fromConfigFile 
                                        menuFile="$1/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt"
                                        grep $filename /tmp/Maps.txt > /tmp/scratch

					#  Extract map name within quotes or without quotes
                                        awk '{print $1}' /tmp/scratch  > /tmp/scratch1
                                        grep \" /tmp/scratch1 > /dev/null 2>&1
					if [ $? -ne 0 ]; then
						mapName=`more /tmp/scratch1` 
					else
						mapName=`awk --field-separator="\"" '{print $2}' /tmp/scratch`
					fi

                                        sed -e '/# Insert maps here/a\' -e "     TOGGLEITEM \"$mapName\" M \"\" \"map $mapName\"" $menuFile > /tmp/scratch
					# Restore MenuAndToolbars file
                                        cp /tmp/scratch $menuFile > /dev/null 2>&1

					# Copy binary map files
                                        cp "/tmp/fxcMaps/$filename"* $1/FXC/fsl/data/depictables/backgrounds/$localization > /dev/null 2>&1
                                        echo "Restored $mapName background map"
                                fi
                        done
			rm -f /tmp/Maps.txt > /dev/null 2>&1
                        rm -fr /tmp/fxcMaps/ > /dev/null 2>&1
                else
                        echo "Local maps could not be restored; no map directory"
                fi

                # Restore user defined color tables
                if [ -d /tmp/color ]; then
                        cp /tmp/color/* $1/FXC/fsl/data/color/ > /dev/null 2>&1
                        cp /tmp/colorTables.state $1/FXC/fsl/data/state/ > /dev/null 2>&1
                        rm -fr /tmp/color > /dev/null 2>&1
                        rm -f /tmp/colorTables.state > /dev/null 2>&1
                        echo "Restored color tables defined by user"
		fi
	fi

	rm -f /tmp/scratch /tmp/scratch1 > /dev/null 2>&1
}

canMachineRunAWIPS()
{
	# This function checks for certain AWIPS code and
	# variables to determine if the igc code can be
	# run. The igc is required to create maps and
	# also to run an FXC server.
	#
	# Output parameter 1: "loadTcl" (GLOBAL)
	# Output parameter 2: "server" (GLOBAL)

	# Check if fxa home directory is defined
	if test -z $FXA_HOME; then
		fxaHome="/awips/fxa"
	else
		fxaHome=$FXA_HOME
	fi
	# Check if fxaWish interpreter is installed
	if [ -f $fxaHome/bin/fxaWish ]; then
		echo "D2D software found"
		if [ -f $fxaHome/bin/ldadWish ]; then
			echo "Proper tcl/tk environment exist"
			loadTcl=no
		else
			loadTcl=yes
		fi
		server=yes
	else
		echo "Could not find AWIPS (fxaWish) code" 
		printf "The FXC server cannot be installed. Continue [y/n]: "
		read more
			if [ $more = n ]; then
			echo "Terminating FXC installation"
			echo " "
			rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2
			exit 2
		fi
		server=no
 		loadTcl=no
	fi
}

computeRadarKeys()
{
	# This function computes the radar depict keys for a particular
	# radar. The new keys are stored in the Scale.txt file, if
	# no keys are found for that radar. Note: the scale numbers
	# may need to be changed for some localizations.
	#
	# Input parameter 1: radar id
	# Output parameters: new keys stored in Scale.txt 

	# Convert to lower case letters
	Radar=`echo $1 | tr "A-Z" "a-z"`

	# Find new radar index in radarInfoMaster file
	grep "^$Radar " $homeDir/FXC/fsl/data/config/radarInfoMaster.txt > /tmp/scratch
	if [ $? -ne 0 ]; then
		echo "Radar $1 not found. Exiting ..."
		rm -f /tmp/scratch
		exit 14
	else
		# Eliminate multiple occurrences
		sed -n 1,1p /tmp/scratch > /tmp/scratch1
	fi

	indx=`awk '{print $4}' /tmp/scratch1`

	# Convert new radar id to upper case letters
	RADAR=`echo $Radar | tr "a-z" "A-Z"`

	# Note: lab%, genericKey, label, and LUT defined
	# at beginning of this script

	# Check if radar keys are already in Scale.txt file
	grep $RADAR  $homeDir/FXC/fsl/data/keys/Scale.txt > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		# Compute actual keys for each radar product and append to Scale.txt
		i=0
		while [ "$i" -lt "${#label[@]}" ]
		do
			let "radarKey=$base + $indx * 65536 + ${genericKey[$i]}"
			echo "$radarKey  4-8,12-14  n  -  P  RUNZ  IFF  T,${LUT[$i]}  \"${label[$i]} $RADAR\"" >> $homeDir/FXC/fsl/data/keys/Scale.txt
			let "i = $i + 1"
		done
	fi
}

#----------------------Main Program-----------------------

# Check if user is in fxalpha group
uid=`id|awk --field-separator="(" '{print $4}'|tr -d ')'`
if [ ! $uid = fxalpha ]; then
	echo "Installer must be in fxalpha group, you are in $uid group"
	printf "Do you want to continue anyways [y/n]: n\rDo you want to continue anyways [y/n]: "
	read yn
	clear
	if test -z $yn; then
		exit 413
	elif [ $yn = n ]; then
		exit 414
	fi
fi

# Present script options to user

echo " "
echo " +--------------------------------------+"
echo " | Client and Server Code Installation  |"
echo " |--------------------------------------|"
echo " |                                      |"
echo " |  1 - Install FX-Collaborate          |"
echo " |  2 - Install FX-Chat                 |"
echo " |  3 - Patch Software                  |"
echo " |                                      |"
echo " |  0 - Manage System Configuration     |"
echo " |                                      |"
echo " +--------------------------------------+"

if [ -d $homeDir/FXC ]; then
 	opt=0
else
	opt=1
fi 
printf " Select desired option [0-3]: $opt\r Select desired option [0-3]: "
read choice
echo " "
if test -z $choice; then
	choice=$opt
fi

if test -z $homeDir; then
	printf "Specify the FXC installation directory: "
	read homeDir
	if [ -d $homeDir/FXC/fsl/linux ]; then
		echo $homeDir > $homeDir/FXC/fsl/linux/fxcHome
	fi
	askDir="yes"
else
	askDir="no"
fi

mapError=false

if [ $choice = 1 -o $choice = 2 ]; then
	
	# FX-CHAT OR FX-COLLABORATE INSTALLATION

	# Always request installation directory
	if [ $askDir = "no" ]; then
		printf "Specify the installation directory: "
		read homeDir
	fi

	# Create home directory, if necessary
	if [ -d $homeDir ]; then
		foundHome=true
	else
		echo " "
		mkdir $homeDir
		if [ $? -ne 0 ]; then
			exit 12
		fi
		chmod 775 $homeDir
	fi

	# Change to directory containing Java links (linux).
	# User may have installed JDK on previous pass.
	if [ -d $homeDir/FXC/fsl/linux ]; then
		export PATH=$homeDir/FXC/fsl/linux:$PATH
		cd $homeDir/FXC/fsl/linux
		FXCinstalled=true
	else
		cd $homeDir
		FXCinstalled=false
	fi

	# Preset Java install flag
	# (Do not install Java yet)
	loadJava=n

	# Determine current version of Java
	java -version > /tmp/scratch2 2>&1
	grep $javaVersion /tmp/scratch2 > /tmp/scratch
	if [ $? -ne 0 ]; then
		echo "Java version $javaVersion or later is needed to run FXC"
		printf "Do you want to install version $javaVersion now [y/n]: "
		read loadJava
		if [ $loadJava = y ]; then
			echo "This will not replace the current version of Java. Other"
			echo "Java applications will continue to use the old version"
			echo " "
			printf "Specify the Java installation directory: "
			read javaDir
		else
			echo " "
			echo "FXC will not run with older versions of Java"
			printf "Do you want to continue anyways [y/n]: "
			read install
			if [ $install = n ]; then
				echo "Terminating FXC installation"
				echo " "
				rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2
				exit 1
			fi 
		fi
	else
		echo "Java version $javaVersion found"
	fi
	rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2

	saved=no
	loadTcl=no
	server=no
	localization=FSL

	# Check if server code can be run on this machine
	if [ $choice = 1 ]; then 
		# Check if D2D code has been loaded on this machine
		canMachineRunAWIPS
		getCurrentLocalization
		echo "Localization is $localization"
	fi

	# Define local system environment

	export CLASSPATH=.:$homeDir/FXC
	export FXC_HOME=$homeDir/FXC
	export PATH=$homeDir/FXC/fsl/linux:$homeDir/FXC/fsl/depict:$homeDir/FXC/fsl/fac:$PATH

	if [ $FXCinstalled = true ]; then
		echo " "
		echo "FXC is already installed."
		printf "Do you want to overwrite [y/n]? "
		read installCode
	else
		# New FXC installation
		installCode=y
	fi

	javaLink="no"
	rmiLink="no"

	# Load java from CDROM or local disk
	askJava=yes
	if [ $loadJava = y ]; then 
		printf "\nDo you want to install Java from CD [y/n]? " 
		read cdrom
		askJava=no

		if [ $cdrom = y ]; then
			# Load Java file from CD
			cd /mnt/cdrom
			echo "Loading Java ..."
		else
			# Load Java file from local disk
			cd $homeDir
		fi

		# Check for multiple sdk files
		num=`ls -l $homeDir |grep $javaVersionString|wc -l` 
		if [ $num -ne 1 ]; then
			echo "Error: More than one Java $javaVersionString file"
			exit 713
		fi
 
		# Searching for proper version string 
		if [ -f *"$javaVersionString"*bin ]; then
			sdkFile=`ls -c1 *"$javaVersionString"*bin`
			echo "Installing $sdkFile"
			if [ -d $javaDir ]; then
				x="Java directory exists"
			else
				mkdir $javaDir > /dev/null 2>&1
				if [ $? -ne 0 ]; then
					echo "Unable to create java directory. Exiting..."
					exit 711
				fi
			fi
			cp $sdkFile $javaDir/ > /dev/null 2>&1
			# Run Java installation executable
			cd $javaDir
			sh $javaDir/$sdkFile
		else
			echo "Java $javaVersionString not found on local hard drive. Exiting..."
			exit 753
		fi 
	fi

	# Load the FXC code from CDROM or local disk
	if [ $installCode = y ]; then
		# Load FXC file from CD or local disk
		# (Minimizing user prompts with askJava)
		if [ $askJava = yes ]; then
			printf "\nDo you want to install from CD [y/n]? "
			read cdrom
		fi

		# Save old files and links to java, if appropriate
		if [ $FXCinstalled = true -a $loadJava = n ]; then
			if [ $choice = 1 ]; then
				saveAndRestore $homeDir save
			fi
			cd $homeDir/FXC/fsl/linux
			ls -l java > /dev/null 2>&1
			if [ $? -eq 0 ]; then
				ls -l java | awk '{print $NF}' | awk --field-separator=" > " '{print $1}' > /tmp/scratch
				javaLink=`more /tmp/scratch`
				loadJava=x
			fi

			ls -l rmiregistry > /dev/null 2>&1
			if [ $? -eq 0 ]; then
				ls -l rmiregistry | awk '{print $NF}' | awk --field-separator=" > " '{print $1}' > /tmp/scratch
				rmiLink=`more /tmp/scratch`
			fi
			rm -fr $homeDir/FXC
			saved=true
		fi

		if [ $cdrom = n ]; then
			# Installing FXC from local disk

			# Check if more than one FXC tar file exists.
			# If so, ask user which one to use.
			tar=`ls $homeDir/FXC*tar* | wc -l` > /dev/null 2>&1
			if [ $tar -eq 1 ]; then
				fxcFile=`ls -c1 $homeDir/FXC*tar*`
				echo "Loading $fxcFile from local disk"
				modFile=`echo $fxcFile | sed "s/FXC/$localization/"` 
			else
				ls -o -g $homeDir/FXC*tar*
				printf "Specify file name from above list: "
				read fxcFile
				# File name must contain FXC (e.g. FXC1_0_0.tar)
				# Also create a mod file name for the localization (e.g. TAE1_0_0.tar)
				echo $fxcFile > /tmp/scratch
				sed -e "s/FXC/$localization/" /tmp/scratch > /tmp/scratch2
				modFile=`more /tmp/scratch2`
			fi

			rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2
			echo " "

		elif [ $cdrom = y ]; then
			# Installing FXC from CD
			# Copy all COTS and applications from CD to local disk first
			cd /mnt/cdrom

			# Check if CD is inserted and mounted as /mnt/cdrom
			ls -c1 FXC*tar* > /dev/null 2>&1
			if [ -$? -ne 0 ]; then
				echo "/mnt/cdrom does not contain FXC tar file. Exiting ..."
				exit 754
			fi

			# Next, look for FXC tar file on CD
			echo "Loading fxc software ..."
			ls -c1 FXC*tar* > /tmp/scratch
			fxcFile=`more /tmp/scratch`

			# Check if a tar file by that name already exists on hard drive
			if [ -f $homeDir/$fxcFile ]; then
				chmod +w $homeDir/$fxcFile > /dev/null 2>&1
				if [ $? -eq 0 ]; then
					rm -f $homeDir/$fxcFile
				else
					echo "Unable to overwrite $homeDir/$fxcFile . Exiting..."
					echo " "
					exit 383
				fi
			fi
			# Copy FXC tar file from CD to local disk
			cp /mnt/cdrom/$fxcFile $homeDir/

			# Now, copy special tar file from CD
			# These could be maps, special depict keys, etc.
			sed -e "s/FXC/$localization/" /tmp/scratch > /tmp/scratch1
			modFile=`more /tmp/scratch1`
			if test -z $modFile; then
				x="No special files for this localization"
			else
				if [ -f /mnt/cdrom/$modFile ]; then
					cp /mnt/cdrom/$modFile $homeDir
				fi 
			fi

			# Finally, install ldadWish, if needed
			if [ $loadTcl = yes ]; then
				echo "Loading ldadWish ..."
				cp /mnt/cdrom/ldadWish $fxaHome/bin/
				echo "NOTE: Added ldadWish to $fxaHome/bin"
			fi
		else
			echo "Invalid option. Exiting ..."	
			exit 771
		fi

		# Untar FXC files
		# FXC code is in a file named FXC*tar* (e.g. FXC1_0_0.tar.gz)
		# (Only one FXC file may be on the CD)

		cd $homeDir
		echo $fxcFile > /tmp/scratch
		grep gz /tmp/scratch > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo "Unzipping tar file $fxcFile"
			gunzip $fxcFile
		fi
		# Strip gz extension and untar
		sed -e 's/.gz//' /tmp/scratch > /tmp/scratch2
		fxcFile=`more /tmp/scratch2`
		tar -xvf $fxcFile
		if [ $? -ne 0 ]; then
			echo "Unable to untar $fxcFile. Exiting ..."
			rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2
			exit 3	
		fi

		# FXC special files must be in a file [WFO]*tar* (e.g. SLC1_0_0.tar.gz)
		echo "Checking for special files"
		echo $modFile > /tmp/scratch
		grep gz /tmp/scratch > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			if [ -f $modFile ]; then
				echo "Unzipping gz file $modFile"
				gunzip $modFile
			fi
		fi
		# Strip gz extension and untar
		sed -e 's/.gz//' /tmp/scratch > /tmp/scratch2
		modFile=`more /tmp/scratch2`
		if [ -f $modFile ]; then
			tar -xvf $modFile
			if [ $? -eq 0 ]; then
				echo "Untaring tar file $modFile"
			else
				echo "Unable to untar $modFile. Exiting ..."
				rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2
				exit 3	
			fi
		fi
		
		echo " "
		echo "FXC application code read and untared"
	else
		# Old FXC and Java directories are unchanged
		echo "Previously installed FXC code is being used"
	fi

	# FXC is already installed; no need to load FXC again
	echo "Creating proper environment..."

	# Create a GeneralConfig.txt file, if necessary
	# FXC may not have a GeneralConfig.txt file if checked out from CVS
	if [ -f $homeDir$configFile ]; then
		x="GeneralConfig.txt exists"
	else
		cp $homeDir$configFile".template" \
			$homeDir$configFile
	fi

	# Update default localization in fxcenvirons.csh, if necessary
	# For use by FXC client before connecting to a server (uses FSL for FX-Chat).
	grep "setenv FXA_LOCAL_SITE" $homeDir/FXC/fsl/linux/fxcenvirons.csh > /tmp/scratch1
	if [ $? -eq 0 ]; then
		# Update localization in fxcenvirons.csh
		oldLine=`more /tmp/scratch1`
		sed -e "s/$oldLine/     setenv FXA_LOCAL_SITE $localization/" $homeDir/FXC/fsl/linux/fxcenvirons.csh > /tmp/scratch1
		mv /tmp/scratch1 $homeDir/FXC/fsl/linux/fxcenvirons.csh
	else
		echo "Script error in fxcenvirons.csh - FXA_LOCAL_SITE not defined"	
	fi 

	# Now, create a Java link in FXC/fsl/linux directory
	# (directory already in PATH since it is needed for scripts)
	cd $homeDir/FXC/fsl/linux
	if [ $loadJava = y ]; then
		# Note: downloaded version of java is j2re; CD may contain j2sdk
		if [ -d $javaDir/j2re$javaVersion ]; then
			prefix="j2re"
		else
			prefix="j2sdk"
		fi
		ln -s $javaDir/$prefix$javaVersion/bin/java java
		ln -s $javaDir/$prefix$javaVersion/bin/rmiregistry rmiregistry 
	elif [ $loadJava = x ]; then
		if [ !$rmiLink = no ]; then
			ln -s $rmiLink rmiregistry
		fi
		if [ !$javaLink = no ]; then 
			ln -s $javaLink java
			echo "Restored link to Java"
		else
			echo "No link to Java established"
		fi
	fi

	# Also, define fxcHome in FXC tree
	echo $homeDir > $homeDir/FXC/fsl/linux/fxcHome 

	# Update server in Hosts.txt file to include WFO name of this machine
	cd $homeDir/FXC/fsl/linux
	if [ $server = yes ]; then
		# Convert 3-letter WFO ID to name (e.g. SLC -> Salt Lake City)
		grep "^$localization " $homeDir/FXC/fsl/data/config/WFOids.txt > /tmp/scratch1
		siteName=`awk --field-separator=" - " '{print $4}' /tmp/scratch1 | awk --field-separator="/" '{print $1}'`

		#sed -e 's/^.//' /tmp/scratch > /tmp/scratch2
		#sed -e 's/.$//' /tmp/scratch2 > /tmp/scratch

	    # Check if site is already in Hosts.txt file
		grep "$siteName" $homeDir/FXC/fsl/data/config/Hosts.txt > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			# Insert server before "No Server" in Hosts.txt file
			sed -e '/No Server/i\'  $homeDir/FXC/fsl/data/config/Hosts.txt -e "\"$siteName\"        \"\"" > /tmp/scratch2

			# Set ip address to null (i.e. local host)
			cp /tmp/scratch2 $homeDir/FXC/fsl/data/config/Hosts.txt
			echo "New server $siteName added to Hosts.txt file"
		else
			echo "Server $siteName found in Hosts.txt file"
		fi	
	fi

	# FX-COLLABORATE ONLY - CHECK LOCALIZATION, MENU, AND MAPS 
	if [ $choice = 1 ]; then

		# Determine menu from WFOids.txt file
		getCurrentMenuName $localization fromConfigFile
		echo "Default menu set to $menuName type"

		# Check if java installation was successfull
		java -version > /tmp/scratch2 2>&1
		grep $javaVersion /tmp/scratch2 > /tmp/scratch
		if [ $? -ne 0 ]; then
			echo "Unable to complete installation. Java $javaVersion not found."
			exit 377
		fi

		# Create the appropriate menu
		chmod 755 $homeDir/FXC/fsl/support/*
		java fsl.support.MenuMaker $localization u > /dev/null 2>&1
		dos2unix -q $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt
		echo "Created $menuName menu type"

		# Convert server ID to server name by looking in WFOids.txt file
		# Extract line where localization is in first column
		# (Find server name by using "-" field separator)
		grep "^$localization " $homeDir/FXC/fsl/data/config/WFOids.txt > /tmp/scratch

		serverName=`more /tmp/scratch | awk --field-separator=" - " '{print $4}' | awk --field-separator="/" '{print $1}'`

		if test -z "$serverName"; then
			echo "Server ID is not found in reference file"
			echo " "
			rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2
			echo "$localization server was not added to menu"
		fi
	
		# Check if site is already in menu file
		menuFile="$homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt"

		if [ -f $menuFile ]; then 
			grep "CHOICEITEM \"$serverName\"" $menuFile > /dev/null 2>&1
			if [ $? -ne 0 ];then 
				# Add to CHOICEITEM menu (insert)
				sed -e '/No Server\" true/i\' -e "     CHOICEITEM \"$serverName\" X \"\" server \"$serverName\"" $menuFile > /tmp/scratch 
				# Add to COMBOBOX menu
				sed -e '/No Server\" #/i\' -e '     \"xzx\" \\' /tmp/scratch > /tmp/scratch2
				sed -e "s/xzx/$serverName/" /tmp/scratch2 > /tmp/scratch 
				cp /tmp/scratch $menuFile
				echo "New server $serverName added to menu"
			else
				echo "Server already in menu"
			fi
		fi

		# Create radar depict keys in Scale.txt if they don't exist
		# Find new radar in radarInfoMaster file
		grep "$localization" $homeDir/FXC/fsl/data/config/radarInfoMaster.txt > /tmp/scratch
		if [ $? -ne 0 ]; then
			echo "Radar information not found. Exiting"
			rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2
			exit 14
		else
			# Eliminate multiple occurrences
			sed -n 1,1p /tmp/scratch > /tmp/scratch1
		fi
		
		newRadar=`more /tmp/scratch1 | awk '{print $1}'`

		# Convert new radar id to upper case letters
		newRADAR=`echo $newRadar | tr "a-z" "A-Z"` 

		computeRadarKeys $newRadar
	
		echo "New radar is $newRADAR"

		# Create localization data and map backgrounds
		if [ $server = yes ]; then

			# Check if localization exists; if not, create FXC localization
			grep "^$localization " $homeDir/FXC/fsl/data/config/Localizations.txt > /dev/null 2>&1
			if [ $? -eq 0 ]; then
				echo "FXC localization for $localization exists"
			else 
				if [ -d $FXA_HOME/data/localizationDataSets/$localization ]; then
					$homeDir/FXC/fsl/support/makeLocalization $localization
					echo "Created $localization localization coordinates"
				else
					echo "AWIPS localization information for $FXC_HOME can not be found"
					echo " "
					rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2 > /dev/null 2>&1
					exit 35
				fi
			fi

			# Create map backgrounds
			mapError=false 
			if [ $localization = FSL ]; then
				x="Not allowed to modify FSL maps; under software CM"
			elif [ -d $homeDir/FXC/fsl/data/depictables/backgrounds/$localization ]; then 
				# Map directory already exists for this localization
				echo "Map backgrounds already exist for this localization"
				printf "Do you want to overwrite existing map files [y/n]: "
				read mapGen
				if [ $mapGen = y ]; then
					# Create map backgrounds
					rm -fr $homeDir/FXC/fsl/data/depictables/backgrounds/$localization
					java fsl.support.CreateLocalMaps $localization > /dev/null 2>&1
				else
					echo "No maps were generated"
				fi
			else
				# No map directory was found for this localization
				printf "Creating basic map backgrounds for $localization site. Continue [y/n]: "
				read mapGen
				if [ $mapGen = y ]; then
					# Create map backgrounds
					echo $FXA_DATA > /tmp/scratch
					if [ $? -ne 0 ]; then
						echo "FXA_DATA not defined. Terminating installation."
						exit 4
					fi
					java fsl.support.CreateLocalMaps $localization > /dev/null 2>&1 
				fi 
			fi
	
		else
			# This site does not have the AWIPS software to be a server
			echo "Map backgrounds for $localization could not be generated"
			echo "Contact $localization site to obtain maps"
			echo " "
		fi

		# Check if the map directory exist and if it contains any files
		if [ -d $homeDir/FXC/fsl/data/depictables/backgrounds/$localization ]; then
			numFiles=`ls -l $homeDir/FXC/fsl/data/depictables/backgrounds/$localization | wc -l`
			if [ $numFiles -eq 1 ]; then
				echo " "
				echo ">> Map backgrounds for $localization not generated <<"
				mapError=true
			fi
		else
			echo " "
			echo ">> Map backgrounds for $localization not generated <<"
			mkdir $homeDir/FXC/fsl/data/depictables/backgrounds/$localization
			mapError=true
		fi

		# Restore old files, if appropriate
		if [ -d /tmp/fxcMaps -o $saved = true ]; then
			saveAndRestore $homeDir restore
		fi

		# Clean up map directories for NWS users
		if [ $menuName = NWS ]; then
			if [ -f $homeDir/FXC/fsl/support/RemoveMaps ]; then
				cd $homeDir/FXC/fsl/data/depictables/backgrounds/$localization
				$homeDir/FXC/fsl/support/RemoveMaps
			fi
		fi

		# Start purger
		chmod -R 775 $homeDir/FXC > /dev/null 2>&1
		echo "You will need to purge files in /tmp if you are running a server"
		printf "Do you want to start the purger (crontab) [y/n]: y\rDo you want to start the purger (crontab) [y/n]: "
		read purge

		if test -z $purge; then
			purge=y
		fi
		if [ $purge = y ]; then
			# Start purger immediately
			$homeDir/FXC/fsl/linux/purger $homeDir
			if [ $? -eq 0 ]; then
				echo "Starting purger (crontab)"
			else
				echo "Unable to start purger. See your system administrator for help!"
				echo "  "
			fi
		fi 
	
		# Swap model depict key files for OB5 and OB4
		grep ob5 $FXA_HOME/lib/tcl/tclFXA/version.tcl > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			cd $homeDir/FXC/fsl/data/keys
			cp GFS90_OB5.txt GFS90.txt
			cd $homeDir/FXC/fsl/data/config
                	cp Sources_OB5.menu Sources.menu
		fi	
		grep ob4 $FXA_HOME/lib/tcl/tclFXA/version.tcl > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			cd $homeDir/FXC/fsl/data/keys
			cp GFS90_OB5.txt GFS90.txt
			cd $homeDir/FXC/fsl/data/config
               		 cp Sources_OB5.menu Sources.menu
		fi

		# Change area-of-responsibility to this WFO's (CONOPS)
		grep $localization $homeDir/FXC/fsl/data/config/WFODomains.txt > /dev/null 2>&1
		if [ $? -eq 0 ]; then
  			grep $localization $homeDir/FXC/fsl/data/config/WFODomains.txt > /tmp/fxc_tmp
  			cwa=`awk --field-separator="#" '{print $2}' /tmp/fxc_tmp`
  			rm -f /tmp/fxc_tmp
  			cd $homeDir/FXC/fsl/data/state/
			if [ -f $cwa"_AreasOfResponsibility.fxd" ]; then
  				cp $cwa"_AreasOfResponsibility.fxd" AreasOfResponsibility.fxd
  				echo "Changed to $cwa area of responsibility ($localization)"
			else
				echo "No area-of-responsibility polygons file exist"
			fi
		fi

        fi
            

	# Clean up /tmp directory
	rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2 > /dev/null 2>&1

	echo  "Installation complete"
	echo " "
	echo "+-----------------------------------------------+"
	echo "| Use Option 0 to customize your configuration  |"
	echo "+-----------------------------------------------+"

elif [ $choice = 3 ]; then

	# PROVIDE USER THE OPPORTUNITY TO INSTALL A PATCH

	# Look for patches on CD or in home directory
	patchLoc="none"
	mount /mnt/cdrom > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "No CD mounted"
		ls $homeDir/*PATCH* > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			patchLoc=$homeDir
		fi
	else
		ls /mnt/cdrom/*PATCH* > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			patchLoc="/mnt/cdrom"
		fi
	fi

	if [ $patchLoc = none ]; then
		echo "No patch file found on CD or local directory"
	else
		cd $homeDir
		echo "Found these patches in $patchLoc:"
		ls -1c $patchLoc/*PATCH*
		printf "\nSpecify patch file (or c/r): "
		read patch
		# Check if file is zipped (gz)
		echo $patch > /tmp/scratch
		grep gz /tmp/scratch > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			gunzip $patch
			# Drop gz from file name variable
			sed -e 's/.gz//' /tmp/scratch > /tmp/scratch2
			patch=`cat /tmp/scratch2`
		fi
		# Untar patch file
		if test -z $patch; then
			x="Ignore patch"
		else
			tar -xvf $patch 
		fi
	fi

elif [ $choice = 0 ]; then
  while [ loop = loop ]
  do 
	clear
	echo " "
	echo " +-----------------------------------------+"
	echo " | +-------------------------------------------+"
	echo " | |         FXC System Management             |"
	echo " | |-------------------------------------------|"
	echo " | |                                           |"
	echo " | |  0 - View System Documentation            |"
 	echo " | |  1 - Add/delete/modify a server (on menu) |"
 	echo " | |  2 - Restore/use a different main menu    |"
 	echo " | |  3 - Replace/add/delete radar (on menu)   |"
 	echo " | |  4 - Import/export site localization data |"
 	echo " | |  5 - Modify server access control list    |"
 	echo " | |  6 - Enable/disable auto-connect          |"
	echo " | |  7 - Set default scale (at startup)       |"
	echo " | |  8 - Create map from D2D userFile1.shp    |"
	echo " | |  9 - Create map from FXC graphic          |"
	echo " | |                                           |"
 	echo " |_|  s - Start client and/or server           |"
	echo "   |                                           |"
	echo "   +-------------------------------------------+"
 	printf "   Select desired option [0-9]: 0\r   Select desired option [0-9]: "
 	read option
 	echo " "
	if test -z $option; then
		option=0
	fi

  if [ $option = s ]; then
	# Start client and/or server
	clear
	cd $homeDir/FXC/fsl/linux

	echo " "
	echo "+--------------------------------------------------------+"
	echo "| +----------------------------------------------------------+"
	echo "| | +------------------------------------------------------------+"
	echo "| | |                       STARTUP MENU                         |"
	echo "| | |------------------------------------------------------------|"
	echo "| | |                                                            |"
	echo "| | |          The FXC client icon is located in:                |"
	echo "| | |  $homeDir/FXC/fsl/data/config/buttons/FXCicon.png   |"
	echo "| | |                                                            |"
	echo "| | | 1 - FX-Chat (only)         FXC/fsl/linux/run-FXChat        |"
	echo "| | | 2 - FX-Collaborate         FXC/fsl/linux/run-FXCollaborate |"
	echo "|_| | 3 - FXC server             FXC/fsl/linux/run-server        |"
	echo "  |_| 4 - FXC server and client  FXC/fsl/linux/BriefingTool      |"
	echo "    |                                                            |"
	echo "    +------------------------------------------------------------+"

	printf "    Select service to be started [1-4]: 2\r    Select service to be started [1-4]: "
	read fxc
	if test -z $fxc; then
		fxc=2
	fi
	if [ $fxc = 1 ]; then
		if [ -f run-FXChat ]; then
			./run-FXChat
		else
			echo "FX-Chat not available"
		fi 
	elif [ $fxc = 2 ]; then
		./run-FXCollaborate
	elif [ $fxc = 3 ]; then
		./run-server
	elif [ $fxc = 4 ]; then
		./BriefingTool
	else
		echo "Unrecognized option"
	fi	

  elif [ $option = 0 ]; then
	# Print system documentation
	clear
	echo " "
	echo "+------------------------------------+"
	echo "| +-------------------------------------+"
	echo "| | +--------------------------------------+"
	echo "| | |         DOCUMENTATION MENU           |"
	echo "| | |--------------------------------------|"
	echo "| | |                                      |"
	echo "| | |  1 - FXC Overview                    |"
	echo "|_| |  2 - FXC Installation Instructions   |"
	echo "  |_|  3 - FXC User Guide                  |"
	echo "    |                                      |"

	if test ! -f $homeDir/FXC/info/FXC_UG_Hist.html ; then
		echo "    |  Requires Web access ...             |"
		echo "    |                                      |"
	fi 
	echo "    +--------------------------------------+"

	printf "    Select desired document [1-3]: 1\r    Select desired document [1-3]: "
	read document
	echo " "
	if test -z $document; then
                document=1
	fi

	browser=unavailable

	if [ -d $homeDir/FXC/info ]; then
		overview=$homeDir/FXC/info/FXCoverview.html
		installation=$homeDir/FXC/info/FXCinstall.html
		userguide=$homeDir/FXC/info/FXC_User_Guide.html
	else
		overview=http://www-sdd.fsl.noaa.gov/~fxa/FXC/overview.html
		installation=http://www-sdd.fsl.noaa.gov/~fxa/FXC/fxcdoc/FXCinstall.html
		userguide=http://www-sdd.fsl.noaa.gov/~fxa/FXC/fxcug/FXC_User_Guide.html
	fi
		
	which netscape > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		browser=available
		if [ $document = 1 ]; then
			netscape $overview
		elif [ $document = 2 ]; then
			netscape $installation
		elif [ $document = 3 ]; then
			netscape $userguide
		fi
	else 
		which firefox > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			browser=available
                	if [ $document = 1 ]; then
				firefox $overview
			elif [ $document = 2 ]; then
				firefox $installation
			elif [ $document = 3 ]; then
				firefox $userguide
			fi
		fi
	fi
	
	if [ $browser = unavailable ]; then
		echo "Documentation requires Netscape browser"
		echo "(HTML files are located in FXC/fsl/info)"
	fi

  elif [ $option = 1 ]; then
	# Activate a server (add, delete, or modify)

	cd $homeDir/FXC/fsl/linux
	printf "\nAdd, delete, modify a server [a/d/m]: a\rAdd, delete, modify a server [a/d/m]: "
	read action

	if test -z $action; then
		action=a
	fi
	if [ $action = a ]; then
		# Adding a server requires that the localization is available
		echo "Available D2D localizations:"
		hostList=`ls -c1 $FXA_HOME/data/localizationDataSets/`
		echo $hostList > /tmp/scratch1
		for host in $hostList; do
			theHost=`echo $host|tr "_" " "`
			awk --field-separator="-" "/$theHost/"'{print $1,$4}' $homeDir/FXC/fsl/data/config/WFOids.txt
		done 
		# From FXC backgrounds, list the available servers and their IDs
		# (It is assumed that the FXC localization was imported)
		echo " "
		echo "Available FXC localizations:"
		hostList=`ls -c1 $homeDir/FXC/fsl/data/depictables/backgrounds/`
		echo $hostList >> /tmp/scratch1
		for host in $hostList; do
			theHost=`echo $host|tr "_" " "`
			awk --field-separator="-" "/$theHost/"'{print $1,$4}' $homeDir/FXC/fsl/data/config/WFOids.txt
		done 
		echo " "
		echo "If the server is not in the above list,"
		echo "import the localization first (option 4)"

	elif [ $action = d ]; then
		# From Hosts.txt, list the available servers and their IDs
		grep "^\"" $homeDir/FXC/fsl/data/config/Hosts.txt > /tmp/scratch
		hostList=`awk --field-separator="\"" '{print $2}' /tmp/scratch|tr " " "_"`
		for host in $hostList; do
			theHost=`echo $host|tr "_" " "`
			awk --field-separator="-" "/$theHost/"'{print $1,$4}' $homeDir/FXC/fsl/data/config/WFOids.txt
		done
	elif [ $action = m ]; then
		echo "Note: Can only be used to change server ip information"

	else
		echo "Invalid option"
		exit 33
	fi

	getCurrentLocalization
	printf "\nEnter server name [3-letter ID]: $localization\rEnter server name [3-letter ID]: "
	read newServer

	if test -z "$newServer"; then
		newServer=$localization
	fi

	# Convert id to upper case letters
	newServer=`echo $newServer | tr "a-z" "A-Z"`

	# Convert server ID to server name by looking in WFOids.txt file
	# Extract line where localization is in first column
	# (by searching for localization followed by "-")

	grep "^$newServer " $homeDir/FXC/fsl/data/config/WFOids.txt > /tmp/scratch
	serverName=`awk --field-separator=" - " '{print $4}' /tmp/scratch | awk --field-separator="/" '{print $1}'`

	# sed -e 's/^.//' /tmp/scratch 
	# sed -e 's/.$//' /tmp/scratch

	# Check if the server ID was found in WFOids.txt
	if test -z "$serverName"; then
		echo "$newServer is not a recognized identifier"
		echo " "
		rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2
		exit 10
	fi 

	# Determine the active menu in order to modify
	# server entries at the appropriate locations.
	# (Menu is specified in GeneralConfig.txt)

	getCurrentLocalization 
	getCurrentMenuName $localization fromConfigFile 
	menuFile=$homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt
	if [ -f $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt ]; then 
		dos2unix -q $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt 
	else
		echo "Menu file for $menuName does not exist"
		action=x
	fi

	# Add or delete the specified server
	if [ $action = a ]; then

		# Check if maps can be generated or already exist
		grep $newServer $homeDir/FXC/fsl/data/config/Localizations.txt > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "Localization for $serverName does not exist"
			exit 11
		fi

		# Note that "a" can also be used to update only the ip address
		printf "Specify ip address (C/R for this machine): "
		read address
		echo " "

		# Accept a null ip address
		testAddr=`echo $address | tr " " "_"`
		if test -z $testAddr; then
			address=\"\"
		fi

		# Define local system environment
		export CLASSPATH=.:$homeDir/FXC
		export FXC_HOME=$homeDir/FXC
		export PATH=$homeDir/FXC/fsl/linux:$homeDir/FXC/fsl/depict:$homeDir/FXC/fsl/fac:$PATH
		export FXA_LOCAL_SITE=$newServer

		# Add server to menu file

		# Check if site is already in menu file
		if [ -f $menuFile ]; then 
			grep "CHOICEITEM \"$serverName\"" $menuFile > /dev/null 2>&1
			if [ $? -ne 0 ];then
				# Add to CHOICEITEM menu (insert)
				sed -e '/No Server\" true/i\' -e "     CHOICEITEM \"$serverName\" X \"\" server \"$serverName\"" $menuFile > /tmp/scratch
				# Add to COMBOBOX menu
				sed -e '/No Server\" #/i\' -e '     \"xzx\" \\' /tmp/scratch > /tmp/scratch2
				sed -e "s/xzx/$serverName/" /tmp/scratch2 > /tmp/scratch
				cp /tmp/scratch $menuFile
				echo "Updating $menuName menu file"
			else
				echo "Server already in menu"
			fi
		fi

		# Check if site is already in Hosts.txt file
		grep "$serverName" $homeDir/FXC/fsl/data/config/Hosts.txt > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			# Insert server before "No Server" in Hosts.txt file
			sed -e '/No Server/i\' -e "\"$serverName\"   $address" $homeDir/FXC/fsl/data/config/Hosts.txt > /tmp/scratch
			cp /tmp/scratch $homeDir/FXC/fsl/data/config/Hosts.txt
			echo "New server $serverName added to Hosts.txt file"
			
		else
			# Replacing server in Hosts.txt file"
			sed -e "/$serverName/d" $homeDir/FXC/fsl/data/config/Hosts.txt > /tmp/scratch
			sed -e '/No Server/i\' -e "\"$serverName\"      $address" /tmp/scratch > /tmp/scratch2
			cp /tmp/scratch2 $homeDir/FXC/fsl/data/config/Hosts.txt
			echo "Replacing server $serverName $address in Hosts.txt file"
		fi

		# Create map backgrounds
		# Check if this machine supports the desired localization
		ls -c1 $FXA_HOME/data/localizationDataSets > /tmp/scratch
		grep $newServer /tmp/scratch > /dev/null 2>&1
		if [ $? -eq 0 ]; then 
			# Localization for the desired office exists in D2D
			
			# Determine if localization for the office exists in FXC
			grep $newServer $homeDir/FXC/fsl/data/config/Localizations.txt > /tmp/scratch
			if [ $? -ne 0 ]; then
				# Localization does not exist, create FXC localization
				if [ -d $FXA_HOME/data/localizationDataSets/$newServer ]; then
					$homeDir/FXC/fsl/support/makeLocalization $newServer
					echo "Created $serverID localization coordinates"
				else
					echo "Cannot create FXC localization $serverID; no D2D localization"
					echo " "
					rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2 > /dev/null 2>&1
					exit 36
				fi
			fi

			if [ -d $homeDir/FXC/fsl/data/depictables/backgrounds/$newServer ]; then 
				printf "Do you want to overwrite existing map files [y/n]: "
				read answer 
				if [ $answer = y ]; then
					rm -fr $homeDir/FXC/fsl/data/depictables/backgrounds/$newServer
					java fsl.support.CreateLocalMaps $newServer > /dev/null 2>&1
				else	
					echo "Note: No new map backgrounds were created"
				fi
			else
				echo "Creating basic map backgrounds for $newServer site"
				java fsl.support.CreateLocalMaps $newServer > /dev/null 2>&1
			fi

			# Clean up map directories for NWS users
			if [ $menuName = "NWS" ]; then
				if [ -f $homeDir/FXC/fsl/support/RemoveMaps ]; then
					cd $homeDir/FXC/fsl/data/depictables/backgrounds/$newServer 
					$homeDir/FXC/fsl/support/RemoveMaps 
				fi
			fi 
		fi

		# Check if the map directory exist and if it contains any files
		if [ -d $homeDir/FXC/fsl/data/depictables/backgrounds/$newServer ]; then
			numFiles=`ls -l $homeDir/FXC/fsl/data/depictables/backgrounds/$newServer | wc -l`
			if [ $numFiles -eq 1 ]; then
				echo ">> Map backgrounds for $newServer not generated <<"
				echo "You may need to import the maps from another site"
				mapError=true
			fi
		else
			echo ">> Map backgrounds for $newServer not generated <<"
			echo "You may need to import the maps from another site"
			mkdir $homeDir/FXC/fsl/data/depictables/backgrounds/$newServer
			mapError=true
		fi
		echo "Server changes completed"
		getCurrentLocalization  #reset localization

	elif [ $action = d ]; then
		# Delete coresponding menu items
		# (This could erase more than desired)

		if [ -f $menuFile ]; then
			sed -e "/CHOICEITEM $serverName/d" $menuFile > /tmp/scratch2
			sed -e "/\"$serverName\"/d" /tmp/scratch2 > /tmp/scratch
			cp /tmp/scratch $menuFile
			echo "Deleted server $serverName from $menuName menu"
		fi

		# Delete server from Hosts.txt file 
		sed -e "/$serverName/d" $homeDir/FXC/fsl/data/config/Hosts.txt > /tmp/scratch
		cp /tmp/scratch $homeDir/FXC/fsl/data/config/Hosts.txt
		echo "Deleted server $serverName from Hosts.txt file"

	elif [ $action = m ]; then
		# Change the ip address in Hosts.txt 
		echo "You may enter the ip addresses of the repeaters" and
		echo "the server; e.g. 123.44.55.66:1128   123.44.77.88"
		echo " "
		printf "Specify ip address (C/R for this machine): "
                read address
                echo " "
		# Accept a null ip address
		testAddr=`echo $address | tr " " "_"` 
		if test -z $testAddr; then
			address=\"\"
		fi
		# Check if site is already in Hosts.txt file
		grep "$serverName" $homeDir/FXC/fsl/data/config/Hosts.txt > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "New server $serverName does not exist in Hosts.txt file"
			
		else
			# Replacing server in Hosts.txt file"
			sed -e "/$serverName/d" $homeDir/FXC/fsl/data/config/Hosts.txt > /tmp/scratch
			sed -e '/No Server/i\' -e "\"$serverName\"      $address" /tmp/scratch > /tmp/scratch2
			cp /tmp/scratch2 $homeDir/FXC/fsl/data/config/Hosts.txt
			echo "Updating server $serverName with $address in Hosts.txt file"
		fi
		
	fi

#	rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2

  elif [ $option = 2 ]; then
	# Select different menu
	cd $homeDir/FXC/fsl/linux

	export CLASSPATH=.:$homeDir/FXC
	export FXC_HOME=$homeDir/FXC
	export PATH=$homeDir/FXC/fsl/linux:$homeDir/FXC/fsl/depict:$homeDir/FXC/fsl/fac:$PATH

	# Request site ID (will look up menu in WFOids.txt)
	getCurrentLocalization
	printf "\nEnter the site ID for the desired menu (3-letter ID): $localization\rEnter the site ID for the desired menu (3-letter ID): "
	read newLocalization

	if test -z $newLocalization; then
               newLocalization=$localization 
	fi

	# Convert to upper case
	newLocalization=`echo $newLocalization | tr "a-z" "A-Z"`

	# Determine current menu name
	getCurrentMenuName $newLocalization Default
	echo "Default menu set to $menuName type"

	# Determine radar for the new menu
	grep "$newLocalization" $homeDir/FXC/fsl/data/config/radarInfoMaster.txt > /tmp/scratch
	if [ $? -ne 0 ]; then
		echo "Radar information not found. Exiting"
		rm -f /tmp/scratch > /dev/null 2>&1
		exit 14
	else
		# Eliminate multiple occurrences
		sed -n 1,1p /tmp/scratch > /tmp/scratch1
	fi

	newRadar=`more /tmp/scratch1 | awk '{print $1}'`

	# Create the radar menu entries and keys
	computeRadarKeys $newRadar
	echo "New radar is $newRadar"

	# Create new menu and also update GeneralConfig.txt
	cd $homeDir/FXC/fsl/support
	java fsl.support.MenuMaker $newLocalization u > /dev/null 2>&1
	dos2unix -q $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt
	rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2

  elif [ $option = 3 ]; then
	# Replace the radar on the menu

	# Refer to radarInfoMaster.txt and radarDepictKeys.template
	# in /awips/fxa/data/localization/NationalData for
	# computation of radar keys.

	# Determine menu directory in order to modify
	# server in the appropriate menu.

	getCurrentLocalization
	getCurrentMenuName $localization fromConfigFile
	menuFile=$homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt
	dos2unix -q $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt

	# Determine if user wants to replace or add a radar
	printf "Replace, add or delete a radar [r/a/d]: a\rReplace, add or delete a radar [r/a/d]: "
	read action

	if test -z $action; then
		action=a
	fi
	valid=0

	# Determine current/primary radar in use from menu
	grep "primary radar is" $menuFile > /tmp/scratch
	sed -e 's/# The primary radar is //' /tmp/scratch > /tmp/scratch1
	#sed -e 's/.$//' /tmp/scratch1 > /tmp/scratch  # Strips last character or CR!!!!
	primaryRADAR=`more /tmp/scratch1 | tr "a-z" "A-Z"`
	# Convert to lower case letters
	primaryRadar=`echo $primaryRADAR | tr "A-Z" "a-z"`
	if test -z $primaryRadar; then
		echo "Unable to determine the primary radar from menu"
		echo "(The menu file may be corrupted)"
		echo " "
		action=x
		valid=1
	elif [ $primaryRadar = %%% ]; then
		echo "Unable to determine the primary radar from menu"
		echo "(The menu file may be corrupted)"
		echo " "
		action=x
		valid=1
	fi

	if [ $action = d -o $action = r ]; then

		printf "Specify radar to be replaced/deleted: $primaryRadar\rSpecify radar to be replaced/deleted: "
		read oldRadar

		if test -z $oldRadar; then
			oldRadar=$primaryRadar
		fi
		# Convert old radar id to upper and lower case letters 
		oldRadar=`echo $oldRadar | tr "A-Z" "a-z"`
		oldRADAR=`echo $oldRadar | tr "a-z" "A-Z"`

		# Remember the old radar for radar replacement (oldRadar -> %%%)
		myOldRadar=$oldRadar

		grep -i $oldRadar $menuFile > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "$oldRadar not found in the menu"
			action=x  # Exit from option
		elif [ $oldRADAR = $primaryRADAR ]; then
			if [ $action = d ]; then
				echo "Cannot delete your primary radar $oldRadar"
				action=x  # Exit from option
			elif [ $action = r ]; then
				# Delete specified radar from menu (only to replace radar)
				# Flag radar with %%% temporarily
	                        sed -e "s/MENU $oldRadar/MENU %%%/" $menuFile > /tmp/scratch
        	                sed -e "s/$oldRADAR/%%%/" /tmp/scratch > /tmp/scratch1
                	        mv /tmp/scratch1 $menuFile
                        	# Delete radar keys
				sed -e "/$oldRADAR/d" $homeDir/FXC/fsl/data/keys/Scale.txt > /tmp/scratch
				mv /tmp/scratch $homeDir/FXC/fsl/data/keys/Scale.txt
				oldRadar=%%%
				oldRADAR=%%%
			fi
		else
			# Delete specified radar from menu 
			sed -e "/$oldRadar/d" $menuFile > /tmp/scratch
			sed -e "/$oldRADAR/d" /tmp/scratch > /tmp/scratch1
			mv /tmp/scratch1 $menuFile
			# Delete radar keys
			sed -e "/$oldRADAR/d" $homeDir/FXC/fsl/data/keys/Scale.txt > /tmp/scratch
			mv /tmp/scratch $homeDir/FXC/fsl/data/keys/Scale.txt
			if [ $action = d ]; then
				echo "$oldRADAR has been deleted"
			fi
		fi
		valid=1
	fi

	if [ $action = a -o $action = r ]; then	
		# Replacing or adding a radar
		printf "Specify name of new radar (4-letter ID): $myOldRadar\rSpecify name of new radar (4-letter ID): "
		read newRadar
		newRadar=`echo $newRadar | tr "A-Z" "a-z"`

		if test -z $newRadar; then
			newRadar=$myOldRadar
		fi
		undo=false
		grep $newRadar $menuFile > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo "Cannot add $newRadar. Radar already on menu"
			newRadar=$myOldRadar
			undo=true
		fi
		grep "^$newRadar " $homeDir/FXC/fsl/data/config/radarInfoMaster.txt > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "Unknown radar"
			newRadar=$myOldRadar
			undo=true
		fi 
		newRADAR=`echo $newRadar | tr "a-z" "A-Z"`

		# Update menu file
		if [ $action = r ]; then
			# oldRadar is %%% if radar is being replaced
			# otherwise equals four letter identifier
			computeRadarKeys $newRadar
			sed -e "s/MENU $oldRadar/MENU $newRadar/" $menuFile > /tmp/scratch
			sed -e "s/$oldRADAR/$newRADAR/" /tmp/scratch > $menuFile
			if [ $myOldRadar = $primaryRadar ]; then
				sed "s/primary radar is $oldRadar/primary radar is $newRadar/"  $menuFile > /tmp/scratch
				mv /tmp/scratch $menuFile
				echo "Primary radar is now $newRadar" 
			else
				echo "$myOldRadar has been replaced by $newRadar"
			fi

		elif [ $action = a -a $undo = false ]; then
			computeRadarKeys $newRadar
			# Need to add all the radar products for new radar (no substitution)
			# Insert (patch) products from MasterMenu.txt
			grep "%%%" $homeDir/FXC/fsl/data/config/MasterMenu.txt > /tmp/scratch
			awk '{$1="";print}' /tmp/scratch > /tmp/scratch1
			sed -e '/The primary radar is/d' /tmp/scratch1 > /tmp/scratch2
			sed -e '/The primary radar is/{r /tmp/scratch2' -e ';}'  $menuFile > /tmp/scratch
			sed -e "s/MENU %%%/MENU $newRadar/" /tmp/scratch > /tmp/scratch1
			sed -e "s/%%%/$newRADAR/" /tmp/scratch1 > $menuFile
			echo "$newRadar has been added to the menu"
		fi
		rm /tmp/scratch /tmp/scratch1 /tmp/scratch2 > /dev/null 2>&1
		valid=1
	fi

	if [ $valid = 0 ]; then
		echo "Unrecognized option"
	fi

  elif [ $option = 4 ]; then
	# Extract localization information
	cd $homeDir/FXC/fsl/linux
	printf "Export or import a localization [e/i]: "
	read port

	if [ $port = e -o $port = i ]; then
		getCurrentLocalization
		printf "Specify desired WFO (3-letter ID): $localization\rSpecify desired WFO (3-letter ID): "
		read wfo
		echo " "

		if test -z $wfo; then
			wfo=$localization
		fi

		# Convert to upper case letters
		echo $wfo > /tmp/scratch	
		tr "a-z" "A-Z" < /tmp/scratch > /tmp/scratch1
		wfo=`more /tmp/scratch1` 
	fi

	if [ $port = e ]; then
		# export localization values to a tar file
		if [ -d $homeDir/FXC/fsl/data/depictables/backgrounds/$wfo ]; then
			cd $homeDir/FXC/fsl/data/depictables/backgrounds/$wfo
			# Clean up directory first by removing unwanted maps (dynamic list!!)
			echo "Deleting unnecessary maps"
			rm -f Airport* ARTCC* DFW* High_ARTCC* High_Sectors* IAH* Jet* Low_ARTCC* Sid.* Star.* Super_High* TWEB* USAF* Volcanoes* VOR* Launch*> /dev/null 2>&1
			cd $homeDir
			grep "^$wfo " FXC/fsl/data/config/Localizations.txt > localization$wfo
			tar -cvf maps$wfo.tar FXC/fsl/data/depictables/backgrounds/$wfo > /dev/null 2>&1
			tar -cvf fxcExport$wfo.tar localization$wfo maps$wfo.tar > /dev/null 2>&1
			echo "Stored localization and maps in $homeDir/fxcExport$wfo.tar"
			rm -f maps$wfo.tar localization$wfo > /dev/null 2>&1
		else
			echo "Files for $wfo not found in FXC localization files"
		fi
	elif [ $port = i ]; then
		# import localization values into Localizations.txt
		cd $homeDir
		if [ -f fxcExport$wfo.tar ]; then
			tar -xvf fxcExport$wfo.tar > /dev/null 2>&1
			# Delete localization info, if it already exists
			sed "/^$wfo/d" FXC/fsl/data/config/Localizations.txt > /dev/null 2>&1
			# Update Localizations.txt and add maps  
			if [ -f localization$wfo ]; then
				echo '#' >> FXC/fsl/data/config/Localizations.txt
				cat localization$wfo >> FXC/fsl/data/config/Localizations.txt
				tar -xvf maps$wfo.tar > /dev/null 2>&1
				echo "Imported localization data for site $wfo"
				rm -f maps$wfo.tar localization$wfo > /dev/null 2>&1
			else
				echo "File localization$wfo not found"
			fi
		else
			echo "File fxcExport$wfo.tar not found in $homeDir"
		fi
	else
		echo "Unrecognized option"
	fi
	rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2
	#export FXA_LOCAL_SITE=$localization	

  elif [ $option = 5 ]; then
	# Modify authorized list of users
	echo " USER           GROUP(S)"
	echo " -----------------------"
	operation=a
	if [ -f $homeDir/FXC/fsl/data/config/CollabGroups.txt ]; then
		cat $homeDir/FXC/fsl/data/config/CollabGroups.txt
	fi
	
	printf "\nAdd or delete user [a/d]: "
	read operation
	printf "\nSpecify User Group to be modified: "
	read group
	printf "\nSpecify User Name: "
	read user
	echo " "

	if test -z $operation; then
		operation=x  # Exit from option
	elif test -z $user; then
		operation=x  # Exit from option
	elif test -z $group; then
		operation=x  # Exit from option
	fi

	if [ $operation = a ]; then
		echo "$user         $group" >> $homeDir/FXC/fsl/data/config/CollabGroups.txt
		echo "Added $user to $group"
	elif [ $operation = d ]; then
		sed -e "/$user/d" $homeDir/FXC/fsl/data/config/CollabGroups.txt > /tmp/scratch
		cp /tmp/scratch $homeDir/FXC/fsl/data/config/CollabGroups.txt

		# Delete file if empty
		contents=`more $homeDir/FXC/fsl/data/config/CollabGroups.txt`
		if test -z "$contents"; then
			rm $homeDir/FXC/fsl/data/config/CollabGroups.txt
			rm -f /tmp/scratch
		fi
		echo "Deleted $user from $group"
	else
		echo "Invalid option or entry"
	fi
	rm -f /tmp/scratch

  elif [ $option = 6 ]; then
	# Enable or disable auto-connect to server
	# NOTE: Servers in Hosts.txt must be within quotes!

	# List the available servers and their IDs
	grep "^\"" $homeDir/FXC/fsl/data/config/Hosts.txt > /tmp/scratch
	hostList=`awk --field-separator="\"" '{print $2}' /tmp/scratch|tr " " "_"`
	for host in $hostList; do
		theHost=`echo $host|tr "_" " "`
		awk --field-separator="-" "/$theHost/"'{print $1,$4}' $homeDir/FXC/fsl/data/config/WFOids.txt
	done
	echo "NONE    (To disconnect)"

	getCurrentLocalization
	printf "\nSpecify auto-connect server (3-letter ID or NONE): $localization\rSpecify auto-connect server (3-letter ID or NONE): "
	read autoServer        

	if test -z $autoServer; then
		autoServer=$localization
	fi

	if [ $autoServer = "NONE" ]; then
		serverName="No Server"
	else
		# Find corresponding server city in WFOids.txt
		# Conver to upper case letters
		autoServer=`echo $autoServer | tr "a-z" "A-Z"`

		# Find the city that is associated with the server
		# extract line where localization is in first column
		# (find server name by using "-" field separators)
		# grep "$autoServer " $homeDir/FXC/fsl/data/config/WFOids.txt > /tmp/scratch
		grep "^$autoServer " $homeDir/FXC/fsl/data/config/WFOids.txt > /tmp/scratch
		if [ $? -ne 0 ]; then
			echo "$autoServer not a recognized server"
			autoServer=notfound
		else
			serverName=`awk --field-separator=" - " '{print $4}' /tmp/scratch | awk --field-separator="/" '{print $1}'`
		fi
		# Check if there is more than one match
		occur=`cat /tmp/scratch | wc -l`
		if [ $occur -gt 1 ]; then
			echo "Found multiple $autoServer; no action taken"
			autoServer=notfound
		fi

	fi

	if [ $autoServer != notfound ]; then
		# Determine currently used menu from GeneralConfig.txt 
		# in order to set the auto-connect to the desired server city

		getCurrentLocalization
		getCurrentMenuName $localization fromConfigFile	
		menuFile=$homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt
		dos2unix -q $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt

		if [ -f !$menuFile ]; then
			echo " "
			echo "$menuFile does not exist"
		else
			echo "Current menu is $menuName"
		fi

		# Determine if the server city is listed on the menu
		# (There can be many server cities on the menu)

		# Search for the city both, with and without quotes
		search="server $serverName"

		grep "$search" $menuFile > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			search="server $Q$serverName$Q"
			grep "$search" $menuFile > /dev/null 2>&1
			if [ $? -ne 0 ]; then	
				echo "$search is not found in the menu"
				autoServer=notfound
			fi
		fi
	fi

	# Check if the CHOICEITEM for the server is on two lines (join lines)
	# (Find line that starts with CHOICE..and ends with /; add new line; delete CR/LF)
	sed -e "/CHOICEITEM $Q$serverName"'.* \\$/N; s/\\\n//' $menuFile > /tmp/scratch
	mv /tmp/scratch $menuFile

	# Make the appropriate menu changes in two locations in the menu file
	if [ "$serverName" = "No Server" ]; then

		# Change menu to automatically select "No Server"
		sed -e 's/true \#//' $menuFile > /tmp/scratch
		sed -e "s/CHOICEITEM $QNo Server.*/CHOICEITEM $QNo Server$Q N $Q$Q server $Q$No Server$Q true/" /tmp/scratch > /tmp/scratch1
		# Also change combo box
		sed -e "s/COMBOBOX Server server.*/COMBOBOX Server server \"No Server$Q \\\ /" /tmp/scratch1 > /tmp/scratch
		cp /tmp/scratch $menuFile
	
		echo "Auto-connect has been disabled"

	elif [ $autoServer != notfound ]; then
		# Change menu file to automatically connect to server 
		sed -e "s/CHOICEITEM $Q$serverName.*/CHOICEITEM $Q$serverName$Q $Q$Q $Q$Q server $Q$serverName$Q true \#/" $menuFile > /tmp/scratch
		# Also change combo box 
		sed -e "s/COMBOBOX Server server.*/COMBOBOX Server server $Q$serverName$Q \\\ /" /tmp/scratch > /tmp/scratch1
		cp /tmp/scratch1 $menuFile

		echo "FXC will automatically connect to $serverName upon startup"
	fi

	rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2

  elif [ $option = 7 ]; then
	# Change default scale
	# Scales are numbered as they appear on the menu (0-n)
	awk '{print $1,$2}' $homeDir/$configFile > /tmp/scratch
	grep DEFAULTSCALE  /tmp/scratch > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "Unable to get default scale from configuration file"
		exit 81
	else
		awk '/DEFAULTSCALE/{print $2}' /tmp/scratch | tr -d '\r' > /tmp/scratch1
		scale=`cat /tmp/scratch1`
	fi

	getCurrentLocalization
	awk "/$localization/"'{print $2,$3}' $homeDir/FXC/fsl/data/config/Localizations.txt 

	printf "\nSpecify desired default scale number [0-n]: $scale\rSpecify desired default scale number [0-n]: "
	read default

	if test -z $default; then
		# no change
		echo "Default scale was not changed"
	else
		# Check if scale exists for the localization
		awk "/$localization/"'{print $2,$3}' $homeDir/FXC/fsl/data/config/Localizations.txt > /tmp/scratch1
		#num=`expr $default + 1`				# increment scale number > 0
		#sed -n "$num"p /tmp/scratch1 > /tmp/scratch2	# print line with textual scale (save scratch2) 
		# Show user textual name of scale (spaces around $default important)
		name=`awk "/ $default"'$/{print $1}' /tmp/scratch1` 		# extract the textual name	

		if test -z "$name"; then
			echo "Scale $default does not exist for $FXA_LOCAL_SITE localization"
		else
			# Replace default scale in file
			grep DEFAULTSCALE $homeDir/$configFile > /tmp/scratch1
			line=`cat /tmp/scratch1`
			sed -e "s/$line/DEFAULTSCALE      $default/" $homeDir/$configFile > /tmp/scratch
			mv /tmp/scratch $homeDir/$configFile 

			# awk '/MENUOPTION/{print $2}' $homeDir/$configFile | tr -d '\r' > /tmp/scratch
			# menu=`more /tmp/scratch`
			getCurrentMenuName $localization fromConfigFile

			# Temporary workaround to bring up county map for WFO scale
			if [ "$name" = WFO ]; then
				sed -e 's/map Counties\" false/map Counties\" true/' $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt > /tmp/scratch
				mv /tmp/scratch $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt
			else
				sed -e 's/map Counties\" true/map Counties\" false/' $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt > /tmp/scratch
				mv /tmp/scratch $homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt
			fi
			echo "Default scale changed to $name"
		fi
	fi 
	rm -f /tmp/scratch /tmp/scratch1 /tmp/scratch2 > /dev/null 2>&1

  elif [ $option = 8 -o $option = 9 ]; then

	# Create new map from an AWIPS shape file or DGM graphic
	getCurrentLocalization
	if test -z $homeDir; then
		printf "Specify the installation directory: "
		read homeDir
	fi

	# Determine desired action
	printf "\nDo you want to add or delete a map [a/d]: a\rDo you want to add or delete a map [a/d]: "
	read action
	if test -z $action; then
		action=a
	fi

	# Determine active menu
	getCurrentMenuName $localization fromConfigFile
	menuFile="$homeDir/FXC/fsl/data/config/$menuName/MenuAndToolBars.txt"

	# Change depict key based on type of map to be added (max one map)
	if [ $option = 8 ]; then
		mapKey=1311
	elif [ $option = 9 ]; then
		mapKey=1321
		if [ $action = a ]; then
			echo "Map will be generated from Graphic_A"
		fi
	fi

	if [ $action = a ]; then
		# Add a shapefile map (maximum of one)
		# Check if a shapefile has already been added
		grep $mapKey $homeDir/FXC/fsl/data/config/Maps.txt > /tmp/scratch
		if [ $? -eq 0 ]; then
			echo "A shape file has already been added. In order to add"
			echo "another shape file you must first replace the existing"
			echo "map key with a new key (i.e. available D2D depict key)"
			printf "Enter new new map key [C/R to exit]: "
			read newKey
			if test -z $newKey; then
				echo "Exiting ..."
				exit 872
			else

				grep $newKey $homeDir/FXC/fsl/data/config/Maps.txt > /dev/null 2>&1
				if [ $? -ne 0 ]; then 

					sed -e "s/$mapKey/$newKey/" $homeDir/FXC/fsl/data/config/Maps.txt > /tmp/scratch 
					cp /tmp/scratch  $homeDir/FXC/fsl/data/config/Maps.txt 
					grep $newKey  $homeDir/FXC/fsl/data/config/Maps.txt > /tmp/scratch
					oldMap=`awk --field-separator="\x22" '{print $2}' /tmp/scratch`
 					echo "$oldMap has been given the new key $newKey"
					rm -f /tmp/scratch
				else
					echo "Key already in use; exiting ..."
					exit 967
				fi
			fi
		fi

		# Get a name for the new map
		printf "\nSpecify menu name of the map: "
		read mapName
		echo ""

		# Check if a map by that name already exists
		grep "$mapName" $homeDir/FXC/fsl/data/config/Maps.txt > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			option=999
			mapFound=false
			echo "A map by that name already exists"
		fi

		# Check if shape or dgm files exist in D2D directory
		mapFound=false
		if [ $option = 8 ]; then
		     if [ -f $FXA_HOME/data/localizationDataSets/$localization/userFile1.shp ]; then
		          if [ -f $FXA_HOME/data/localizationDataSets/$localization/userFile1.shx ]; then
		               if [ -f $FXA_HOME/data/localizationDataSets/$localization/userFile1.dbf ]; then 
					mapFound=true
		               fi
			  fi
		      fi
		      if [ $mapFound = false ]; then
			  echo "$FXA_HOME/data/localizationDataSets/$localization/userFile1.shp, shx, dbf not found"
		      fi
		elif [ $option = 9 ]; then
		      if [ -f $homeDir/FXC/fsl/data/depictables/dgm/Graphic_A01.dgm ]; then
			  mapFound=true
		      fi
		      if [ $mapFound = false ]; then
			  echo "$homeDir/FXC/fsl/data/depictables/Graphic_A01.dgm not found"
			  echo "Drawing must be save as Graphic 1 from drawing tool"
		      fi
		fi

		if [ $mapFound = true ]; then
			# Get name and applicable scale info from user
			awk "/$localization/"'{print $2,$3}' $homeDir/FXC/fsl/data/config/Localizations.txt
			printf "\nSpecify geographic scale number: 2\rSpecify geographic scale number: "
			read scale
			if test -z "scale"; then
				scale=2
			fi

			printf "\nMake $mapName the default for scale [0-n]: na\rMake $mapName the default for scale [0-n]: "
			read default
			if test -z $default; then
				default="-" 
			fi
			mapOK=true

			# Convert menu name to file name
			echo $mapName > /tmp/scratch
			map_Name=`sed -e 's/ /_/' /tmp/scratch`

			# Use igc code to create a DGM file (AWIPS key=1311)
			cd $homeDir/FXC/fsl/support
			export PATH=$homeDir/FXC/fsl/linux:$PATH
			if [ $option = 8 ]; then
				scaleCheck=`echo $scale | awk --field-separator="," '{print $2}'` 
				if test -z $scaleCheck; then
					java fsl.support.CreateLocalMaps $localization $scale 1311 $map_Name  > /dev/null 2>&1
				else
					echo "Bad geographic scale number"
				fi
				if test ! -f $homeDir/FXC/fsl/data/depictables/backgrounds/$localization/$map_Name"."$scale; then
					echo ">> Map was not created <<"
					mapOK=false
				fi
			elif [ $option = 9 ]; then

				# Add map for maximum of four scales (arbitrary limit)
				cd $homeDir/FXC/fsl/data/depictables/
				scales=`echo $scale | awk --field-separator="," '{print $1}'` 
				cp dgm/Graphic_A01.dgm backgrounds/$localization/$map_Name"."$scales 
				scales=`echo $scale | awk --field-separator="," '{print $2}'` 
				if test -z $scales; then
					dummy=0
				else 
					cp dgm/Graphic_A01.dgm backgrounds/$localization/$map_Name"."$scales
				fi
				scales=`echo $scale | awk --field-separator="," '{print $3}'` 
				if test -z $scales; then 
					dummy=0
				else 
					cp dgm/Graphic_A01.dgm backgrounds/$localization/$map_Name"."$scales
				fi
				scales=`echo $scale | awk --field-separator="," '{print $4}'` 
				if test -z $scales; then
					dummy=0
				else 
					cp dgm/Graphic_A01.dgm backgrounds/$localization/$map_Name"."$scales
				fi
			fi

			if [ $mapOK = true ]; then
				# Extract valid scales from Maps.txt
				fxcMaps=$homeDir/FXC/fsl/data/config/Maps.txt
				searchString="\"$mapName\"     1311"
				awk "/$searchString/"'{for(i=NF-2;i<NF-1;i++) print $i}' $fxcMaps > /tmp/scratch
				grep "$searchString" $fxcMaps > /dev/null 2>&1 
				if [ $? -eq 0 ];then
					scale=`more /tmp/scratch`",$scale"
					sed -e "/$searchString/d" $fxcMaps > /tmp/scratch
					cp /tmp/scratch $homeDir/FXC/fsl/data/config/Maps.txt
				fi

				# Update Maps.txt file with the additional scale
				echo "\"$mapName\"     $mapKey     "DGM"    -     $scale     $default     $map_Name" >> $fxcMaps
	
				# Check if map entry exists in menu
				grep "TOGGLEITEM \"$mapName\" M" $menuFile > /dev/null 2>&1 
				if [ $? -ne 0 ]; then
					sed -e '/# Insert maps here/a\' -e "     TOGGLEITEM \"$mapName\" M \"\" \"map $mapName\"" $menuFile > /tmp/scratch
					cp /tmp/scratch $menuFile
				fi

				echo "Added $mapName to scale(s) $scale"
			fi
			rm -f /tmp/scratch

		fi

	elif [ $action = d ]; then
		# Delete a map entry (make it unusable)
		# Get the name of the map that is to be deleted
		printf "\nSpecify menu name of the map: "
		read mapName 
		echo ""
		#echo $mapName > /tmp/scratch
		#map_Name=`sed -e 's/ /_/' /tmp/scratch`

		grep "$mapName" $homeDir/FXC/fsl/data/config/Maps.txt > /tmp/scratch
		if [ $? -eq 0 ]; then
			grep $mapKey /tmp/scratch > /dev/null 2>&1
			if [ $? -ne 0 ]; then 
				# This map was not added by the user
				printf "This is an installed map; delete anyways [y/n]: n\rThis is an installed map; delete anyways [y/n]: "
				read delMap
				if test -z $delMap; then
					delMap=n
				elif [ $delMap != y -o delMap = !n ]; then
					delMap=n
					echo "Invalid choice"
				fi
			else
				delMap=y 
			fi
					
			if [ $delMap = y ]; then
				# Delete map from Map.txt file
				sed -e "/^\"$mapName\"/d" $homeDir/FXC/fsl/data/config/Maps.txt > /tmp/scratch
				cp /tmp/scratch $homeDir/FXC/fsl/data/config/Maps.txt

				# Delete map from menu
				#searchString="TOGGLEITEM \"$mapName\" M"
				searchString="TOGGLEITEM \"$mapName\" "

				sed -e "/$searchString/d" $menuFile > /tmp/scratch
				cp /tmp/scratch $menuFile
				echo "Deleted $mapName map from menu"
			fi
		else
			echo "$mapName was not found"
		fi 
		rm -f /tmp/scratch

	else
		echo "Invalid response" 
	fi

 
  elif [ $option = x ]; then
	# Swap OB5 and OBS model keys (temporary)

	echo "Updating GFS40 and other depict keys"
	cd $homeDir/FXC/fsl/data/config
	cp Sources_OB6.menu Sources.menu
	cd $homeDir/FXC/fsl/data/keys
	cp GFS90_OB6.txt GFS90.txt

  else
	echo "Unrecognized option" 
  fi

  # Determine if user wants to continue or exit
  printf "Continue with system management [y/n]: y\rContinue with system management [y/n]: "
  read again

  if test -z $again; then
	again=y
  fi
  if [ $again != y ]; then
	exit 798
  fi
  done # End of system management loop
 
fi

if [ $mapError = true ]; then
	echo "          INSTALLATION ERROR"
	echo " "
	echo "Maps could not be generated. Check the following:"
	echo "Can you start the IGC with $FXA_HOME/bin/IGC_Process ?"
	echo "Can fxaWish be found at \$FXA_HOME/bin/fxaWish ?"
	echo " "
	echo "If so, generate maps again with 'FXCinstall' >"
	echo "'Management Option' > 'Revise/Add Server'"
fi

echo " "
exit 1000