#!/bin/bash
# v.0.1
STICK=$1
ISOIMAGE=$2


#lade vmlinuz & initrd.gz herunter
if [ -n "$3" ]; then

# downloading vmlinuz initrd.gz
  if [ -f vmlinuz ]
    then
      echo "- vmlinuz gefunden, wird gelöscht!"
      rm vmlinuz
    fi
  echo "+ Lade vmlinuz für $3 herunter..."
  wget --quiet http://archive.ubuntu.com/ubuntu/dists/$3/main/installer-i386/current/images/hd-media/vmlinuz

  if [ -f initrd.gz ]
    then
      echo "- initrd.gz gefunden, wird gelöscht!"
      rm vmlinuz
  fi
  echo "+ Lade initrd.gz für $3 herunter..."
  wget --quiet http://archive.ubuntu.com/ubuntu/dists/$3/main/installer-i386/current/images/hd-media/initrd.gz

  if [ -f $2 ]
    then
      echo "+ Iso Datei gefunden, sieht gut aus ;)"
    else
      echo "- ...keine Iso-Datei gefunden, bitte herunter laden. Script wird beendet"
      quit
  fi

#bearbeitung des USB-Sticks
  echo "+ installiere notwendige Pakete..."
  sudo apt-get --no-upgrade -q=5 --trivial-only install syslinux mtools mbr
  echo
  echo "Erstellen Sie bitte manuell eine Fat16 (LBA) Partition die Bootbar ist (Sternchen unter Boot!)."
  read -p "Drücken Sie [Enter] um die Partitionierung zu beginnen..."
  umount $STICK
  umount $STICK"1"
  screen fdisk $STICK
  echo "+ erstelle Dateisystem.."
  mkfs -t vfat $STICK"1"
  echo "+ erstelle Verzeichnisse..."
  mkdir -p /mnt/flash
  mount -t vfat $STICK"1" /mnt/flash
  echo "+ schreibe Bootsektor..."
  syslinux -s $STICK"1"
  mkdir -p /mnt/iso
  mount -o loop $ISOIMAGE /mnt/iso
  cp -R /mnt/iso/isolinux/* /mnt/flash
  mv /mnt/flash/isolinux.cfg /mnt/flash/syslinux.cfg
  mkdir -p /mnt/flash/install
  cp vmlinuz /mnt/flash/install
  echo " + kopiere Iso Datei..."
  cp initrd.gz /mnt/flash/install
  cp $ISOIMAGE /mnt/flash
  install-mbr $STICK
  umount /mnt/flash
  umount /mnt/iso
  echo " Fertig!"
else
  echo
  echo "Benutzung:" $0 "</dev/usbstick> <iso-file> <Name>"

  echo
  echo "Version   Name"
  echo "-------+------------"
  echo " 6.06  |  dapper"
  echo " 8.04  |  hardy"
  echo " 8.10  |  intrepid"
  echo " 9.04  |  jaunty"
  echo " 9.10  |  karmic"

fi

