#! /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 /
cd /

# 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"
hpacked_loc="/mnt/chrproot.tar.gz"
gzip_cmd="/mnt/gzip"
mkdir_cmd="/mnt/mkdir"
server_uid="root"
root_dev="dev/sda"
mkfs_options="-b1024"
network_supp="NO"

while :
do

dialog --clear --title "Installation Menu" \
        --menu "What style of installation do you wish to perform?\n" \
        20 70 7 \
        "Harddisk"   "Packed Linux files from harddisk [chrproot.tar.gz]" \
        2> /tmp/result
retval=$?
install_type=`cat /tmp/result`
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac

# 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`

dialog --clear --title "Network Connection" \
        --menu "Do you use your box in a Network?\n" \
        20 70 7 \
        "YES"   "Network Connection" \
        "NO"   "Network Connection" \
        2> /tmp/result
retval=$?
network_supp=`cat /tmp/result`
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac

if [ "$network_supp" = "YES" ] ; then

# 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 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`

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 - Root file system \n\
  "$root_dev"2 - 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"1 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"2 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac
swap_part=`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_part" - Type Linux, Most of disk\n\
  "$swap_part" - Type Linux Swap, maybe 32 Mb\n\
\n" \
   16 60 2> /tmp/result
retval=$?
case $retval in
  1)
    echo "Cancelled!"; exit;;
  255)
    continue;;
esac

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

    "Harddisk")
        
    $mkdir_cmd /callisto
     mount -t ext2 $root_part /callisto
    $gzip_cmd -d <$hpacked_loc | pax -rvup e
     cd /callisto
         ;;
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

if [ "$network_supp" = "YES" ] ; then

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

else

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

fi

# End of network setup
##fi

cd /
sync
umount $root_part
sync

