#!/bin/bash
#
# This script runs the actual post-install script
#
echo "Start postflight script"

#echo $*
#echo -----
#env

# the Application's name - this must be configured for each project!
APPNAME="X2"

# get the params we care about
INSTALLER="$1"
DESTPATH="$2"

# destpath is not kept properly in all times. If we have a dest token, use it.
# needs removal of app name if it's there
if [ "$DESTINATION_TOKEN" ]; then
	echo using "$DESTINATION_TOKEN"
	EXECSPATH="$DESTINATION_TOKEN/Contents/MacOS"
else
	EXECSPATH="$DESTPATH/$APPNAME.app/Contents/MacOS"
fi

# get path to executables, go there for further instruction
echo "cd $EXECSPATH"
if ! cd "$EXECSPATH"
then 
	exit 1
fi

######
# what application is appropriate for this machine?

# where the result will go, plus default.
REALAPP="Release_G3"

echo `machine`
PROCNUM=`machine | sed -e "s/ppc//g"`
echo $PROCNUM

if [ "$PROCNUM" -ge 7400 ]
then
	REALAPP="Release_G4"
elif [ "$PROCNUM" -ge 970 ]; then 
	if [ "$PROCNUM" -lt 7400 ]; then
		REALAPP="Release_G5"
	fi
fi

#####
# back to basic installer 

# info
echo "using $REALAPP"

# create a symlink with a temp name
TEMPSYM="temp_sym"
if ! ln -s "$REALAPP" "$TEMPSYM"
then 
	exit 1
fi

# move the new symlink to replace previous sym or binary
# (installer makes default binary)
if ! mv "$TEMPSYM" "$APPNAME"
then 
	exit 1
fi

# all is well
exit 0
