#!/bin/sh
here=`dirname $0`
#
#  Cleans out EVERYTHING in the preferences Menu for PhOS installation
#
#--------------------------------------------------------------------VARIABLES


if [ x$here = "x" ]
	then
		alert "		PhosphurOS  Installer

This cannot run from a directory with spaces!

Please extract this zip to a location where there will be no spaces in the path name!" "Crap"
exit
	else
		sleep 0
fi

prefer=/phos-b1-p0/home/config/be/Preferences

cd $here
phos=$here/files/PhosphurOS.pkg
pp=/phos-b1-p0
sys=/boot/beos/system


#--------------------------------------------------------------------VARIABLES


################                 ERROR
error() #######################################
{
if [ $1 = "MOUNTPOINT" ] 
	then
		alert --stop "           PhosphurOS  Installer

Attention, You did not setup the partition as required!
Please ensure that  phos-b1-p0  is all lower case!" "Crap"
	part_alert
	test_part  #almost forgot this one!
	else

alert --stop "           PhosphurOS  Installer

Error: $1" "Crap"
fi
} #########################################
#################             ERROR


main()
{
alert "           PhosphurOS  Installer

This will aid you in preparing to install the PhosphurOS.

There is something you MUST do to unlock PhosphurOS." "Exit" "Continue"
	but=$?
	[ $but = 1 ] && do_it
	[ $but = 0 ] && exit

}	

# __________________________________________________________________
# --------------------------------------------------------------------------------------------------------------
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# =========================================



#---------------------------------------------------------------------------------------------------------------
do_it()  #######################################
{
	# Here we will here call the desired functions
	#      ask user to change partition name
part_alert
	#	test if they did, return otherwise
test_part
	#	then call the installer
install
	#	now clean up
cleanup
	#	now we need to patch
patch
	#	See if we need to patch for AMD Athlon XP / MP
AMD_SUCKS
	#	Install Hey, incase we installed to another partition
hey_install
	#	Perform SwitchBlade's patching
switchblade_patch
	#	see if we need to restart
test_restart
} ###########################################
#-----------------------------------------------------------------------------------------------------------------


#----------------------------      PARTITION   ALERT
part_alert()   #####################################
{
	#Tell the user here that the partition needs to be renamed
	# we will not acall outside functions
	alert "           PhosphurOS  Installer

In order to install the PhosphurOS, you must rename the receiving partition as:

		phos-b1-p0

This is to aid in the handling of CPU-specific enhancments.  This also allows for last minute patches and updates.

When you choose to continue, A list of your Disks will appear, and a small alert telling you what to rename the receiving partition to." "Exit" "Let's get going"
	but=$?
	[ $but = 0 ] && exit
	$sys/Tracker /
	alert --info "    phos-b1-p0   "   "Done"
				#we now are pushed to the test
}   ##########################################
#----------------------------    PARTITION   ALERT



	#-------#-------------#-----------#------------#---------------#-----------#--------------------



#----------------------------     TEST     PARTITION
test_part()  ######################################
{
	#Test the existence of the partition (or mountpoint)
	touch /phos-b1-p0/SEE
	if [ -e /phos-b1-p0/SEE ]
		then
			echo Tested good
		else
			error MOUNTPOINT
fi
}  ###########################################
#----------------------------    TEST    PARTITION


install()
{
	alert "		PhosphurOS   Installer

If the installer asks you a question after seemingly completing, do the following:

Press Ctrl-Alt-Del and kill SoftwareValet

This will not be a problem, as once you are alerted after the extraction, the installer is done.  It just doesn't always figure that out.

After wards, a last minute patch, and cleanup will occur.  You may not see any prompts." "Gotcha"

	SoftwareValet $phos
	if [ -e ~/home/config/settings/CPU_MODEL_MXP ]
		then
			rm /phos-b1-p0/beos/system/kernel_intel.patch
			cp /var/tmp/xp-patch /phos-b1-p0/beos/system/kernel_intel.patch
			rm /phos-b1-p0/beos/system/lib/libroot.so.patch
			cp /var/tmp/xp-patch /phos-b1-p0/beos/system/lib/libroot.so.patch
		else
			echo Coooooooooool
	fi
}
cleanup()
{
	mv $prefer/* /var/tmp
	unzip -o -d /phos-b1-p0/ $here/files/prefer_patch.zip
}

patch()
{
	unzip -o -d /phos-b1-p0/ $here/files/updater.zip
}

hey_install()
{
	unzip -o -d /boot/ $here/files/hey.zip
}

AMD_SUCKS()
{
	if [ -e /boot/home/config/settings/CPU_MODEL_MXP ]
		then
			amd_kern=$pp/beos/system/kernel_intel.patch
			amd_libroot=$pp/beos/system/lib/libroot.so.patch
			rm $amd_kern
			rm $amd_libroot
			cp /tmp/xp-patch $amd_kern
			cp /tmp/xp-patch $amd_libroot
			echo PATCHED FOR XP
			rm /boot/home/config/settings/CPU_MODEL_MXP
		else
			echo No need to patch for CPU
	fi
}


test_restart()
{
	
	hey media_server _ABL
	media=$?
	hey audio_server _ABL
	audio=$?
	hey Tracker _ABL
	track=$?

echo $media$audio$track

if [ $media$audio$track = "000" ]
	then
		alert "DONE!" "Awesome"
	else
		alert "Would you like to restart now?" "No" "Duh"
		but=$?
		[ $but = 0 ] && exit
		shutdown -r
fi
}

switchblade_patch()
{
	cp $here/files/nvidia-2001_07_31.zip $pp/apps/drivers/Unified\ NVidia\ Driver/
 	cp $here/files/Bootscript $pp/beos/system/boot/
}

main   #call the main function to get us on our way.