#!/bin/sh
#All lines before START INSTALL are ignored by STX Installer; this means one
#script can be used for a textual installer and for the graphical installer
#It's important to remember that the STX Installer only handles a subset of
#bash's scripting features; this should be enough for most installation tasks
#but if more functionality is needed pester "webmaster at mikeasoft dot com".

#Check for root
if [ "`whoami`" != "root" ]
then
	dialog --title "Permissions error" --backtitle "STX Installer" --infobox "Sorry, this program must be executed as root." 4 80
	exit
fi

ROOTDISKS=""
for DISK in `cat /proc/diskstats | grep -E "[hs]d.[0-9]+" | awk {'print $3'}`
do
	TEST=`sfdisk -cn /dev/${DISK:0:3} ${DISK:3:1}`
	if [ "$TEST" == "83" ]	#Make sure it's a linux partition
	then
		ROOTDISKS="$ROOTDISKS $DISK Internal"
	fi
done
dialog --title "Choose partitions" --backtitle "STX Installer" --menu "Which partition would you like to use for the root partition?" 17 80 10 $ROOTDISKS

HOMEDISKS=""
for DISK in `cat /proc/diskstats | grep -E "[hs]d.[0-9]+" | awk {'print $3'}`
do
	TEST=`sfdisk -cn /dev/${DISK:0:3} ${DISK:3:1}`
	if [ "$TEST" == "83" ]	#Make sure it's a linux partition
	then
		HOMEDISKS="$HOMEDISKS $DISK Internal"
	fi
done
HOMEDISKS="$HOMEDISKS Root Bleh"
dialog --title "Choose partitions" --backtitle "STX Installer" --menu "Which partition would you like to use for the home partition?" 17 80 10 $HOMEDISKS

SWAPDISKS=""
for DISK in `cat /proc/diskstats | grep -E "[hs]d.[0-9]+" | awk {'print $3'}`
do
	TEST=`sfdisk -cn /dev/${DISK:0:3} ${DISK:3:1}`
	if [ "$TEST" == "82" ]	#Make sure it's a swap partition
	then
	        SWAPDISKS="$SWAPDISKS $DISK Internal"
	fi
done
SWAPDISKS="$SWAPDISKS None Bleh"
dialog --title "Choose partitions" --backtitle "STX Installer" --menu "Which partition would you like to use for the swap partition?" 17 80 10 $SWAPDISKS

exit
#The following line tells STX Install to start reading the script for
#installation. For more information on this please read the STX Install
#manual available at http://linux.mikeasoft.com/stxinstall (doesn't exist yet)

#Anything inside `s will be executed by the GUI installer, other commands will
#be ignored (except those defined in the scripting language, such as echo, if, 
#fi, then, etc. This allows for ncurses interfaces to be built with dialog

####START INSTALL####

dialog --title "Performing installation" --backtitle "STX Installer" --guage "Formatting /dev/$SWAP" 6 80 5

#Format the partitions
echo "Formatting Partitions (0%)"

if [ -n "$SWAP" ]
then
	if [ "$SWAP" != "None" ] 
	then
		echo "Formatting /dev/$SWAP as swap partition (1%)"
		`swapoff /dev/$SWAP`
		`umount /dev/$SWAP`
		`mkswap /dev/$SWAP`
		`swapon /dev/$SWAP`
	fi
fi

if [ -n "$HOME" ]
then
	if [ "$HOME" != "Root" ]
	then
		if [ "$HOME" != "$ROOT" ] 
		then
			echo "Formatting /dev/$HOME as $FORMAT for home (3%)"
			`umount /dev/$HOME`
			`mkfs.$FORMAT /dev/$HOME`
		fi
	fi
fi

echo "Formatting /dev/$ROOT as $FORMAT for root (6%)"
`umount /dev/$ROOT`
`mkfs.$FORMAT /dev/$ROOT`

#Mount partitions
echo "Mounting partitions (10%)"
`mkdir /mnt/root`
`mount /dev/$ROOT /mnt/root`

echo "Creating /home (10%)"
`mkdir /mnt/root/home`
if [ "$HOME" != "Root"]
then
	if [ "$HOME" != "$ROOT" ]
	then
		`mount /dev/$HOME /mnt/root/home`
	fi
fi

#Copy filesystem
echo "Copying filesystem (/bin) (11%)"
`cp -pR /bin /mnt/root/`
echo "Copying filesystem (/boot) (15%)"
`cp -pR /boot /mnt/root/`
echo "Copying filesystem (/dev) (19%)"
`cp -pR /dev /mnt/root/`
echo "Copying filesystem (/etc) (20%)"
`cp -pR /etc /mnt/root/`
echo "Creating /mnt (25%)"
`mkdir /mnt/root/mnt`
echo "Copying filesystem (/lib) (26%)"
`cp -pR /lib /mnt/root/`
echo "Copying filesystem (/opt) (35%)"
`cp -pR /opt /mnt/root/`
echo "Creating /proc (40%)"
`mkdir /mnt/root/proc`
echo "Copying filesystem (/root) (41%)"
`cp -pRL /root /mnt/root/`
echo "Copying filesystem (/sbin) (43%)"
`cp -pR /sbin /mnt/root/`
echo "Creating /sys (46%)"
`mkdir /mnt/root/sys`
echo "Creating /tmp (47%)"
`mkdir /mnt/root/tmp`
echo "Copying filesystem (/usr) (50%)"
`cp -pR /usr /mnt/root/`
echo "Copying filesystem (/var) (80%)"
`cp -pR /var /mnt/root/`

echo "Creating fstab (90%)"

echo "Done. (100%)"

####END INSTALL####
