#!/bin/sh

# 13/May/2002 version 0.04

BASEDIR=/home/reed
VERSION=patch1-try1
REFERENCES=" NetBSD Security Advisory 2002-002
 gzip buffer overrun with long filename
 http://www.securityfocus.com/bid/3712"
LOGFILE=/var/log/binary-updates

why()
{
  echo "This is a binary update for ${VERSION}."
  echo "${REFERENCES}"
}

usage()
{
  why
  echo usage: binary-update [-ifrvlw]
  cat << end-of-usage
  -i   install (extract archive)
  -r   reverse changes
  -l   list files in archive
  -w   why? (reference information)
end-of-usage

  exit 1
}

err()
{
  exitval=$1
  shift
  echo 1>&2 "$*"
  if [ -d "$TEMPDIR" ] ; then
    rm -rf "$TEMPDIR"
  fi
  exit $exitval
}

log_it()
{
  echo "${VERSION}	$1	`date`" >> $LOGFILE
}

## main

trap 'err 6 "Aborted."' 1 2 3 6 14 15

args=`getopt fwilrv $*`
if [ $? != 0 ]; then
  usage
fi

set -- $args

if [ $# -le 1 ]; then
  usage
fi

while [ $# -gt 0 ]; do
  case "$1" in
    -i)
        iflag="i"
        ;;
    -f)
        echo force
        ;;
    -r)
        rflag="r"
        ;;
    -v)
        verbose="-v"
        ;;
    -l)
        lflag="l"
        ;;
    -w)
        why
        ;;
  esac
  shift
done

if [ -z "$lflag" -a -z "$iflag" -a -z "$rflag" ] ; then
  # all done
  exit
fi

if [ ! -z "$iflag" -a ! -z "$rflag" ] ; then
  err 1 "Can't install and reverse changes at same time!"
fi

TEMPDIR=`mktemp -d /tmp/binary-update`
if [ $? -ne 0 ]; then
  err 1 "Can't create temp directory, exiting..."
fi

tail +168 $0 | pax -O > $TEMPDIR/file-list
if [ $? -ne 0 ]; then
  err 1 "Problem: maybe corrupted archive"
fi

if [ ! -z "$lflag" ] ; then
  echo listing files in archive file:
  cat $TEMPDIR/file-list
fi

CURRENTDIR=`pwd`
cd $BASEDIR

if [ ! -z "$rflag" ] ; then
  echo reversing changes
  while read filename ; do
    directory=`dirname $filename`
    backupfilename="$directory/.binary-update/"`basename $filename`".pre-$VERSION"
    if [ ! -e "$backupfilename" ] ; then
      err 1 "Backup $backupfilename doesn't exist. Aborting."
    fi
    mv "$backupfilename" "$filename"
## need to check for success
    if [ ! -z "$verbose" ] ; then
      echo moved "$backupfilename" to "$filename"
    fi
  done < $TEMPDIR/file-list
  log_it "Reversed"
fi

if [ ! -z "$iflag" ] ; then

  echo backing up files
  while read filename ; do
    directory=`dirname $filename`
    if [ ! -d "$directory/.binary-update" ]; then
## need to check for success
      mkdir -p "$directory/.binary-update"
      echo made backup directory $directory/.binary-update
    fi
    backupfilename="$directory/.binary-update/"`basename $filename`".pre-$VERSION"
    if [ -e "$backupfilename" ]; then
      echo $backupfilename already exists.
      err 1 "Backup not done. Aborting."
    fi
    cp -p "$filename" "$backupfilename"
## need to check for success
    if [ ! -z "$verbose" ] ; then
      echo copied to "$backupfilename"
    fi
  done < $TEMPDIR/file-list

  cd "$CURRENTDIR"

  echo now extracting new files to $BASEDIR
  tail +168 $0 | (cd "$BASEDIR" ; pax -O $verbose -pe -r )
# need to test success here

  log_it "Installed"

fi # if install

rm -rf $TEMPDIR

exit

### SCRIPT ENDS HERE -- TAR FILE FOLLOWS