#!/bin/sh

# The floppy device which you will use as the SAR floppy:
# /dev/fd0 is the first floppy, etc.
FLOPPY=/dev/fd0

# size of your floppy disk, will be used for formatting and for
# the RAMDISK size in the bootable diskette
DISKSIZE=1440

# The top directory where you start this shell from
TOPDIR="/conf/SAR-disk"

# The location of your bootable zImage file
ZIMAGE="/zImage"

# The command you'd use to make a floppy filesystem. I suggest mkfs, since
# that filesystem gets recognized by the kernel as a floppy-to-ram loadable
# format. If you choose another one, you'll be working off a floppy instead
# of off a RAM disk.
MKFS=mkfs

# The command you'd use to check your floppy
FSCK=fsck

# An additional flag you may need for mount, if the MKFS and FSCK commands
# aren't mkfs and fsck
MOUNTFLAG=""

# You've edited enough. Now try it out.


VER="1.00"

getyesno ()
{
    echo -n 'SAR ==> Press y to proceed, anything else to skip: '
    read ANS
    if [ "$ANS" = "y" ]
    then
        $*
    else
        echo 'Skipped.'
    fi
}	

msg ()
{
    echo -n 'SAR ==> ' 
    echo $*
}

error ()
{
    msg "Oops.. something went wrong!" $*
    exit 1
}

insertdisk ()
{
    msg ''
    msg 'Insert a new but low-level formatted floppy into the drive.'
    msg 'This will be your' $1.
}    
removedisk ()
{
    msg 'Remove the floppy from the drive.'
    msg 'Label it' $1 'and store it in a safe place.'
    msg ''
}

formatdisk ()
{
    msg ''
    msg 'About to format the ' $1.
    msg 'Insert it into the drive.. and'
    getyesno $MKFS -c $FLOPPY $DISKSIZE || error "$MKFS failure." \
	"Is the diskette low-level formatted?"
}

checkdisk ()
{
    msg ''
    msg 'About to check validity on' $1.
    getyesno $FSCK -av $FLOPPY || error "$FSCK failure. Crappy disk!"
}

mountdisk ()
{
    msg ''
    msg 'Mounting floppy on /tmp/sardisk.'
    mkdir -p /tmp/sardisk || error "Mkdir failure. Cannot create" \
	"/tmp/sardisk directory!"
    mount -v $MOUNTFLAG $FLOPPY /tmp/sardisk || error "Mount failure." \
	"Cannot mount floppy on /tmp/sardisk!"   
}

liloconfig ()
{
    cd lilo
    echo boot = $FLOPPY				>  lilo.conf
    echo compact                                >> lilo.conf
    echo delay = 10                             >> lilo.conf
    echo install = boot.b                       >> lilo.conf
    echo disktab = disktab                      >> lilo.conf
    echo map = /tmp/sardisk/lilo.map		>> lilo.conf
    echo verbose = 3                            >> lilo.conf
    echo backup = /dev/null                     >> lilo.conf
    echo image = /tmp/sardisk/$ZIMAGE           >> lilo.conf
    echo ramdisk = $DISKSIZE                    >> lilo.conf
    echo root = $FLOPPY                         >> lilo.conf
    
    ./lilo -C lilo.conf || error "Lilo error. Cannot make disk bootable!"
    
    rm lilo.conf
    cd ..
}

copyimage_aux ()
{
    cp $ZIMAGE /tmp/sardisk || error "Cannot copy $ZIMAGE to floppy!"
    rdev /tmp/sardisk/$ZIMAGE $FLOPPY || error "Cannot change " \
	"floppy root device!"
    rdev -r /tmp/sardisk/$ZIMAGE $DISKSIZE || error "Cannot change RAM" \
	"size on floppy!"
        
    liloconfig
}    

copyimage ()
{
    msg ''
    msg 'About to copy zImage file to floppy and to enable it thru LILO.'
    getyesno copyimage_aux
}

makedev_aux ()
{
    cd /tmp/sardisk
    mkdir dev
    awk '{
	system (sprintf ("mknod -m %s %s %s %s %s", $6, $1, $2, $3, $4));
	system (sprintf ("chown %s %s", $5, $1));
    }' $TOPDIR/dev.lst	|| error "Fault in device creation!"
    cd $TOPDIR
}

makedev ()
{
    msg ''
    msg 'About to make devices on floppy.'
    getyesno makedev_aux
}

copyfiles ()
{
    msg ''
    msg 'About to copy necessary files to floppy.'
    getyesno "cp -adP `cat files.lst` /tmp/sardisk" || error \
	"Error while copying files to floppy!"
}

extrafiles_aux ()
{
    rm -f extra/etc/shells extra/etc/passwd
    echo /bin/sash > extra/etc/shells
    echo root::0:0:Superuser:/:/bin/sash > extra/etc/passwd

    cd extra
    cp -adP . /tmp/sardisk  || error "Cannot copy extras to floppy!"
    cd ..
    rm -f extra/etc/shells extra/etc/passwd
}

extrafiles ()
{
    msg ''
    msg 'About to copy extra files to floppy.'
    getyesno extrafiles_aux
}

symlinks_aux ()
{
    cd /tmp/sardisk
    awk '{system (sprintf ("ln -s %s %s", $1, $2)); }' $TOPDIR/links.lst || \
	error "Cannot make extra symlinks on floppy!"
    cd $TOPDIR
}

symlinks ()
{
    msg ''
    msg 'About to make symbolic links on floppy.'
    getyesno symlinks_aux
}

unmountdisk ()
{
    msg ''
    msg 'Unmounting floppy from /tmp/sardisk.'
    msg 'This may take some time, if the file buffers need to be flushed.'
    umount $FLOPPY || error "Problem while unmounting floppy!"
    rm -r /tmp/sardisk
}

echo ''
echo 'ICCE Search and Rescue Disk Maker  V' $VER
echo 'Copyright (c) ICCE / Karel Kubat 1994. All rights reserved.'
echo ''
echo 'SAR-make by Karel Kubat, karel@icce.rug.nl'
echo ''

insertdisk 'SAR DISK'
formatdisk 'SAR DISK'
checkdisk 'SAR DISK'
mountdisk
copyimage 
makedev 
copyfiles 
extrafiles 
symlinks
unmountdisk 
removedisk 'SAR DISK'
