#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin

PARTS="hda1 hdb1 hdc1 hdd1 hde1 hdf1 hdg1 sda1 sdb1 sdc1 sdd1"
DRIVES="hda hdb hdc hdd hde hdf hdg sda sdb sdc sdd"

# in megabytes
IMAGEFS_SIZE=90

#functions
geninstall () {

  echo "


You have selected to install $PRODUCT to your hard drive.
Attempting to find hard drive(s)..."

  for i in `echo $DRIVES | sed -e "s/$CDROM //"`; do
    if fdisk -l /dev/$i | grep -qi disk; then
      VDISKS="$VDISKS $i"
    fi
  done

  echo "I have found the following drives:"

  for j in "$VDISKS"; do
    echo "$j"
  done

  echo "
Which drive would you like to install to?"
  read -p"Please choose: " RESPONSE

  if ! echo $VDISKS | grep -q $RESPONSE; then
    echo "That is not a valid hard drive!"
    halt
  fi

  echo "Continuing with install to $RESPONSE"

  if ! mount -t tmpfs none /tmp; then
    echo "Creating RamDisk failed."
    halt
  fi

  echo "Created RamDisk successfully!"

  cp /cdrom/$ASTIMG /tmp/$ASTIMG

  if ! dd if=/tmp/$ASTIMG of=/dev/$RESPONSE; then
    echo "Install failed."
  else
    echo "Installation Successful! Please restart your
machine and remove the cd-rom."
  fi

  halt
}

astlive () {

  # Mount root, image, etc.

  for i in ${CMDLINE}; do
    if echo $i | grep -q "asturw="; then
      ASTURW=`echo $i | awk '/^asturw=/ { print substr($0, 8); }'`
    fi
  done

  # Auto detect ASTURW
  if [ "$ASTURW" = "auto" -a -x /sbin/findfs ]; then
    ASTURW=`/sbin/findfs LABEL=ASTURW 2>/dev/null`
  fi

  # Get amount of RAM in machine (in mB)
  MEM=`awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo`

  if [ $MEM -lt 100 ]; then
    echo "You might have problems running AstLinux with only ${MEM}mB
of RAM. You should consider getting more."
    sleep 3
    RAMFS=no
  elif [ $MEM -lt 200 ]; then
    RAMFS=no
  else
    RAMFS=yes
  fi

  if echo "${CMDLINE}" | grep -q noram; then
    echo "Overriding RAM detection as specified on the command line"
    RAMFS=no
  fi

  if echo "${CMDLINE}" | grep -q debug; then
    echo "Debugging enabled...will drop to a shell at some point"
    DEBUG=1
  fi

  echo "Looking for AstLinux image..."
  if ! losetup /dev/loop0 /cdrom/os/$ASTIMG; then
    echo "Losetup failed"
    exec /bin/sh
  fi

  echo "AstLinux image found!"
  if ! mount -o ro /dev/loop0 /image; then
    echo "Mount failed"
    exec /bin/sh
  fi

  if [ -n "$ASTURW" ]; then
    echo "Configuring for unionfs..."

    echo "Checking asturw filesystem"

    # check fsck here... (toss stdout, but keep stderr)

    e2fsck -p $ASTURW >/dev/null
    status=$?

    case $status in
    0)
      echo "Asturw filesystem clean."
      ;;

    1)
      echo "Fsck detected and repaired errors on $ASTURW"
      ;;

    2)
      echo "Fsck detected and repaired errors on $ASTURW; rebooting"
      sleep 15
      reboot
      ;;

    *)
      echo "Fsck detected errors on $ASTURW ($status)"
      exec /bin/sh
      ;;
    esac

    if [ "$RAMFS" = "yes" ]; then
      mkdir -p /mnt/asturw /mnt/asturo

      mount -t ext2 -o noatime $ASTURW /mnt/asturw

      # no longer needed with new version of unionfs
      # rm -rf /mnt/asturw/lib/modules

      mount -t tmpfs -o size=${IMAGEFS_SIZE}m none /mnt/asturo

      echo "Copying AstLinux files to RAM..."

      cp -a /image/. /mnt/asturo/

      mount -o ro,remount /mnt/asturo

      mount -t unionfs -o dirs=/mnt/asturw=rw:/mnt/asturo=ro none /new

      cd /
      echo "unmounting squashfs image"
      umount /dev/loop0

      # DJH think this is extraneous
      # don't forget to release the image
      # losetup -d /dev/loop0

      # unmount the 'cdrom'
      # umount /cdrom

    else
      echo "Astlinux files will remain on loopback device."

      mkdir -p /mnt/asturw

      mount -t ext2 -o noatime $ASTURW /mnt/asturw

      # no longer needed since new unionfs version
      # rm -rf /mnt/asturw/lib/modules

      mount -t unionfs -o dirs=/mnt/asturw=rw:/image=ro none /new
    fi

  else
    # non unionfs stuff
    if [ "$RAMFS" = "yes" ]; then
      mount -t tmpfs -o size=${IMAGEFS_SIZE}m none /new

      echo "Copying AstLinux files to RAM..."

      cp -a /image/. /new/
      mount -o ro,remount /new

      # unmount the loop device
      cd /
      umount /dev/loop0
      # umount /cdrom

    else 
      echo "Moving mount point to /new"
      mount --move /image /new
    fi

  fi

  umount /sys
  umount /proc

  # Pivot root and start real init
  echo "Pivoting..."
  cd /new
  pivot_root . oldroot
  exec /usr/sbin/chroot . /bin/sh <<- EOF >dev/console 2>&1
exec /sbin/init ${CMDLINE}
EOF
}

astinstall () {
  echo ""
}

pwareinstall () {
  echo ""
}


# Get kernel CMDLINE
mount -t proc none /proc
mount -t sysfs none /sys

CMDLINE="`cat /proc/cmdline`"

LSMOD="`ls /lib/modules/*.ko 2>/dev/null`"

if [ -n "$LSMOD" ]; then
  echo "Loading required modules..."
  for i in $LSMOD; do
    insmod $i
  done
  fi

  # Mount Root Device
  for x in $DRIVES; do
    if mount -t iso9660 -r /dev/${x} /cdrom >/dev/null 2>&1; then
      if [ -f /cdrom/os/astflag ]; then
        CDROM=${x}
      else
        umount /dev/${x}
      fi
    fi
  done

  for x in $DRIVES; do
    if mount -t vfat -r /dev/${x}1 /cdrom >/dev/null 2>&1; then
      if [ -f /cdrom/os/astflag ]; then
        CDROM=${x}
      else
        umount /dev/${x}1
      fi
    fi
  done

  # CD not found
  if [ -z "$CDROM" ]; then
    echo "AstLinux medium not found."
    exec /bin/sh
  fi

  for i in ${CMDLINE}; do

    case $i in
    astimg=*)
      ASTIMG=`echo $i | awk '/^astimg=/ { print substr($0, 8); }'`
      ;;

    astinstall)
      PRODUCT="AstLinux"
      geninstall
      ;;

    newinstall)
      export PRODUCT="AstLinux"
      exec /sbin/init ${CMDLINE}
      ;;

    astlive)
      astlive
      ;;

    pwareinstall)
      PRODUCT="PBXWare"
      geninstall
      ;;

  esac

done

