#! /bin/sh
#hacked version of cdrdisk-net to insall from harddrive
# Set up path
PATH=.:/bin:/sbin:/usr/bin:/usr/sbin
export PATH

# Set up terminal
TERM=con80x25
export TERM

# Make / writeable
mount -w -n -o remount /dev/ramdisk /

# Define all [default] values
host_addr="198.6.75.7"
host_name="Helios"
network_addr="198.6.75.0"
network_mask="255.255.255.0"
broadcast_addr="198.6.75.255"
gateway_addr="none"
server_name="198.6.75.3"
#server_loc="/usr/gary/minix/tools/linux"
server_loc="/usr/src/Linux.MIN"
packed_loc="/usr/src/TAPE_FILE3.gz"
hpacked_loc="/mnt/chrproot.tar.gz"
gzip_cmd="gzip"
mkdir_cmd="mkdir"
server_uid="root"
root_dev="dev/sda"
cfdisk_options="-z"
mkfs_options="-b1024"
tape_dev="/dev/nst0"
floppy_dev="/dev/fd0"

while :
do

dialog --clear --title "Installation Menu" \
        --menu "What style of installation do you wish to perform?\n" \
        20 70 7 \
        "Packed Net"  "Packed Linux files from server [aka TAPE_FILE3]" \
        "Net"         "'cpio' from a Linux file system on a server" \
        "Tape"        "Local streaming [SCSI] tape" \
        "Harddisk"   "Packed Linux files from harddisk" \
        "Diskettes"   "Floppy Diskettes" \
   "MkLinux"     "MkLinux CD Install" \
   "CDROM"       "[Experimental] CD Install" \
        2> /tmp/result
retval=$?
install_type=`cat /tmp/result`
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac


if [ "$install_type" = "Tape" ] ; then

# Get tape device name
dialog --title "Tape Device Name" --clear \
        --inputbox "What [SCSI] tape do you want to install from?\n\
	Be sure to use the non-rewinding name!\n"\
	16 51 $tape_dev 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
tape_dev=`cat /tmp/result`

elif [ "$install_type" = "Diskettes" -o "$install_type" = "MkLinux" ] ; then

# Get floppy device name
dialog --title "Floppy Device Name" --clear \
        --inputbox "What is the device name for your floppy?\n\
   Note: BeBox should be '/dev/fd1'\n"\
   16 51 $floppy_dev 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
floppy_dev=`cat /tmp/result`

fi

# Get network address
dialog --title "Host Address" --clear \
        --inputbox "What is your host [network] address? \n"\
   16 51 $host_addr 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
host_addr=`cat /tmp/result`

# Get network host name
dialog --title "Host Name" --clear \
        --inputbox "What is your host [network] name? \n"\
   16 51 $host_name 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
host_name=`cat /tmp/result`

# Get network name
dialog --title "Network Number" --clear \
        --inputbox "What is your network number? \n"\
   16 51 $network_addr 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
network_addr=`cat /tmp/result`

# Get network mask
dialog --title "Network Mask" --clear \
        --inputbox "What is your network mask? \n"\
   16 51 $network_mask 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
network_mask=`cat /tmp/result`

# Get broadcast address
dialog --title "Broadcast Address" --clear \
        --inputbox "What is your broadcast address? \n"\
   16 51 $broadcast_addr 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
broadcast_addr=`cat /tmp/result`

# Get gateway name
dialog --title "Gateway Address" --clear \
        --inputbox "If you need to use a gateway\n\
to reach your server, enter it's address here.\n\
If you don't need a gateway, set the value to 'none'.\n\n\
Gateway address? \n"\
   16 70 $gateway_addr 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
gateway_addr=`cat /tmp/result`

if [ "$install_type" = "Net" -o "$install_type" = "Packed Net" ] ; then

# Get host name
dialog --title "Server Name" --clear \
        --inputbox "What is your server's network address? \n"\
   16 51 $server_name 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
server_name=`cat /tmp/result`

# Get host name
dialog --title "Server User Name" --clear \
        --inputbox "User name to use on server? \n"\
   16 51 $server_uid 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
server_uid=`cat /tmp/result`

if [ "$install_type" = "Net" ] ; then
# Get file location
dialog --title "File Location" --clear \
        --inputbox "Where are the Linux/PPC files on your server? \n"\
   16 51 $server_loc 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
server_loc=`cat /tmp/result`

else

# Get file location
dialog --title "File Location" --clear \
        --inputbox "Where is the Linux/PPC distribution image \n\
   file [TAPE_FILE3] on your server? \n"\
   16 51 $packed_loc 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
packed_loc=`cat /tmp/result`

# Get 'gzip' command location
dialog --title "'gzip' Command Location" --clear \
        --inputbox "What is the path for 'gzip' on your server?\n"\
   16 51 $gzip_cmd 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
gzip_cmd=`cat /tmp/result`

fi

 # End of network specific setups
fi


if [ "$install_type" = "Harddisk" ] ; then

# Get file location
dialog --title "File Location" --clear \
        --inputbox "Where is the Linux/PPC distribution image \n"\
   16 51 $hpacked_loc 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
packed_loc=`cat /tmp/result`


# Get 'mkdir' command location
dialog --title "'mkdir' Command Location" --clear \
        --inputbox "What is the path for 'mkdir' on your disk?\n"\
   16 51 $mkdir_cmd 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
mkdir_cmd=`cat /tmp/result`

# Get 'gzip' command location
dialog --title "'gzip' Command Location" --clear \
        --inputbox "What is the path for 'gzip' on your disk?\n"\
   16 51 $gzip_cmd 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
gzip_cmd=`cat /tmp/result`

fi

