# Quick script to build package file for FloppyFW
#
# It should work on the FloppyFW machine, or on a regular distro.
# make a package tree, cd to just under that tree,
# and execute this script as in the USAGE line below.
#
# Use at your own risk, may be very buggy.
#
# B. Wood 3/6/01
#
USAGE="usage: mkpkg [-hVQ] package_name [floppy_device]
	-h		This help screen
	-V		Print the version number and exit
	-Q		Suppress interactive prompts"
TEXT=""
COPY_OPT="-i"
QUIET="n"
VERSION="FloppyFW mkpkg utility beta 3-13-01 bcwood@pacbell.net"
TMP_PREFIX="/usr/"

while [ $# -ge 1 ] 
do
  case $1 in
    --) shift; break ;;
    -h) shift; echo "$USAGE"; exit 0 ;;
    -V) shift; echo "$VERSION"; exit 0  ;;
    -Q) shift; QUIET="y"; COPY_OPT="" ;;
    -*) echo "$0: Unknown flag \"$1\"" >&2; exit 1 ;;
     *) break ;;
  esac
done


case $# in
  1)
    PACKAGE_NAME=$1
    DEVICE=""
    TEXT=" and skip move to floppy"
    ;;
  2)
    PACKAGE_NAME=$1
    DEVICE="$2"
    TEXT=" on floppy device \"$DEVICE\""
    ;;
  *)
    echo "$USAGE"
    exit 0
    ;;
esac

if test "$QUIET" = "n" ; then
  echo -n "Make package \"$PACKAGE_NAME\"$TEXT? (y/n) "
  read JUNK
  if test "$JUNK" = "n" ; then
    exit 0
  fi
fi

if [ -d $PACKAGE_NAME ] ; then
  cd $PACKAGE_NAME
  TMP_PREFIX=""
else
  cd /
  FLOPPYFW="y"
  COPY_OPT=""
  if test "$QUIET" = "n" ; then
    echo "You need to be in the directory just below $PACKAGE_NAME."
    echo "or the root directory if working from FloppyFW."
    echo -n "About to make package from root. Continue? (y/n) "
    read JUNK
      if test $JUNK != "y" ; then
        exit 0
      fi 
  fi
  if [ ! -s etc/$PACKAGE_NAME.list ] ; then
    echo "Sorry, you must have a /etc/$PACKAGE_NAME.list file."
    exit 0
  fi
fi


if [ -s etc/$PACKAGE_NAME.list ] ; then
  if test "$PWD" = "/" ; then 
    PD=""
  else
    PD=$PWD
  fi
  
  if test "$QUIET" = "n" ; then
    echo -n "Use list file found in $PD/etc/$PACKAGE_NAME.list? (y/n) "
    read JUNK
  else 
    JUNK="y"
  fi
  if test "$JUNK" = "y" ; then
    if test "$FLOPPYFW" = "y" ; then
      echo "Making FloppyFW package using $PD/etc/$PACKAGE_NAME.list"
      tar -cvf $TMP_PREFIX$PACKAGE_NAME.tar `cat etc/$PACKAGE_NAME.list`
    else
      echo "Making package using list $PD/etc/$PACKAGE_NAME.list"
      tar -cpvof ../$PACKAGE_NAME.tar `cat etc/$PACKAGE_NAME.list`
      cd ..
    fi
  fi
else
  echo "Making package without using list file:"
  tar -cpvof ../$PACKAGE_NAME.tar *
  cd ..
fi

bzip2 -9 -v $TMP_PREFIX$PACKAGE_NAME.tar
mv $TMP_PREFIX$PACKAGE_NAME.tar.bz2 $TMP_PREFIX$PACKAGE_NAME.bz2

if test "$DEVICE" != "" ; then
  umount /mnt/floppy > /dev/null 2>&1
  umount /mnt > /dev/null 2>&1
  mount -t vfat $DEVICE /mnt
  if [ ! -d /mnt/packages ] ; then
    mkdir /mnt/packages
  fi
  cp $COPY_OPT $TMP_PREFIX$PACKAGE_NAME.bz2 /mnt/packages
  umount /mnt
else
  echo "No floppy device specified. File not copied"
  echo "Listing new file:" 
  ls -l $TMP_PREFIX$PACKAGE_NAME.bz2
fi
