#!/bin/sh

PAD_DIR="Applications/PAD System 5.0"
BUILD_NUMBER="build 1103"

TARGET_DIR=`cat /tmp/PADLInstallLoc`
#echo "PADLInstallLoc was $TARGET_DIR"
sudo rm -f /tmp/PADLInstallLoc

if [ "$TARGET_DIR" == "/" ] ; then
	#special case for /
	TARGET_DIR=""
fi

if [ -f "$TARGET_DIR/$PAD_DIR/Digitizer/ValidInstall_Digitizer" ] ; then
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/Digitizer/Digitizer Manager.app/Contents/Resources"
	sudo chmod -R ugo+w "$TARGET_DIR/$PAD_DIR/Digitizer/Digitizer Manager.app/Contents/plugins/digitizer"
	if [ "$BUILD_NUMBER" != "" ]; then
		sudo echo "$BUILD_NUMBER" > "$TARGET_DIR/$PAD_DIR/Digitizer/Digitizer Manager.app/Contents/Resources/patch"
	fi
#else
	#sudo rm -drf "$TARGET_DIR/$PAD_DIR/Digitizer/Digitizer Manager.app"
fi

#if [ -f "$TARGET_DIR/$PAD_DIR/ValidInstall_License" ] ; then
#	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/LicenseManagement.app/Contents/Resources"
#	if [ "$BUILD_NUMBER" != "" ]; then
#		sudo echo "$BUILD_NUMBER" > "$TARGET_DIR/$PAD_DIR/LicenseManagement.app/Contents/Resources/patch"
#	fi
#else
#	sudo rm -drf "$TARGET_DIR/$PAD_DIR/LicenseManagement.app"
#fi

if [ -f "$TARGET_DIR/$PAD_DIR/Marker/ValidInstall_Marker" ] ; then
	sudo chmod -R ugo+w "$TARGET_DIR/$PAD_DIR/Marker/Marker Design.app/Contents/Resources"
	sudo chmod -R ugo+w "$TARGET_DIR/$PAD_DIR/Marker/Marker Design.app/Contents/plugins"
	if [ "$BUILD_NUMBER" != "" ]; then
		sudo echo "$BUILD_NUMBER" > "$TARGET_DIR/$PAD_DIR/Marker/Marker Design.app/Contents/Resources/patch"
	fi
#else
#	sudo rm -drf "$TARGET_DIR/$PAD_DIR/Marker/Marker Design.app"
fi

if [ -f "$TARGET_DIR/$PAD_DIR/PADAutoMark/ValidInstall_PADAutoMark" ] ; then
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/PADAutoMark/PAD AutoMark.app/Contents/Resources"
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/PADAutoMark/PAD AutoMark.app/Contents/MarcOS/PadUpdater.app"
	if [ "$BUILD_NUMBER" != "" ]; then
		sudo echo "$BUILD_NUMBER" > "$TARGET_DIR/$PAD_DIR/PADAutoMark/PAD AutoMark.app/Contents/Resources/patch"
	fi
#else 
#	sudo rm -drf "$TARGET_DIR/$PAD_DIR/PADAutoMark.app"
fi

if [ -f "$TARGET_DIR/$PAD_DIR/PADEstimate/ValidInstall_Estimate" ] ; then
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/PADEstimate/PADEstimate.app/Contents"
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/PADEstimate/PADEstimate.app/Contents/Resources"
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/PADEstimate/PADEstimate.app/Contents/MacOS"
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/PADEstimate/PADEstimate.app/Contents/MacOS/PadUpdater.app"
	
	if [ "$BUILD_NUMBER" != "" ]; then
		sudo echo "$BUILD_NUMBER" > "$TARGET_DIR/$PAD_DIR/PADEstimate/PADEstimate.app/Contents/Resources/patch"
	fi
#else 
#	sudo rm -drf "$TARGET_DIR/$PAD_DIR/PADEstimate.app"
fi

if [ -f "$TARGET_DIR/$PAD_DIR/Pattern/ValidInstall_Pattern" ] ; then
	sudo chmod -R ugo+w "$TARGET_DIR/$PAD_DIR/Pattern/Pattern Design.app/Contents/Resources"
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/Pattern/Pattern Design.app/Contents/plugins/plot/plotter/configs"

	if [ "$BUILD_NUMBER" != "" ]; then
		sudo echo "$BUILD_NUMBER" > "$TARGET_DIR/$PAD_DIR/Pattern/Pattern Design.app/Contents/Resources/patch"
	fi
#else
#	sudo rm -drf "$TARGET_DIR/$PAD_DIR/Pattern/Pattern Design.app"
fi

if [ -f "$TARGET_DIR/$PAD_DIR/PlotManager/ValidInstall_Plot" ] ; then
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/PlotManager/PlotManager.app/Contents/Resources"
	sudo chmod ugo+w "$TARGET_DIR/$PAD_DIR/PlotManager/PlotManager.app/Contents/plugins/plot/plotter/configs"
	if [ "$BUILD_NUMBER" != "" ]; then
		sudo echo "$BUILD_NUMBER" > "$TARGET_DIR/$PAD_DIR/PlotManager/PlotManager.app/Contents/Resources/patch"
	fi
##else
#	sudo rm -drf "$TARGET_DIR/$PAD_DIR/PlotManager/PlotManager.app"
fi

echo "remove ValidInstall_*"
sudo rm -f "$TARGET_DIR/$PAD_DIR/Pattern/"ValidInstall_*
sudo rm -f "$TARGET_DIR/$PAD_DIR/Marker/"ValidInstall_*
sudo rm -f "$TARGET_DIR/$PAD_DIR/Digitizer/"ValidInstall_*
sudo rm -f "$TARGET_DIR/$PAD_DIR/PlotManager/"ValidInstall_*
sudo rm -f "$TARGET_DIR/$PAD_DIR/PADAutoMark/"ValidInstall_*
sudo rm -f "$TARGET_DIR/$PAD_DIR/PADEstimate/"ValidInstall_*

if [ "$TARGET_DIR" != "" ] ; then
	PAD_BASENAME=$(basename "$PAD_DIR")
	if [ ! -d "$TARGET_DIR/$PAD_BASENAME" ]; then
		sudo mv -f "$TARGET_DIR/$PAD_DIR" "$TARGET_DIR/"
	else
		sudo cp -Rpf "$TARGET_DIR/$PAD_DIR" "$TARGET_DIR/"
	fi
	PAD_DIRNAME=$(dirname "$PAD_DIR")
	if [ "$PAD_DIRNAME" != "" ]; then
		sudo rm -rf "$TARGET_DIR/$PAD_DIRNAME"
	fi
fi

exit 0
