#!/bin/sh

# required in pwd:
# - etherboot-5.4.3.tar.bz2
#	http://downloads.sourceforge.net/etherboot/etherboot-5.4.3.tar.bz2

PACKAGE=etherboot
VERSION=5.4.3

CWD=$PWD
TMP=${TMP:-/tmp}
MNT=$PACKAGE-$$
IMGS="$PACKAGE.img ${PACKAGE}_pcmcia.img"

for IMG in $IMGS ; do
  # build
  echo "Building: $IMG"
  rm -rf $TMP/$PACKAGE-$VERSION/
  tar jxf $CWD/$PACKAGE-$VERSION.tar.bz2 -C $TMP/ || exit 1
  chmod -R g-w $TMP/$PACKAGE-$VERSION/
  chown -R root:root $TMP/$PACKAGE-$VERSION/
  cd $TMP/$PACKAGE-$VERSION/src/ || exit 1
  if [ $IMG = $PACKAGE.img ] ; then
    BUILD_PCMCIA=""				# etherboot.img
  else
    BUILD_PCMCIA="EXTRA_CFLAGS=-DCONFIG_PCMCIA"	# etherboot_pcmcia.img
  fi
  make $BUILD_PCMCIA allzlilos &> make_allzlilos.output || exit 1

  # prep image
  dd if=/dev/zero of=$TMP/$IMG bs=1 count=0 seek=1440K &> /dev/null || exit 1
  mkdosfs $TMP/$IMG &> /dev/null || exit 1
  syslinux -s $TMP/$IMG || exit 1
  mkdir -p $TMP/$MNT/
  mount -t vfat $TMP/$IMG $TMP/$MNT/ -o loop || exit 1

  # config & text setup
  cat << EOF > $TMP/$MNT/syslinux.cfg
default ne
prompt 1
display message.txt

EOF

  # be tidy. start with a cleared screen
  echo -ne "\014" > $TMP/$MNT/message.txt

  cat << EOF >> $TMP/$MNT/message.txt

Welcome to the Etherboot v$VERSION Bootdisk

The following is a complete list of drivers available on this disk. Since they
do not necessarily have a one-to-one name relationship with those in the Linux
kernel, you may have to try several at random until one works.

3c503        cs89x0       etherfabric  ne           r8169        tg3
3c509        davicom      forcedeth    ns83820      rtl8139      tlan
3c515        depca        ide_disk     ns8390       sis900       tulip
3c529        dmfe         mt23108      pc_floppy    sk_g16       undi
3c595        e1000        mt25218      pcnet32      skel-isa     via-rhine
3c90x        eepro        mtd80x       pnic         skel         via-velocity
amd8111e     eepro100     myri10ge     prism2_pci   smc9000      w89c840
bnx2         epic100      natsemi      prism2_plx   sundance     wd

Enter your selection at the prompt below.

EOF

  # update config, copy drivers
  # note the ugly kludge for 8.3 filenames. this is necessary!
  echo -n "Copying drivers: "
  cd $TMP/$PACKAGE-$VERSION/src/bin/
  for ORGNAME in *.zlilo ; do
    LABEL=$(basename $ORGNAME .zlilo)
    case "$LABEL" in
      etherfabric)
        DOSNAME=ethrfbrc.zli
      ;;
      forcedeth)
        DOSNAME=forcdeth.zli
      ;;
      pc_floppy)
        DOSNAME=pc_flopy.zli
      ;;
      prism2_pci)
        DOSNAME=prsm2pci.zli
      ;;
      prism2_plx)
        DOSNAME=prsm2plx.zli
      ;;
      via-rhine)
        DOSNAME=viarhine.zli
      ;;
      via-velocity)
        DOSNAME=viavlcty.zli
      ;;
      *)
        DOSNAME=$LABEL.zli
      ;;
    esac
    echo -n "$LABEL "
    echo -e "label $LABEL\n  kernel $DOSNAME" >> $TMP/$MNT/syslinux.cfg
    cp -p $ORGNAME $TMP/$MNT/$DOSNAME
  done
  echo

  # image finale
  umount $TMP/$MNT/
  sync
  mv $TMP/$IMG $CWD/

  # tidy up
  rm -rf $TMP/$MNT/ $TMP/$PACKAGE-$VERSION/
done