#!/bin/bash
# knoppix-mkbootfloppy - Generate bootfloppies for Knoppix
# (works for Knoppix 3.4)
# (C) Klaus Knopper May 2004
#

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin"
export PATH

umask 022

# This does not need to run as root on Knoppix.
# [ "`id -u`" != "0" ] && exec sudo "$0" "$@"

# LANG et al.
[ -f /etc/sysconfig/i18n ] && { . /etc/sysconfig/i18n;  export LANG LC_ALL; }
[ -f /etc/sysconfig/knoppix-terminalserver ] && . /etc/sysconfig/knoppix-terminalserver

XDIALOG_HIGH_DIALOG_COMPAT=1
export XDIALOG_HIGH_DIALOG_COMPAT

TMP="/tmp/knoppix-mkbootfloppy.$$"

KERNEL="$(uname -r)"

case "$KERNEL" in 2.6*) KERNEL=linux26; MINIROOT=minirt26.gz ;; *) KERNEL=linux24; MINIROOT=minirt24.gz ;; esac

# Utility functions, usually called in $0 "start"
# Variable substitution has to be finished before

bailout(){
# Leave and remove temporary files
  rm -f "$TMP"
  exit $1
}

trap bailout 2 15

DIALOG="dialog"
[ -n "$DISPLAY" ] && [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog"

case "$LANGUAGE" in
de|at|ch)
CONTINUE="Weiter"
CANCEL="Abbruch"
TITLE0="Knoppix Bootfloppy erzeugen"
MESSAGE0="Formatiere Diskette und kopiere Daten..."
TITLE1="Diskette 1"
MESSAGE="Dieses Programm erzeugt zwei Disketten, mit denen Knoppix auf einem Rechner gestartet werden kann, dessen CD-Laufwerk nicht bootf�hig ist. Sie ben�tigen neben der aktuellen KNOPPIX-CD (ab 3.4) zwei 1.44MB Disketten, die vom Programm VOLLST�NDIG GEL�SCHT und mit dem Linux-Kernel bzw. einer initialen Ramdisk versehen werden. Wenn Sie fortfahren wollen, legen Sie jetzt bitte die KNOPPIX-CD in ein CD-Rom Laufwerk ein, mounten sie, und klicken dann auf \"Weiter\"."
ERROR="Kann die KNOPPIX-CD in keinem CD-Laufwerk finden (durchsucht wurden /cdrom*, /mnt/cdrom* und /media/cdrom*. Nochmal versuchen?"
MESSAGE1="Bitte legen Sie nun die 1. Diskette (zur Erzeugung der Kernel-Bootfloppy) ein."
FLOPPYERROR="Es ist ein Fehler beim Schreiben auf /dev/fd0 aufgetreten. Diese Diskette ist vermutlich kaputt oder schreibgesch�tzt. Nochmal versuchen?"
TITLE2="Diskette 2"
MESSAGE2="Bitte legen Sie nun die 2. Diskette (zur Erzeugung der Init-Ramdisk) ein."
SUCCESS="Das Erzeugen der Boot- und Initrd-Disketten war erfolgreich."
;;
*)
CONTINUE="Continue"
CANCEL="Cancel"
TITLE0="Knoppix Bootfloppy Creator"
MESSAGE0="Formatting floppy disk and copying data..."
TITLE1="Disk 1"
MESSAGE="This program creates two floppy disks, which can be used in order to boot Knoppix on a computer that cannot boot directly from CD. Besides a current KNOPPIX-CD (3.4 and up), you will need two 1.44MB floppy disks, which be COMPLETELY ERASED and installed with a Linux-kernel and an initial ramdisk. If you want to continue, please insert the KNOPPIX-CD into a CD-Rom drive now, mount the CD so that it is accessible, and click on \"Continue\"."
ERROR="Can't find the KNOPPIX-CD in any CD-Rom drive (searched /cdrom*, /mnt/cdrom* and /media/cdrom*). Try again?"
MESSAGE1="Please insert the first floppy disk now (for creating the Kernel-bootfloppy)."
FLOPPYERROR="There has been an error while writing to /dev/fd0. The floppy disk is probably not writable or defective. Try again?"
TITLE2="Disk 2"
MESSAGE2="Please insert the second floppy disk now (for creating the initial ramdisk)."
SUCCESS="Creation of boot- and initrd floppy disks was successful."
;;
esac

gauge(){
status=0
while [ ! -e "$TMP.done" ]; do echo "$status" ; status="`expr \( 100 - $status \) / 8 + $status + 1`"; sleep 8; done | $DIALOG --title "$TITLE0" --gauge "$MESSAGE0" 8 65 0
}

endgauge(){
touch "$TMP.done" ; wait ; rm -f "$TMP.done"
}

format(){
fdformat /dev/fd0u1440 >/dev/null && mformat a: >/dev/null
return "$?"
}

$DIALOG --defaultno --ok-label "$CONTINUE" --yes-label "$CONTINUE" --no-label "$CANCEL" --cancel-label "$CANCEL" --title "$TITLE0" --yesno "$MESSAGE" 12 65 || bailout 1
while :; do
rm -f "$TMP" "$TMP".done
# Find CD-Rom boot/isolinux directrory
SRCDIR=""
for i in / /mnt /media; do
for d in `ls -1d "$i"/*cdrom* 2>/dev/null`; do
 [ -e "$d"/boot/isolinux/"$KERNEL" -a -e "$d"/boot/isolinux/"$MINIROOT" ] && SRCDIR="$d"/boot/isolinux && break 2
done
done
[ -z "$SRCDIR" ] && { $DIALOG --title "$TITLE0" --yesno "$ERROR" 7 75 && continue || bailout 1; }
break
done


$DIALOG --ok-label "$CONTINUE" --yes-label "$CONTINUE" --no-label "$CANCEL" --cancel-label "$CANCEL" --title "$TITLE1" --yesno "$MESSAGE1" 7 65 || bailout 1
while :; do
rm -f "$TMP" "$TMP".done
gauge &
format && syslinux /dev/fd0u1440 && \
mcopy "$SRCDIR"/{"$KERNEL",boot.msg,logo.16,f2,f3} a: && \
{ rm -f "$TMP"; perl -p -e 's|^APPEND|APPEND load_ramdisk=1 prompt_ramdisk=1 root=/dev/fd0 rw|g;s|quiet ||g;s|initrd=[^ ]* ||g;' "$SRCDIR"/isolinux.cfg >"$TMP" && mcopy "$TMP" a:syslinux.cfg >/dev/null; }
[ "$?" != "0" ] && { endgauge; $DIALOG --title "$TITLE1" --yesno "$FLOPPYERROR" 9 65 && continue || bailout 1; }
for i in `ls -1 "$SRCDIR"/*.kbd 2>/dev/null`; do mcopy "$i" a:; done
endgauge
break
done

$DIALOG --ok-label "$CONTINUE" --yes-label "$CONTINUE" --no-label "$CANCEL" --cancel-label "$CANCEL" --title "$TITLE2" --yesno "$MESSAGE2" 7 65 || bailout 1
while :; do
rm -f "$TMP" "$TMP".done
gauge &
format && dd if="$SRCDIR"/"$MINIROOT" of=/dev/fd0u1440 bs=18k
[ "$?" != "0" ] && { endgauge; $DIALOG --title "$TITLE2" --yesno "$FLOPPYERROR" 9 65 && continue || bailout 1; }
endgauge
break
done

$DIALOG --title "$TITLE0" --msgbox "$SUCCESS" 6 65
bailout 0