#! /bin/sh -
# Install v0.97 - getting better!

install=`basename $0`

function prtc {
	echo
	echo -n [ press return to continue ]
	read
}

echo "Welcome to the Linux v0.97 installation script."

# skip this junk if the hard drive has been partitioned
if [ ! -e .partitioned ]; then
cat <<!!

This script will guide you through installing Linux on your hard
drive.  Doing so will require allocating a partition on your hard
drive, making a filesystem on that partition, and installing the
binaries from this disk on to that partition.  You will also have the
option of making a 'swap' partition, and additional partitions (ie. a
/usr partition).

Within this script, yes or no questions will have something like (Y/n)
in them.  The capital letter is the default choice - to select the
other choice, you must specify it.  When a prompt is asking for more
general information, the default will appear within square brackets,
like [this].

!!

echo -n "Do you wish to continue? (Y/n): "
read ans
if [ .$ans = .n -o .$ans = .N ]; then
	echo
	echo Alright, then, goodbye.
	exit 1
fi

cat <<!!

The first step in installing Linux involves creating partitions on your
hard drive(s).  To do this, you need to edit your hard disk(s)
partition table using 'fdisk'.

You must specify the hard drive to partition.  Hard drive 1 is
specified by /dev/hda, and hard drive 2 by /dev/hdb.  When you are
done partitioning your hard drive, type 'done' at the prompt.  To skip
the partitioning step (ie. you are installing over an old Linux
installation, or have already done this step and the script failed to
detect this), type 'skip'.  To abort this procedure completely, type
'abort' at the prompt.

!!

loop=true
while [ $loop = true ]; do
	echo -n "Drive to partition (/dev/hda|/dev/hdb|done|skip|abort): "
	read ans
	if [ .$ans = .done -o .$ans = .skip ]; then
		loop=false
	elif [ .$ans = .abort ]; then
		echo "Process aborted."
		exit 1
	elif [ $ans ]; then
		fdisk $ans
	else
		echo Sorry, there is no default.
	fi
done

if [ .$ans = .done ]; then
	touch .partitioned
	cat <<!!

You must now reboot your computer so Linux can reread the partition
tables.  While it is rebooting, make sure to remove this root disk and
insert the boot disk.  (Be careful to not reboot or remove the root
disk before the system is completely halted!  You will receive a
message when it is okay to turn off power (reboot).)

To continue the installation, login as 'install' again, and the
previous steps will automatically be skipped.

!!
	prtc
	halt
fi

if [ .$ans = .skip ]; then
	touch .partitioned
fi

fi #endif bit about partitioning

cat <<!!

The next step in installing Linux is to make filesystems on the
partition(s) you created, or prepare them for use as swap space.

Linux knows about the following partitions:
!!

prtc
echo
fdisk -l
prtc
cat <<!!

The following prompt accepts four commands:

	mkfs -c /dev/hd* blocks		- make a filesystem of
					  specified size on the
					  specified partition.

	mkswap -c /dev/hd* blocks	- prepare the specified
					  partition for swapping.

		NOTE:	the -c flag to mkfs and mkswap is optional, but
			recommended.  Without it, the partition will
			not be checked for bad blocks automatically.

	list				- list the partitions again.

	done				- done with this step

	abort				- abort out of this script
!!

loop=true
while [ $loop = true ]; do

	echo
	echo -n "Action (mkfs|mkswap|list|done|abort): "
	read ans
	set $ans
	if [ $1 ]; then
		if [ .$1 = .list ]; then
			echo
			fdisk -l
		elif [ .$1 = .done ]; then
			loop=false
		elif [ .$1 = .abort ]; then
			echo
			echo Process aborted.
			exit 1
		elif [ .$1 = .mkfs -o .$1 = .mkswap ]; then
			echo
			$*
		else
			echo "unsupported action."
		fi
	else
		echo
		echo "Sorry, there is no default action."
	fi
done

cat <<!!

Okay, now we're ready to mount your new partition(s), and actually
start copying Linux over to your system.

However, since I'm not very smart, I don't have the slightest idea
what partitions I'm supposed to use, so you'll have to tell me.
Please answer the following questions carefully:

(For a reminder of the partitions available, type 'list' at any of the
prompts.  If you don't wish to specify a partition, select 'none'. )
!!

# root partition

loop=true
while [ $loop = true ]; do

	echo
	echo -n "Root partition [/dev/hda1]: "
	read ans
	if [ $ans ]; then
		if [ $ans = none ]; then
			echo
			echo "You must specify a root partition."
		elif [ $ans = list ]; then
			echo
			fdisk -l
			prtc
		elif [ $ans = abort ]; then
			echo Process aborted.
			exit 1
		elif [ ! -b $ans ]; then
			echo
			echo $ans is not a block device.
		else
			root=$ans
			loop=false
		fi
	else
		root=/dev/hda1
		loop=false
	fi
done

# /usr partition

loop=true
while [ $loop = true ]; do

	echo
	echo -n "/usr partition [none]: "
	read ans
	if [ $ans ]; then
		if [ $ans = none ]; then
			usr=
			loop=false
		elif [ $ans = list ]; then
			echo
			fdisk -l
			prtc
		elif [ $ans = $root ]; then
			echo
			echo "You have already selected $ans as your root partition."
		elif [ $ans = abort ]; then
			echo Process aborted.
			exit 1
		elif [ ! -b $ans ]; then
			echo
			echo $ans is not a block device.
		else
			usr=$ans
			loop=false
		fi
	else
		usr=
		loop=false
	fi
done

# swap partition

loop=true
while [ $loop = true ]; do

	echo
	echo -n "swap partition [none]: "
	read ans
	if [ $ans ]; then
		if [ $ans = none ]; then
			swap=
			loop=false
		elif [ $ans = list ]; then
			echo
			fdisk -l
			prtc
		elif [ $ans = $root ]; then
			echo
			echo "You have already selected $ans as your root partition."
		elif [ $ans = $usr ]; then
			echo
			echo "You have already selected $ans as your /usr partition."
		elif [ $ans = abort ]; then
			echo Process aborted.
			exit 1
		elif [ ! -b $ans ]; then
			echo
			echo $ans is not a block device.
		else
			swap=$ans
			loop=false
		fi
	else
		swap=
		loop=false
	fi
done

echo "Mounting root partition..."
mount $root /mnt

if [ $usr ]; then

	echo "Mounting /usr partition..."
	if [ ! -d /mnt/usr ]; then
		mkdir /mnt/usr
	fi
	mount $usr /mnt/usr
fi

cat <<!!

Now we are ready to being installing the actual Linux binaries
(programs) on your hard disk.  This will be done automatically, and
without regard to anything that may currently be on those partitions.

(That means if you are upgrading from a previous version of Linux, it
is possible that a number of old, incorrect binaries may be left
behind, and that any 'customized' binaries you have installed on top
of the standard binaries may be eliminated.)

!!

echo -n "[ press return to continue, and type abort to stop here ]"
read $ans
if [ .$ans = .abort ]; then
	echo
	echo "Aborting installation."
	exit 1
fi

echo
echo "Installing /bin..."
if [ ! -d /mnt/bin ]; then
	mkdir /mnt/bin
fi
cp -afv /bin/* /mnt/bin

echo
echo "Installing /dev..."
if [ ! -d /mnt/dev ]; then
	mkdir /mnt/dev
fi
echo "Copying devices..."
cp -af /dev/* /mnt/dev

echo
echo "Installing /etc..."
if [ ! -d /mnt/etc ]; then
	mkdir /mnt/etc
fi
cp -afv /etc/* /mnt/etc
( cd /mnt/etc; for new in *.install; do mv -fv $new `basename $new .install`; done )

echo
echo "Installing /home..."
if [ ! -d /mnt/home ]; then
	mkdir /mnt/home
fi
cp -afrv /home/* /mnt/home

echo
echo "Installing /lib..."
if [ ! -d /mnt/lib ]; then
	mkdir /mnt/lib
fi
cp -afv /lib/* /mnt/lib

echo
echo "Creating /mnt..."
if [ ! -d /mnt/mnt ]; then
	mkdir /mnt/mnt
fi

echo
echo "Creating /tmp..."
if [ ! -d /mnt/tmp ]; then
	mkdir /mnt/tmp
fi

echo
echo "Installing /usr..."
if [ ! -d /mnt/usr ]; then
	mkdir /mnt/usr
fi
cp -afrv /usr/* /mnt/usr

cat <<!!

This is probably the most important question in this install script:
!!
echo
echo -n "What would you like to name your system? [linux]: "
read arg
if [ ! $arg ]; then
	name=linux
else
	name=$arg
fi

echo
echo "Okay, maybe it wasn't that vital.  :)"

echo
echo Generating /etc/rc.local...

cat >/mnt/etc/rc.local <<!!
hostname $name
setterm -blank 10
!!

echo
echo Generating /etc/fstab...
echo $root	/	minix	defaults >> /mnt/etc/fstab
if [ $usr ]; then
	echo $usr	/usr	minix	defaults >> /mnt/etc/fstab
fi
if [ $swap ]; then
	echo $swap	none	swap	sw >> /mnt/etc/fstab
fi

cat <<!!

That should be just about it.  Now you should have a useable
filesystem on your new Linux partition(s).

To be able to boot with your new Linux partitions as root, you need to
follow the instructions for editing your boot image to boot with the
hard disk as root.  This will most likely require using a DOS-based
binary editor such as Norton's Disk Editor, or a similar program.  In
the future, however, you can use the setroot program under Linux,
which allows you to set the root device of a boot image.  (You cannot
use this now, because the root image is in the drive the boot image
goes - you can't have two disks in there at once!)

!!

prtc

# clean up after ourself (successful install)

rm -f .partitioned
exit 0
