#!/bin/sh
# Copyright (C) 1993 David Niemi
# The author places no restrictions on the use of this script.

# this script requires mtools. If you don't have it, get it
# from prep.ai.mit.edu: /pub/gnu or other GNU repositories.

# modified by Patrick Volkerding (volkerdi@mhd1.moorhead.msus.edu)
# and IBM RS6000 compatibility by Torben N. Rasmussen (tnr@csd.cri.dk)

#define Q&D true	#8^)

format=mformat
copy=mcopy
eject=eject

aixdosformat () {
  echo -n "Formatting..."
  dosformat > /dev/null << EOI

n
EOI
  echo "done"
  return 0
}

aixdoswrite() {
  for file in $*; do
    if [ "$file" != "a:" ]; then
      echo Copying $file ...
      doswrite $file `basename $file` > /dev/null
    fi
  done
  return 0
}

aixeject(){
  echo "Skipping eject"
}
  
set -e

a=3
ap=4
cc=1
d=6
e=5
f=1
iv=2
n=3
oi=3
oop=1
q=4
t=3
tcl=2
x=5
xap=2
xd=3
xv=2
y=1

at="Base Linux"
apt="Text based applications"
cct="GNU C 2.5.2 [NOT RECOMMENDED FOR GENERAL USE - NO C++]"
dt="Program Development/GCC c|c++|obj-C 2.4.5/kernel source and more"
et="Emacs"
ft="FAQs"
ivt="InterViews"
nt="Networking/UUCP/Mail/News"
oit="OI ObjectBuilder"
oopt="OOP: Smalltalk"
qt="Extra Alpha Kernel Source"
tclt="Tcl Script Language/Tk Toolkit"
tt="TeX"
xt="X Window System (XFree86 2.0)"
xapt="X Applications"
xdt="X Programming/Development"
xvt="XView 3.2 (OpenLook Window Manager)"
yt="Games"

echo
echo "Which of the following is your target device:"
echo
echo "1 - Sun floppy drive"
echo "2 - 3.5\" Linux drive a: (/dev/fd0H1440)"
echo "3 - 3.5\" Linux drive b: (/dev/fd1H1440)"
echo "4 - RS6000 floppy drive"
echo
echo -n "Enter your choice (1/2/3/4), or the name of an alternate device: "
read device;
FD="a:"
if [ "$device" = "1" ]; then
  device=/dev/rfd0c
elif [ "$device" = "2" ]; then
  device=/dev/fd0H1440
elif [ "$device" = "3" ]; then
  FD="b:"
  device=/dev/fd1H1440
elif [ "$device" = "4" ]; then
  device=/dev/rfd0
  format=aixdosformat
  copy=aixdoswrite
  eject=aixeject
fi
echo "Creating floppies on device $device."

echo
echo "Please insert a blank floppy for the boot kernel disk and"
echo -n "press [enter], or [s] to skip: "
read foo
if [ "X$foo" != 'Xs' ]; then
  while [ 0 ]; do
    echo
    echo -n "What is the name of the boot kernel you wish to use? "
    read bootkern
    if [ -r $bootkern ]; then
      if [ ! "`basename $bootkern .gz`" = "$bootkern" ]; then # compressed
        gzip -cd $bootkern | dd of=$device obs=18k 
        if [ "$device" = "/dev/rfd0c" ]; then
          eject
        fi
      else # uncompressed
        cat $bootkern | dd of=$device obs=18k
        if [ "$device" = "/dev/rfd0c" ]; then
          eject
        fi
      fi
      break;
    else
      echo
      echo "So such boot kernel $bootkern in this directory."
    fi
  done
fi

echo
echo "Please insert a blank floppy for the install disk (uniboot), or if you"
echo "are using a boot kernel, the root disk (root12m or root14m) and then"
echo -n "press [enter], or [s] to skip: "
read foo
if [ "X$foo" != 'Xs' ]; then
  while [ 0 ]; do
    echo
    echo -n "What is the filename of the disk image you wish to use? "
    read rootdisk
    if [ -r $rootdisk ]; then
      if [ ! "`basename $rootdisk .gz`" = "$rootdisk" ]; then # compressed
        gzip -cd $rootdisk | dd of=$device obs=18k
        if [ "$device" = "/dev/rfd0c" ]; then
          eject
        fi
      else # uncompressed
        cat $rootdisk | dd of=$device obs=18k
        if [ "$device" = "/dev/rfd0c" ]; then
          eject
        fi
      fi
      break;
    else
      echo
      echo "No such disk image $rootdisk in this directory."
    fi
  done
fi

dollar='$'

for series in a ap cc d e f iv n oop oi q t tcl x xap xd xv y; do
  diskno=1
  maxdisk=`eval echo "$dollar$series"`
  descrip=`eval echo "$dollar${series}t"`
  echo
  echo "Making $maxdisk floppies for series \"$series\" (${descrip})"
  while [ "$diskno" -le $maxdisk ]; do
    disk=$series$diskno
    done=
    while [ -z "$done" ]; do
      echo "Please insert disk $disk and press [enter], or enter"
      echo -n "[q] to quit, [f] to skip floppy, [s] to skip series: "
      read answer
      case $answer in
      q)
        exit 0
        ;;
      s)
        diskno=$maxdisk
        done=yes
        ;;
      f)
        done=yes
        ;;
      esac
      if [ -z "${done-}" ]; then
        if [ ! -d "$disk" ]; then
          echo '^G'
          echo "Directory \"$disk\" does not exist!"
        else
          if $format $FD; then
            if $copy $disk/* $FD; then
              done=yes
              if [ "$device" = "/dev/rfd0c" ]; then
                eject
              fi
            else
              echo '^G'
              echo "mcopy failed!"
              if [ "$device" = "/dev/rfd0c" ]; then
                eject || :
              fi
            fi
          else
            echo '^G'
            echo "format failed!"
            if [ "$device" = "/dev/rfd0c" ]; then
              eject || :
            fi
          fi
        fi
      fi
    done
    diskno=`expr $diskno + 1`
  done
done
