#!/bin/bash
#(c) 2005 Robert Shingledecker
#Frugal Grub Install

. /etc/init.d/dsl-functions

if [ $UID != 0 ]; then
  echo "Got root?"
  echo -n "Press Enter key. "; read gagme
  exit 1
fi    

if [ ! -d /boot/grub ]; then
  echo "You must fetch grub.dsl."
  echo -n "Press Enter key. "; read gagme
  exit 1
fi    

MIRROR=$(dslrc.lua Mirror) || MIRROR="ibiblio.org/pub/Linux/distributions/damnsmall/"
PROTOCOL=$(dslrc.lua Protocol) || PROTOCOL="http"

echo
echo "${RED}DSL ${WHITE}Compressed Image ${BLUE}Install/Upgrade.${NORMAL}"
echo "${YELLOW}No responsibility for data loss or hardware damage!${NORMAL}"
echo " "
echo "A linux partition large enough to hold the image file"
echo "must be created prior to installation."

echo -n "${CYAN}Enter the target partition to hold image ${WHITE}(EXAMPLE: ${YELLOW}hda1): ${NORMAL}" 
read TARGET
if [ -z "$TARGET" ] ; then
  echo "No partition chosen. The script will be terminated."
  echo -n "Press Enter key. "; read gagme
  exit 1
fi    

DISK="$(echo $TARGET | tr -d [0-9])"
FDISK="$(sfdisk -l /dev/$DISK|grep $TARGET|awk '{print $(NF-1)}'|head -n1)"
if [ "$FDISK" != "83" ]; then
  echo "${RED}Sorry system has not detected a linux partition.${NORMAL}"
  echo "You may need to just reboot, so the system can re-read"
  echo "the partition table, or create your partition(s) with ${YELLOW}cfisk"
  echo "${NORMAL}and then reboot."
  echo "Script is terminating..."
  echo -n "Press Enter key. "; read gagme
  exit 1
fi

if [ ! -e /mnt/$TARGET ]; then
  mkdir /mnt/$TARGET
fi  

echo -n "Install from [L]iveCD, from [F]ile, from [W]eb. (l/f/w): "
read INSTALL
case "$INSTALL" in 
  "L" | "l")
    if [ -f /etc/sysconfig/toram ]; then
       echo "You are running 'totam' mode, not a liveCD!"
       echo -n "Script is terminating... Press Enter key. "; read gagme
       exit 1
    fi
    SOURCE="/cdrom"
    IMAGE="/cdrom"
  ;;   
  "F" | "f")
    # read source partition from user                                               
    echo -n "${CYAN}Enter the full path to the iso.  ${WHITE}(EXAMPLE: ${YELLOW}/home/dsl/dsl-0.6.0.iso): ${NORMAL}"
    read SOURCE                                                                     
    if [ -z "$SOURCE" ] ; then                                                      
      echo "No partition chosen. The script will be terminated."                    
      echo -n "Press Enter key. "; read gagme
      exit 1                                                                        
    fi          
  ;;  
  "W" | "w")
    # read ISO from user
    echo "Be sure that your ramdisk has enough space to hold the image file!"
    echo -n "${CYAN}Enter the name of the iso file ${WHITE}(EXAMPLE: ${YELLOW}current.iso): ${NORMAL}"
    read ISO
    if [ -z "$ISO" ] ; then
      ISO="current.iso"
    fi
    URL="$PROTOCOL"://"$MIRROR""current"/"$ISO"
    echo "${GRENN}Standby fetching ${MAGENTA}$URL ${NORMAL}"
    wget "$URL"
    if [ $? != 0 ]; then
      rm -f $ISO
      echo "${RED}Sorry the iso image file could not be retrieved or does not fit"
      echo "on your ramdisk."
      echo "The script is terminating...${NORMAL}"
      echo -n "Press Enter key. "; read gagme
      exit 1
    fi
    SOURCE="$ISO"
  ;;  
esac  

echo "${YELLOW}For INSTALL answer y to format, for UPGRADE answer n.${NORMAL} "
echo -n "${RED}Format the target partition ${MAGENTA}/dev/$TARGET${NORMAL} (y/..)? "
read FORMAT
if [ "$FORMAT" == "y" ]; then
  echo
  echo "${RED}Last change to exit before destroying all data on ${MAGENTA}/dev/$TARGET${NORMAL}"
  echo -n "${CYAN}Continue (y/..)?${NORMAL} "
  read answer
  if [ "$answer" != "y" ]; then
    echo "Aborted.."
    echo -n "Press Enter key. "; read gagme
    exit 0
  fi
  echo
  echo "${BLUE}Formatting ${MAGENTA}/dev/$TARGET${NORMAL}"
  mke2fs /dev/$TARGET 
fi
mount /mnt/$TARGET  # Mount the new target drive 

echo "${BLUE}Setting up system image on ${MAGENTA}/mnt/$TARGET${NORMAL}"
if [ "$SOURCE" != "/cdrom" ]; then
  # We need to mount the ISO file using the loopback driver.
  # Start by creating a mount point directory; we'll call it "staging"
  mkdir /mnt/staging
  mount -t iso9660 -o loop,ro $SOURCE /mnt/staging
  if [ $? != 0 ]; then
    umount /mnt/$TARGET  # unmount the target drive 
    echo "${RED}Sorry $SOURCE not found!"
    echo "Installation not successful!${NORMAL}"
    echo -n "Press Enter key. "; read gagme
    exit 1
  fi  
  IMAGE="/mnt/staging"
fi

cp -af $IMAGE/KNOPPIX/ /mnt/$TARGET/.
if [ ! -d /mnt/$TARGET/boot ]; then
  mkdir /mnt/$TARGET/boot
fi
cp $IMAGE/boot/isolinux/linux24 /mnt/$TARGET//boot/.
cp $IMAGE/boot/isolinux/minirt24.gz /mnt/$TARGET/boot/.

sync;sync

if [ "$FORMAT" == "y" ]; then
  echo "${BLUE}Setting up grub${NORMAL}"
  mkdir /mnt/$TARGET/boot/grub
  cp /boot/grub/device.map /mnt/$TARGET/boot/grub/. 
  sed 's/hda2/'$TARGET'/' /boot/grub/menu.lst > /mnt/$TARGET/boot/grub/menu.lst
  grub-install /dev/$DISK --root-directory=/mnt/$TARGET
  if [ "$TARGET" != "hda1" ]; then
    echo -n "${CYAN}Do you have Windows installed on the first partition ${MAGENTA}/dev/hda1${NORMAL} (y/..)? "
    read answer
    if [ "$answer" == "y" ]; then
      sed -i -e '45,50s/^#//' /mnt/"$TARGET"/boot/grub/menu.lst
    fi
  fi
fi

sync

umount /mnt/$TARGET
echo "${GREEN}Grub Installation Completed.${NORMAL}"
echo -n "Press Enter key. "; read gagme
exit 0