#!/bin/sh
#FMD: this script will install the bootsector!

if [ -z "$1" ];
then
	echo Usage: $0 disk
	echo where disk is "hdc", "sda", ...
	exit 1
fi

if ! [ "$USER" = "root" ]
then
	echo "you must be root to execute this script!"
	exit 2
fi

DISK="/dev/$1"
#DISK="$1"


	ifconfig eth0
	echo "host: $HOSTNAME"
	echo "Please, check your target host!"
	echo -n "You'll erase the disk $DISK first sectors. This will not erase the partition table, just your previous boot-loader code. Ok ? [N/o] "
	read answer
	if [ "$answer" = "o" ]
	then
                echo "Get the actual disk boot sector of $DISK" && dd if=$DISK of=bootsector.b.disk bs=1 count=512 || ( echo "Error 1 reading the target disk $DISK" ; exit 1 ) && \
                echo "Get the code part of zenboot" && dd if=zenboot.b of=bootsector.b bs=1 count=445 || ( echo "Error 2" ;exit 2 ) && \
                echo "Copy the partition table to the map"&& dd if=bootsector.b.disk of=bootsector.b bs=1 skip=445 seek=445 || ( echo "error3" ;exit 3 )&& \
		echo "Copy the remeaning part of the x86 code (55aa, data and second stage)"&& dd if=zenboot.b of=bootsector.b bs=1 skip=510 seek=510 || ( echo "error4"; exit 4 ) &&\
               echo write all to the DISK boot sector && dd if=bootsector.b of=$DISK || ( echo "error6 writing to the disk $DISK" ; exit 5 )&& echo "Ok, Zen installed on $DISK"
	else
		echo "Aborted by user"
	fi
