#!/bin/bash
#
#  ROOT Setup - Installer for ROOT GNU/Linux
#
#  Copyright (C) 1999-2005 John Eriksson <johne@rootlinux.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#  USA.

LOG=/dev/vc/3

# Read in /etc/profile variables
. /etc/profile

# Define crunch function
crunch() {
 read STRING;
 echo $STRING;
 }

# Define abort function
abort() {
 dialog --backtitle "`bkt Abort Installation`" \
 --defaultno --yesno "Are you sure you want to abort the \
 installation?" 6 40 && exit 1
}

# Define countsize function
countsize() {
a=`ls -s "$1"`
b=`echo $a | cut -f1 -d" "`
echo -n $b; echo kb
}

# Define function to right justify text in the backtitle
bkt() {
 bkti=0
 CURLENGHT="`echo $1 $2 $3 $4 $5 | wc -c`"
 tmpvar=$[ 79 - $VARLENGHT - $CURLENGHT ]
 blanks="`while [ ! "$bkti" = $tmpvar ]; \
 do bkti=$[ $bkti + 1 ]; echo -n " ";done`"
 echo -e "ROOT GNU/Linux ${RLVER}${blanks}$1 $2 $3 $4 $5"
}

# Clear all settings
rm -f /tmp/* 2>/dev/null >/dev/null

# Initialize installation
dialog --backtitle "`bkt System Installation`" \
--infobox "  Initializing ROOT GNU/Linux installation..." 3 53
sleep 3

# Mount dev filesystem
if [ ! -r /dev/fd0 ]; then
  mount -t devfs none /dev
fi

# Mount proc filesystem
if [ ! -r /proc/cmdline ]; then
  mount -t proc none /proc
fi

#
# Keymap setup.
# Automatically inserts the available keymaps in the manu.
#
while true; do
dialog --backtitle "`bkt Keyboard Configuration`" \
--menu "    Select a keymap to load from the list. \
\n\n     Keymap name                  Category" 18 50 9 \
`for ktype in qwerty qwertz azerty dvorak fgGIod; do (cd \
/usr/share/keymaps/i386/$ktype ; for i in *.map.gz; do \
echo $i $ktype; done);done` \
2>/tmp/keymap

if [ $? != 0 ]; then
	abort
    else
	KEYMAP=`cat /tmp/keymap`
	/bin/loadkeys $KEYMAP 2>/dev/vc/3 >/dev/vc/3
	break
fi
done

# Partitioning setup
while [ 0 ]; do
dialog --backtitle "`bkt Partition Setup`" \
--menu "Please choose a partitioning program to run, or choose \
'continue' to continue the installation process." 11 59 3 \
"fdisk" "Powerful with command line interface" \
"cfdisk" "Easy and basic with graphical menus" \
"continue" "Continue the installation process" 2>/tmp/partchoice

if [ $? = 1 -o $? = 255 ]; then
  abort
fi

PARTCHOICE="`cat /tmp/partchoice`"
if [ "$PARTCHOICE" = "continue" ]; then
break;
else
dialog --backtitle "`bkt Partition Setup`" \
--inputbox "Enter name of device to edit. Examples:\n\n \
'/dev/discs/disc0/disc'   (First disk - default)\n \
'/dev/discs/disc1/disc'   (Second disk)\n \
'/dev/hda'                (First IDE disk)\n\n" 13 55 2>/tmp/fdev

if [ $? = 1 -o $? = 255 ]; then
  abort
fi

FDEV="`cat /tmp/fdev`"

if [ -z "$FDEV" ]; then
	FDEV="/dev/discs/disc0/disc"
fi

if [ "$PARTCHOICE" = "fdisk" ]; then
	clear; echo; echo; echo
	echo "ROOT Partition Setup: `fdisk -v`"
	echo "Device: $FDEV"; fdisk $FDEV
elif [ "$PARTCHOICE" = "cfdisk" ]; then
	cfdisk $FDEV
fi
fi
done

# Scan partitions
SWAP_PARTS="`fdisk -l | fgrep "Linux swap"`"
echo "$SWAP_PARTS" >/tmp/swapparts

PARTITIONS="`fdisk -l | fgrep /dev | fgrep part 2>/dev/null`"
echo "$PARTITIONS" >/tmp/partitions

cat /tmp/swapparts | while [ 0 ]; do
read SWAP_PART;

if [ -z "$SWAP_PART" ]; then
  break;
fi

SWAPDEV=`echo "$SWAP_PART" | crunch | cut -f1 -d' '`

if [ ! -z "$SWAPDEV" ]; then
dialog --backtitle "`bkt Partition Setup`" \
--yesno "A Linux swap partition was found:\n $SWAPDEV\n\nDo you want \
to add this partition as a swap partition?" 8 60

 if [ $? = 0 ]; then
  dialog --backtitle "`bkt Partition Setup`" \
  --infobox "Making swap filesystem on:\n $SWAPDEV" 4 60
  swapoff $SWAPDEV >/dev/null 2>/dev/null
  mkswap $SWAPDEV >/dev/vc/3 2>/dev/vc/3
  swapon $SWAPDEV >/dev/vc/3 2>/dev/vc/3
  echo "$SWAPDEV swap swap defaults 0 0" >>/tmp/fstab-swap
  sync
  sleep 1
 fi

fi
done

while [ 0 ]; do
cat << EOF >/tmp/tempscript
dialog --backtitle "`bkt Partition Setup`" \\
--menu "Add partitions which the system should be installed to here. \\
You need at least one partition (/)." 15 74 7 \\
EOF
  cat /tmp/partitions | while [ 0 ]; do
    read PARTITION;
    if [ -z "$PARTITION" ]; then
      break;
    fi
    DEVICE=`echo "$PARTITION" | crunch | cut -f1 -d' '`
    if [ -f /tmp/fstab-root ]; then
      ALTNAME="`cat /tmp/fstab-root | grep $DEVICE | cut -f1 -d' '`"
    fi
    TYPE=`echo "$PARTITION" | tr -d "*" | tr -d "+" | crunch | cut -f6,7,8,9,10 -d' '`
    SIZEK=`echo "$PARTITION" | tr -d "*" | tr -d "+" | crunch | cut -f4 -d' '`
    SIZE=`echo $[ $SIZEK / 1024 ]`
    NAME=`echo $PARTITION | crunch | cut -f1 -d' '`
    if [ -z "$ALTNAME" ]; then
      if [ "$TYPE" = "Linux" -o "$TYPE" = "Linux native" ]; then
        echo "\"$NAME\" \"$TYPE ${SIZE} MB\" \\" >>/tmp/tempscript
      fi
     else
      if [ -f /tmp/fstab-root ]; then
        ON="`cat /tmp/fstab-root | grep $DEVICE | cut -f2 -d' '`"
      fi
      echo "\"$ALTNAME\" \"Mounted on ${ON}, ${SIZE} MB\" \\" >>/tmp/tempscript
    fi
    done
    echo "\"Done - Continue Installation Process\" \"---\" \\" >>/tmp/tempscript
    echo "\"Done - Continue Installation Process\" \"---\" \\" >>/tmp/tempscript
    echo "\"Done - Continue Installation Process\" \"---\" \\" >>/tmp/tempscript
    echo "2>/tmp/return" >>/tmp/tempscript
    rm -f /tmp/return
    . /tmp/tempscript
    if [ $? = 1 -o $? = 255 ]; then
      abort
      continue;
    fi
    NEXT_PARTITION="`cat /tmp/return`"
    if [ -f /tmp/fstab-root ]; then
      if cat /tmp/fstab-root | fgrep "$NEXT_PARTITION" >/dev/null; then
        continue;
      fi
    fi
    if [ "$NEXT_PARTITION" = "Done - Continue Installation Process" ]; then
      if [ ! -f /tmp/fstab-root ]; then
        continue;
      fi
      cat /tmp/fstab-root | while [ 0 ]; do
        read PARTITION;
        POINT="`echo $PARTITION | cut -f2 -d' '`"
        if [ "$POINT" = "/" ]; then
          ROOTPART="`echo $PARTITION | cut -f1 -d' '`"
          echo $ROOTPART >/tmp/rootpart
          break;
        fi
        if [ -z "$PARTITION" ]; then
          break;
        fi
      done
      if [ -f /tmp/rootpart ]; then
        break;
       else
        continue;
      fi
    fi

dialog --backtitle "`bkt Partition Setup`" \
--inputbox "Please specificy a mount point for this partition: \n \
$NEXT_PARTITION\n\nExamples: /, /home, /usr" 11 56 2>/tmp/return

if [ $? = 255 -o $? = 1 ]; then
  continue;
fi

MTPT="`cat /tmp/return`"

if [ -z "$MTPT" -o "`echo "$MTPT" | cut -b1`" = " " ]; then
  continue;
fi

if [ ! "`echo "$MTPT" | cut -b1`" = "/" ]; then
  MTPT="/$MTPT"
fi

rm -f /tmp/return

dialog --backtitle "`bkt Partition Setup`" \
--menu "Please select a filesystem to use for \
this partition:\n $NEXT_PARTITION" 14 48 5 \
reiserfs "" ext3 "" jfs "" ext2 "" noformat "" 2>/tmp/format

if [ $? = 1 -o $? = 255 ]; then
  abort
fi

NEXT_FSTYPE="`cat /tmp/format`"
rm -f /tmp/format

if mount | fgrep "$NEXT_PARTITION" >/dev/null 2>/dev/null; then
  umount $NEXT_PARTITION 2>/dev/vc/3
fi

if [ ! "$NEXT_FSTYPE" = "noformat" ]; then
  if [ "$NEXT_FSTYPE" = "reiserfs" ]; then
dialog --backtitle "`bkt Partition Setup`" \
      --infobox "Making reiserfs filesystem on:\n $NEXT_PARTITION" 4 60
      echo y | mkreiserfs -d $NEXT_PARTITION >/dev/vc/3 2>/dev/vc/3
  elif [ "$NEXT_FSTYPE" = "ext3" ]; then
dialog --backtitle "`bkt Partition Setup`" \
      --infobox "Making ext3 filesystem on $NEXT_PARTITION" 4 60
      mke2fs -j $NEXT_PARTITION 2>/dev/vc/3 >/dev/vc/3
      sync
  elif [ "$NEXT_FSTYPE" = "jfs" ]; then
dialog --backtitle "`bkt Partition Setup`" \
      --infobox "Making jfs filesystem on $NEXT_PARTITION" 4 60
      echo Y | jfs_mkfs $NEXT_PARTITION 2>/dev/vc/3 >/dev/vc/3
      sync
  elif [ "$NEXT_FSTYPE" = "ext2" ]; then
dialog --backtitle "`bkt Partition Setup`" \
      --infobox "Making ext2 filesystem on $NEXT_PARTITION" 4 60
      mke2fs $NEXT_PARTITION 2>/dev/vc/3 >/dev/vc/3
      sync
  fi
fi

dialog --backtitle "`bkt Partition Setup`" \
--infobox "Setting up partition:\n $NEXT_PARTITION" 4 60

mkdir -p /mnt$MTPT >/dev/vc/3 2>/dev/vc/3
mount $NEXT_PARTITION /mnt$MTPT >/dev/vc/3 2>/dev/vc/3
NEXT_FSTYPE="`cat /proc/mounts | grep $NEXT_PARTITION | cut -f3 -d' '`"

if [ "$NEXT_FSTYPE" = "ext2" -o "$NEXT_FSTYPE" = "ext3" ]; then
echo "$NEXT_PARTITION $MTPT $NEXT_FSTYPE defaults 1 1" >>/tmp/fstab-root
  else
echo "$NEXT_PARTITION $MTPT $NEXT_FSTYPE defaults 0 0" >>/tmp/fstab-root
fi
sleep 1

done

# Packages Configuration
dialog --backtitle "`bkt Packages Configuration`" \
--infobox "Locating packages..." 3 36

# Choose every package by default
for cat in base opt net mmedia x gnbase gnopt kde wmaker nonfree; do
   cd /rl/pkgs/${cat}
   for i in *.gz; do
     echo $i >>/tmp/${cat}choose
   done
done

# Show menu of package categories
while [ 0 ]; do
dialog --backtitle "`bkt Packages Configuration`" \
--menu "You should now select which \
packages you want to install.\nSelect a category from the list \
below to edit individual\npackages in that category. \
When done, choose 'continue'." 18 62 9 \
base "Very basic system packages" \
opt "Optional but useful system packages" \
net "Network packages" \
mmedia "Multimedia packages" \
x "X Window System and programs" \
gnbase "Basic GNOME libraries" \
gnopt "Optional GNOME programs" \
kde "KDE environment and programs" \
wmaker "Window Maker and related" \
nonfree "Evil, non-free packages" \
continue "Continue setup" 2>/tmp/return

if [ $? = 1 -o $? = 255 ]; then
  abort
fi

# Put the output in a temporary file
REPLY="`cat /tmp/return`"

# If there is a reply and a corresponding directory exits...
if [ ! -z "$REPLY" -a -d /rl/pkgs/$REPLY ]; then
	cd /rl/pkgs/$REPLY
	cat=$REPLY
	rm -f /tmp/${cat}uh

	# Enable packages that has been marked before
	for i in *.gz; do
		if grep "`echo $i | cut -f1 -d\#`" /tmp/${cat}choose 2>/dev/null >/dev/null; then
			echo "$i $REPLY on" >>/tmp/${cat}uh
		 else
			echo "$i $REPLY off" >>/tmp/${cat}uh
	fi
	done

dialog --backtitle "`bkt Packages Configuration`" \
--checklist "Please select which packages you want to install in \
this category. Be sure you don't delete important packages." 18 62 10 \
`cat /tmp/${cat}uh` 2>/tmp/${cat}choose
fi

if [ "$REPLY" = "continue" ]; then
  break;
fi
done

if [ ! -d /mnt/var/lib/pkg ]; then
  mkdir -p /mnt/var/lib/pkg
  touch /mnt/var/lib/pkg/db
fi

# Install Packages
for pkgcat in base opt net mmedia x gnbase gnopt kde wmaker nonfree; do
   cd /rl/pkgs/$pkgcat
   for i in *; do
     if grep "`echo $i | cut -f1 -d\#`" /tmp/${pkgcat}choose 2>/dev/null >/dev/null; then
	dialog --backtitle "`bkt Installing Packages`" \
	--infobox "Installing ${i}..." 3 60
	pkgadd -f --root /mnt $i >/dev/null 2>/dev/null
     fi
   done
done

# Install kernel
KERNELSRC=/rl/kernel/linux-*.tar.bz2
KERNELBIN=/rl/kernel/kernel.tgz
KERNELVER=`basename $KERNELSRC .tar.bz2 | sed "s/linux-//"`

dialog --backtitle "`bkt Installing Packages`" \
--infobox "Installing Linux Kernel $KERNELVER..." 3 60
tar -C /mnt -xzf $KERNELBIN 2>/dev/null >/dev/null
tar -C /mnt/usr/src -xjf $KERNELSRC 2>/dev/null >/dev/null
ln -sf linux-$KERNELVER /mnt/usr/src/linux
chown -R root.root /mnt/usr/src/linux-$KERNELVER 2>/dev/null >/dev/null

if [ -d /mnt/lib/modules ]; then
  touch /mnt/lib/modules/$KERNELVER/modules.dep
fi

# Add root partition to fstab
if [ -f /tmp/fstab-root ]; then
  cat /tmp/fstab-root >/mnt/etc/fstab
fi

# Add swap partition to fstab
if [ -f /tmp/fstab-swap ]; then
  cat /tmp/fstab-swap >>/mnt/etc/fstab
fi

# Add some standard devices to the fstab
echo "/dev/cdroms/cdrom0 /mnt/cdrom iso9660 noauto,ro,exec 0 0" >>/mnt/etc/fstab
echo "/dev/cdroms/cdrom1 /mnt/cdrom1 iso9660 noauto,ro,exec 0 0" >>/mnt/etc/fstab
echo "/dev/floppy/0 /mnt/floppy auto noauto,exec 0 0" >>/mnt/etc/fstab
echo "none /dev/pts devpts gid=5,mode=620 0 0" >>/mnt/etc/fstab
echo "none /proc proc defaults 0 0" >>/mnt/etc/fstab

# Mount the devfs filesystem on the new system
cd /mnt
if [ ! -d /mnt/dev ]; then
  mkdir -p /mnt/dev
fi
mount -t devfs none /mnt/dev
sync

# Network configuration
DHCP=no
NETWORK=127.0.0.0
IPADDR=127.0.0.1
NETMASK=255.255.255.0

while [ 0 ]; do
 dialog --backtitle "`bkt Network Configuration`" \
--inputbox "Enter the hostname for this computer. Do not include the \
domainname." 9 61 `cat /etc/HOSTNAME | cut -f1 -d.` 2>/tmp/hostname
 if [ $? = 1 -o $? = 255 ]; then
  abort
 fi
 HOSTNM="`cat /tmp/hostname`"
 rm -f /tmp/hostname
 if [ ! -z "$HOSTNM" ]; then
  break;
 fi
done

while [ 0 ]; do
 dialog --backtitle "`bkt Network Configuration`" \
 --inputbox "Enter the domainname for this computer." \
 8 61 `cat /etc/HOSTNAME | cut -f2,3 -d.` 2>/tmp/domainname
 if [ $? = 1 -o $? = 255 ]; then
  abort
 fi
 DOMAIN="`cat /tmp/domainname`"
 rm -f /tmp/domainname
 if [ ! -z "$DOMAIN" ]; then
  break;
 fi
done

echo $HOSTNM.$DOMAIN >/mnt/etc/HOSTNAME
chmod 644 /mnt/etc/HOSTNAME
chown root.root /mnt/etc/HOSTNAME

dialog --backtitle "`bkt Network Configuration`" \
--menu "Please select your network connection method. If the machine \
is not connected to a network, choose 'loopback'." 11 65 3 \
"static IP" "Static IP (internal networks etc)" \
"DHCP" "Dynamic IP via DHCP (cable or DSL)" \
"loopback" "Loopback connection (modem or no net)" 2>/tmp/reply

if [ $? = 1 -o $? = 255 ]; then
  abort
fi
REPLY=`cat /tmp/reply`
rm -f /tmp/reply

if [ "$REPLY" = "DHCP" ]; then
  DHCP="yes"
elif [ "$REPLY" = "loopback" ]; then
  LOOPBACK="yes"
else
  LOOPBACK="no"
fi

if [ "$LOOPBACK" = "no" -a "$DHCP" = "no" ]; then
 while [ 0 ]; do
  dialog --backtitle "`bkt Network Configuration`" \
  --inputbox "Enter the IP address for this computer ($HOSTNM)" \
  8 61 2>/tmp/ipaddress
  if [ $? = 1 -o $? = 255 ]; then
   abort
  fi
  IPADDR="`cat /tmp/ipaddress`"
  rm -f /tmp/ipaddress
  if [ "$IPADDR" = "" ]; then
   continue;
  fi
   echo $IPADDR > /tmp/ipaddress
   break;
 done

 while [ 0 ]; do
  dialog --backtitle "`bkt Network Configuration`" \
  --title "Enter netmask" --inputbox "Enter the netmask for this network." \
8 61 255.255.255.0 2>/tmp/netmask
  if [ $? = 1 -o $? = 255 ]; then
   abort
  fi
  NETMASK="`cat /tmp/netmask`"
  rm -f /tmp/netmask
  if [ "$NETMASK" = "" ]; then
   continue;
  fi
   echo $NETMASK >/tmp/netmask
   break;
 done
 
 BROADCAST=`ipmask $NETMASK $IPADDR | cut -f 1 -d ' '`
 NETWORK=`ipmask $NETMASK $IPADDR | cut -f 2 -d ' '`

 while [ 0 ]; do
  dialog --backtitle "`bkt Network Configuration`" \
  --title "Enter gateway address" --inputbox "Enter the gateway's IP address. \
If you're not using a gateway, just press enter." \
  9 61 2>/tmp/gateway
  if [ $? = 1 -o $? = 255 ]; then
   abort
  fi
  GATEWAY="`cat /tmp/gateway`"
  rm -f /tmp/gateway
  if [ "$GATEWAY" = "" ]; then
    break;
  fi
    echo $GATEWAY >/tmp/gateway
    break;
 done
fi

/bin/cat << ENDFILE > /mnt/etc/rc.d/rc.network
#!/bin/sh
#
# /etc/rc.d/rc.network: network startup
#

echo "Setting up loopback interface."
HOSTNAME=\`cat /etc/HOSTNAME\`
/sbin/ifconfig lo 127.0.0.1
/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo

# Use the lines below to configure
# an ethernet connection.
IPADDR="$IPADDR"
NETMASK="$NETMASK"
NETWORK="$NETWORK"
BROADCAST="$BROADCAST"
GATEWAY="$GATEWAY"
DHCP="$DHCP"
# End of settings.

if [ "\$DHCP" = "yes" ]; then
  echo "Setting up ethernet connection."
  /sbin/dhcpcd -t 12
elif [ ! "\$IPADDR" = "127.0.0.1" ]; then
  echo "Setting up ethernet connection."
  /sbin/ifconfig eth0 \${IPADDR} broadcast \${BROADCAST} netmask \${NETMASK}

  if [ ! "\$GATEWAY" = "" ]; then
    /sbin/route add default gw \${GATEWAY} netmask 0.0.0.0 metric 1
  fi
fi

ENDFILE
chmod 755 /mnt/etc/rc.d/rc.network
/bin/cat << EOF >/mnt/etc/networks
loopback	127.0.0.0
localnet	$NETWORK

EOF
chmod 644 /mnt/etc/networks
/bin/cat << EOF >/mnt/etc/hosts
127.0.0.1	localhost
$IPADDR	 	$HOSTNM.$DOMAIN $HOSTNM

EOF
chmod 644 /mnt/etc/hosts

if [ "$LOOPBACK" = "no" ]; then
 dialog --backtitle "`bkt Network Configuration`" \
 --title "Nameserver configuration" --yesno "Will you be accessing a \
nameserver?" 5 50 
 if [ $? = 0 ]; then
  if [ "$GATEWAY" = "" ]; then
    DNSSAMPLE=`echo $IPADDR | cut -f 1-3 -d .`
  else
    DNSSAMPLE=$GATEWAY
  fi
  while [ "$NAMESERVER" = "" ]; do
   dialog --backtitle "`bkt Network Configuration`" \
   --title "Enter DNS address" \
--inputbox "Enter the DNS address for $DOMAIN." 8 61 $DNSSAMPLE 2>/tmp/dns
   if [ $? = 1 -o $? = 255 ]; then
    rm -f /tmp/dns
    break
   fi
   NAMESERVER="`cat /tmp/dns`"
   rm -f /tmp/dns
  done
  echo "search $DOMAIN" >/mnt/etc/resolv.conf
  echo "nameserver $NAMESERVER" >>/mnt/etc/resolv.conf
 else
  echo "search $DOMAIN" >/mnt/etc/resolv.conf
 fi
fi
if [ "$LOOPBACK" = "no" ]; then
  chmod 644 /mnt/etc/resolv.conf
fi


# Ethernet card probing
cd /mnt

if [ -d lib/modules/`uname -r`/kernel/drivers/net \
     -a ! "$IPADDR" = "127.0.0.1" \
     -a ! -r /cardfound \
     -o "$DHCP" = "yes" ]; then

dialog --backtitle "`bkt Network Configuration`" \
--infobox "Scanning for a network card..." 3 40

for card in de4x5 dgrs eepro100 epic100 hp100 lance \
      ne2k-pci olympic pcnet32 rcpci rtl8139 sktr tulip via-rhine \
      yellowfin 8139too 82596 depca ibmtr 3c501 3c503 3c505 3c507 \
      3c509 3c515 ac3200 acenic sis900 at1700 com90io com90xx \
      cosa cs89x0 de4x5 de600 de620 e2100 eepro eexpress es3210 eth16i \
      ewrk3 fmv18x hostess_sv11 hp-plus hp lne390 ne3210 ni5010 ni52 \
      ni65 sb1000 sealevel smc-ultra smc-ultra32 smc9194 wd pcnet32 \
      hamachi 3c59x ; do
      chroot . /sbin/modprobe $card >/dev/vc/3 2>/dev/vc/3
      
      if [ $? = 0 ]; then
        sleep 1
        dialog --backtitle "`bkt Network Configuration`" \
        --msgbox "Found a card using the $card.o module." 5 60
        echo "$card" > /tmp/cardfound
        break;
      fi
   done
   sleep 2
   
   if [ ! -r /tmp/cardfound ]; then
     dialog --backtitle "`bkt Network Configuration`" \
     --msgbox "No NIC was found on this machine. Please configure it manually later." 6 41
   fi
fi

## Root Password configuration
dialog --backtitle "`bkt Password Configuration`" \
--msgbox "You must set a password for the root account \
in order to login. Press enter to set one." 7 37

cd /mnt; clear; echo; echo; echo; chroot . passwd

# Boot loader configuration
dialog --backtitle "`bkt Boot Loader Configuration`" \
--yesno "Do you want to install LILO?" 5 35

if [ $? = 0 ]; then

LILO_TARGET="/dev/discs/disc0/disc"

dialog --backtitle "`bkt Boot Loader Configuration`" \
--inputbox "Enter the correct name for the device to install \
LILO on. The default device is shown below." 9 53 \
"/dev/discs/disc0/disc" 2>/tmp/liloroot

if [ $? = 1 -o $? = 255 ]; then
  abort
fi

dialog --backtitle "`bkt Boot Loader Configuration`" \
--inputbox "If you want the kernel to have special parameters, \
enter them here. If not, press ENTER." 9 62 2>/tmp/liloparam

if [ $? = 1 -o $? = 255 ]; then
  abort
fi

LILO_TARGET=`cat /tmp/liloroot`
LILOPARAM=`cat /tmp/liloparam`

echo "lba32" > /mnt/etc/lilo.conf
echo "boot=$LILO_TARGET" >> /mnt/etc/lilo.conf
echo "map=/boot/map" >> /mnt/etc/lilo.conf
echo "install=text" >> /mnt/etc/lilo.conf
echo "prompt" >> /mnt/etc/lilo.conf
echo "timeout=100" >> /mnt/etc/lilo.conf
echo "default=linux" >> /mnt/etc/lilo.conf
echo "" >> /mnt/etc/lilo.conf
echo "image=/boot/vmlinuz" >> /mnt/etc/lilo.conf
echo "        label=linux" >> /mnt/etc/lilo.conf
echo "        read-only" >> /mnt/etc/lilo.conf
echo "        root=`cat /tmp/rootpart`" >> /mnt/etc/lilo.conf
echo "        append=\"$LILOPARAM\"" >>/mnt/etc/lilo.conf
echo "" >> /mnt/etc/lilo.conf
echo "#other=/dev/ide/host0/bus0/target0/lun0/part1" >> /mnt/etc/lilo.conf
echo "#        label=win" >> /mnt/etc/lilo.conf

chmod 644 /mnt/etc/lilo.conf

dialog --backtitle "`bkt Boot Loader Configuration`" \
--infobox "Installing the LILO bootloader on:\n $LILO_TARGET" 4 50
cd /mnt
chroot . lilo 2>/dev/vc/3 >/dev/vc/3
sleep 2
fi

dialog --backtitle "`bkt Time Configuration`" \
--yesno "Is your hardware clock set to local time? \
If it's set to UTC, answer No." 6 46 2>/tmp/utc

if [ $? = 0 ]; then
  HWCLOCK="localtime"
 else
  HWCLOCK="UTC"
fi

# Timezone configuration
dialog --backtitle "`bkt Time Configuration`" \
--title "Timezone configuration" \
--menu "Please select the timezone for this machine. \n \
     Name                 Category" \
18 49 10 `cat /etc/zones` 2>/tmp/tz

if [ $? = 1 -o $? = 255 ]; then
  abort
fi

for i in Africa America Antarctica Asia Atlantic \
Australia Brazil Canada Etc Europe General Indian \
Mexico Mideast Pacific SystemV US; do
if [ -f /mnt/usr/share/zoneinfo/$i/`cat /tmp/tz` ]; then
  rm -f /mnt/etc/localtime
  ln -sf /usr/share/zoneinfo/`cat /tmp/tz` /mnt/etc/localtime
fi
done

NETMODULE=`cat /tmp/cardfound`

cat << EOF >/mnt/etc/rc.d/rc.conf
#
# rc.conf(5) - startup config
#

KEYMAP="$KEYMAP"
HWCLOCK="$HWCLOCK"
MODULES="$NETMODULE"
SERVICES="syslogd klogd inetd"

EOF

dialog --backtitle "`bkt System Configuration`" \
--infobox "Setting up default permissions..." 3 40

for file in /mnt/usr/bin/gpg /mnt/usr/bin/vlock \
/mnt/usr/sbin/mtr; do
if [ -f $file ]; then
  chmod +s $file
fi
done

if [ -d /mnt/usr/X11R6/lib/X11/xinit ]; then
  cd /mnt/usr/X11R6/lib/X11/xinit
  if [ -f xinitrc.kde ]; then
    ln -sf xinitrc.kde xinitrc
   elif [ -f xinitrc.gnome ]; then
    ln -sf xinitrc.gnome xinitrc
   elif [ -f xinitrc.wmaker ]; then
    ln -sf xinitrc.wmaker xinitrc
   elif [ -f xinitrc.pekwm ]; then
    ln -sf xinitrc.pekwm xinitrc
   elif [ -f xinitrc.afterstep ]; then
    ln -sf xinitrc.afterstep xinitrc
   elif [ -f xinitrc.fvwm ]; then
    ln -sf xinitrc.fvwm xinitrc
   elif [ -f xinitrc.fluxbox ]; then
    ln -sf xinitrc.fluxbox xinitrc
   elif [ -f xinitrc.openbox ]; then
    ln -sf xinitrc.openbox xinitrc
   elif [ -f xinitrc.enlightenment ]; then
    ln -sf xinitrc.enlightenment xinitrc
   elif [ -f xinitrc.icewm ]; then
    ln -sf xinitrc.icewm xinitrc
  fi
fi


dialog --backtitle "`bkt System Configuration`" \
--infobox "Installing info pages..." 3 40
cat << EOF >/mnt/tmp/makeinfo
#!/bin/bash

if [ -d /usr/share/info ]; then
  rm -f /usr/share/info/dir
  for i in /usr/share/info/*.*; do
    install-info \$i /usr/share/info/dir 2>/dev/null >/dev/null
  done
fi
if [ -d /opt/gnome/info ]; then
  rm -f /opt/gnome/info/dir 2>/dev/null >/dev/null
  for i in /opt/gnome/info/*.*; do
    install-info \$i /opt/gnome/info/dir 2>/dev/null >/dev/null
  done
fi
if [ -d /usr/X11R6/info ]; then
  rm -f /usr/X11R6/info/dir 2>/dev/null >/dev/null
  for i in /usr/X11R6/info/*.*; do
    install-info \$i /usr/X11R6/info/dir 2>/dev/null >/dev/null
  done
fi
EOF

cd /mnt
chroot . chmod 755 /tmp/makeinfo
chroot . /tmp/makeinfo >/dev/null 2>/dev/null
chroot . rm -f /tmp/makeinfo
sleep 1

dialog --backtitle "`bkt System Configuration`" \
--infobox "Updating libraries..." 3 40

echo "/usr/X11R6/lib" >/mnt/etc/ld.so.conf
echo "/usr/local/lib" >>/mnt/etc/ld.so.conf

for dir in /opt/gnome/lib /usr/lib/qt/lib \
/opt/kde/lib; do
if [ -d /mnt$dir ]; then
  echo "$dir" >>/mnt/etc/ld.so.conf
fi
done

chmod 644 /mnt/etc/ld.so.conf

ldconfig -r /mnt

# Create a script to run on first boot
cat << EOF >/mnt/.newinstall
#!/bin/sh
if [ -x /usr/bin/build-xml-catalog ]; then
  /usr/bin/build-xml-catalog >/dev/null 2>/dev/null
fi
if [ -x /usr/bin/build-xsl-catalog ]; then
  /usr/bin/build-xsl-catalog >/dev/null 2>/dev/null
fi
if [ -x /opt/gnome/bin/scrollkeeper-update ]; then
  /opt/gnome/bin/scrollkeeper-update -p /var/lib/scrollkeeper >/dev/null 2>/dev/null
fi
if [ -x /opt/gnome/bin/gtk-update ]; then
  /opt/gnome/bin/gtk-update >/dev/null 2>/dev/null
fi
if [ -x /usr/bin/fc-cache ]; then
  /usr/bin/fc-cache >/dev/null 2>/dev/null
fi

# Set up this directory for KDE
mkdir /tmp/.ICE-unix
chown root.users /tmp/.ICE-unix
chmod 777 /tmp/.ICE-unix
chmod +t /tmp/.ICE-unix

rm -f /.newinstall

EOF

chmod 755 /mnt/.newinstall

# Create an empty lastlog
if [ -d /mnt/var/log ]; then
    touch /mnt/var/log/lastlog
  else
    mkdir -p /mnt/var/log
    touch /mnt/var/log/lastlog
fi

# Turn off swaps and unmount everything
swapoff -a 2>/dev/vc/3 >/dev/vc/3
umount -anr 2>/dev/vc/3 >/dev/vc/3

# Print a "finished" message
dialog --backtitle "`bkt Installation Finished`" \
--msgbox "Installation of ROOT GNU/Linux is \
now complete. Press ENTER to reboot." 6 43

dialog --backtitle "`bkt Rebooting`" \
--infobox "Stand by while rebooting system..." 3 40

# Reboot
sync
sleep 1
reboot -f