# Get root device name
dialog --title "Root Device Name" --clear \
        --inputbox "What disk do you want to install on?\n"\
   16 51 $root_dev 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
root_dev=`cat /tmp/result`

dialog --title "Disk setup" --clear \
        --msgbox \
      "\n\
This installation process assumes that the \n\
disk drive $root_dev is used as follows:\n\
  "$root_dev"1 - Linux boot image\n\
  "$root_dev"2 - Root file system \n\
  "$root_dev"3 - Swap space\n\
\n" \
   16 60 2> /tmp/result

# Get root device name
dialog --title "Root Device Name" --clear \
        --inputbox "Which partition for '/'?\n"\
   16 51 "$root_dev"2 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
root_part=`cat /tmp/result`

# Get swap device name
dialog --title "Swap Device Name" --clear \
        --inputbox "Which partition for swap?\n"\
   16 51 "$root_dev"3 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
swap_part=`cat /tmp/result`

# Get disk options
dialog --title "Options for 'cfdisk'" --clear \
        --inputbox "Options for 'cfdisk'?\n"\
   16 51 "$cfdisk_options" 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
cfdisk_options=`cat /tmp/result`

# Get file system options
dialog --title "Options for 'mkfs'" --clear \
        --inputbox "Options for 'mkfs'?\n"\
   16 51 "$mkfs_options" 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
mkfs_options=`cat /tmp/result`

# Customization complete, go do it!
break

done

# All set, now do it!

dialog --title "Partitioning your disk" --clear \
        --msgbox \
      "\n\
Please partition the disk drive $root_dev as follows:\n\
  "$root_dev"1 - Type PowerPC, Bootable\n\
  "$root_dev"2 - Type Linux, Most of disk\n\
  "$root_dev"3 - Type Linux Swap, maybe 32 Mb\n\
\n" \
   16 60 2> /tmp/result

ifconfig eth0 $host_addr
if [ "$gateway_addr" != "none" ] ; then
   route add default gw $gateway_addr
fi

#route add -net $network_addr netmask $network_mask broadcast $broadcast_addr
route add -net $network_addr netmask $network_mask

TERM=linux
export TERM
cfdisk $root_dev $cfdisk_options

mke2fs $mkfs_options $root_part
if [ "$install_type" != "Harddisk"  ] ; then

#rm -f /etc/mtab
mount -t ext2 $root_part /mnt
cd /mnt

fi
case "$install_type" in
   "Net")
#      rsh $server_name -l $server_uid "cd $server_loc;find . -print | cpio -o" | cpio -iv --make-directories --preserve-modification-time ;;
      rsh $server_name -l $server_uid "cd $server_loc;find . -print | cpio -o" | pax -rvup e ;;
   "Packed Net")
#      rsh $server_name -l $server_uid "cat $packed_loc" | cpio -iv --make-directories --preserve-modification-time ;;
      rsh $server_name -l $server_uid "$gzip_cmd -d <$packed_loc" | pax -rvup e ;;
    "Harddisk")
        
    $mkdir_cmd /callisto
     mount -t ext2 $root_part /callisto
    $gzip_cmd -d <$hpacked_loc | pax -rvup e
     cd /callisto
         ;;
    "Diskettes")
      echo
      echo -n "Insert 'BUILD TOOLS' floppy: "
      read XX
      pax -rvf $floppy_dev
      echo "Insert Base File system floppies (in order)"      
      while :
      do
        echo -n "Insert next floppy - type N to quit: "
        read XX
        if [ "$XX" = "N" -o "$XX" = "n" ] ; then
           break
        fi
#        gzip -d <$floppy_dev | cpio -iv --make-directories --preserve-modification-time
#        bin/gzip -d <$floppy_dev | pax -rvup e
      bin/dd if=$floppy_dev bs=36b | cat >>BASE_FILES.gz
      done
      bin/gzip -d <BASE_FILES.gz | pax -rvup e
      bin/rm BASE_FILES.gz
      ;;
   "CDROM")
      mount -o ro -t iso9660 /dev/sr0 /cdrom
      pax -rvuf /cdrom/RedHat/base/basefs.tar
      ;;
   "MkLinux")
      echo
      echo -n "Insert CDROM-TOOLS floppy: "
      read XX
      pax -rvup e -f $floppy_dev
      tmp/Install
      ;;
     "Tape")
      mt -f $tape_dev rewind
      mt -f $tape_dev fsf 2
#      cpio -iv --make-directories --preserve-modification-time <$tape_dev
      pax -rvuf $tape_dev -p e
      mt -f $tape_dev rewind
      mt -f $tape_dev fsf 1
      ./bin/dd if=$tape_dev of="$root_dev"1 bs=256b
      mt -f $tape_dev rewind
      ;;
esac      

# Create FSTAB
cat >etc/fstab <<ZZZ
$root_part / ext2 rw 1 1
$swap_part none swap
/proc   /proc   proc    defaults
ZZZ

# Initialize swap partition
sbin/mkswap $swap_part

##if [ "$install_type" != "Diskettes" ] ; then

# Set up networking

cat >etc/sysconfig/network-scripts/ifcfg-eth0 <<ZZZ
#!/bin/sh

#>>>Device type: ethernet

#>>>Variable declarations:
DEVICE=eth0
IPADDR=$host_addr
NETMASK=$network_mask
NETWORK=$network_addr
BROADCAST=$broadcast_addr
GATEWAY=$gateway_addr
ONBOOT=yes
#>>>End variable declarations
ZZZ

cat >etc/sysconfig/network <<ZZZ
NETWORKING=yes
HOSTNAME=$host_name
ZZZ

# End of network setup
##fi

cd /
sync
umount $root_part
sync

