#!/bin/sh
#
# buildTimeLocalizations - perform build time localizations
#
#
#
# Passed args:  [ -p prefix_path ] [ -i install_root ]
#
# -p prefix_path        This path is prepended to the install paths and can be
# 			used during package building to preserve the current systems
#                       install
#
# -i install_root       Can be used to specify an alternative install root,
# 			e.g. /opt/treeps The default varies from system to 
#			system, see getInstallRoot
#
# The above are saved during the intial phase of the build and the used during
# the install the values compiled into the program must match the install 
# time values.

if [ -f ".aborted" ]
then
	rm -f .aborted
	rm -f .scriptLog
fi

. ./scripts/generic/setPath

APP_PREFIX_PATH=
APP_INSTALL_ROOT=

CURRENT_DIR=`pwd`

ABORT_FILE=$CURRENT_DIR/.aborted
SCRIPT_LOG=$CURRENT_DIR/.scriptlog

while [ "$#" -gt "0" ]
do
	case $1 in

	    -i ) APP_INSTALL_ROOT=$2 ; shift ; shift  ;;

	    -p ) APP_PREFIX_PATH=$2 ; shift ; shift ;;

	     * ) shift ;;
	esac

done

export APP_PREFIX_PATH APP_INSTALL_ROOT SCRIPT_LOG ABORT_FILE

echo ""
echo "Performing Build Time Localizations - i.e. Configuring"
echo ""
echo "This may take a moment, please be patient"
echo ""

# First check for Motif, if we don't find it bail out early!

MOTIF_HDR_LOCATION="`hdrLocation Xm/Xm.h`"

if [ -z "$MOTIF_HDR_LOCATION" ]
then
  echo
  echo "Can't find the motif include file Xm.h. Is Motif or Lesstif"
  echo "installed on your system? If not you can get OpenMotif from:"
  echo
  echo "	http://www.openmotif.org"
  echo
  echo "Or you can get Lesstif from:"
  echo
  echo "	http://www.lesstif.org"
  echo
  echo "If it is then modify ./scripts/generic/getXIncludePath"
  exit 1
fi

./scripts/generic/saveEnv

# We need some executable probes to determine some things, and before
# we can build them we have to localize them.

cd ./scripts/localize/probes

./localizeProbes  

if [ ! -f ".aborted" ]
then

cd ../../..   # And return to the top so the rest of the scripts work right


# --------- Now we start the program related localizations!! ------------

./scripts/localize/programDefaults

./scripts/localize/programResources

./scripts/localize/makeParameters

else

cd ../../..   # And return to the top so the rest of the scripts work right

abortScript

fi

if [ -f ".aborted" ]
then
	echo
	echo "---- THE LOCALIZATION/CONFIGURATION FOR YOUR SYSTEM FAILED! ----"
	echo
	echo "The file .scriptLog might point to what went wrong."
	echo
    	echo "You can either manually modify:"
	echo
	echo  "	makeConfig"
	echo  "	${platform}/localized.h"
	echo  "	Res/treeps.${platform}"
	echo  "	Install.params"
	echo
	echo "or"
	echo
    	echo "fix the shell based config engine. See doc/Configure for more details."
	echo
	echo "or"
	echo
	echo "email the .scriptLog file to gmd@orbit2orbit.com and I will help."
	echo
	exit -1
fi

echo "Localization Complete"
echo ""
