#!/bin/sh

# You'll have to customize this to know about your own packages.  Also, copy
# rangeargs to your private bin directory, or change invocations below.

cmd=`date`"  $@ "
cd $HOME

dest=$1
shift
system=$1
shift
version=$1
shift
case "$system" in
dist)
    cd /u/sfoc/lwall/src/dist/bugs
    maxnum=`/bin/sed -n -e 's/^[^0-9]*\([1-9][0-9]*\).*$/\1/p' ../patchlevel.h`
    curversion='1.0'
    ;;
perl)
    cd /usr/local/src/perl/bugs
    maxnum=`/bin/sed -n -e 's/^[^0-9]*\([1-9][0-9]*\).*$/\1/p' ../patchlevel.h`
    curversion='2.0'
    ;;
rn)
    cd /u/sfoc/lwall/src/rn/bugs
    maxnum=`/bin/sed -n -e 's/^[^0-9]*\([1-9][0-9]*\).*$/\1/p' ../patchlevel`
    curversion='4.3'
    ;;
patch)
    cd /u/sfoc/lwall/src/patch/bugs
    maxnum=`/bin/sed -n -e 's/^[^0-9]*\([1-9][0-9]*\).*$/\1/p' ../patchlevel.h`
    curversion='2.0'
    ;;
warp)
    cd /u/sfoc/lwall/src/warp/bugs
    maxnum=`/bin/sed -n -e 's/^[^0-9]*\([1-9][0-9]*\).*$/\1/p' ../patchlevel.h`
    curversion='7.0'
    ;;
cdiff)
    cd /u/sfoc/lwall/src/cdiff/bugs
    maxnum=`/bin/sed -n -e 's/^[^0-9]*\([1-9][0-9]*\).*$/\1/p' ../patchlevel.h`
    curversion='1.1'
    ;;
*)
    /bin/mail $dest $USER <<EOM
I don't know how to send patches for a program called $system.  Sorry.

If $NAME can figure out what you meant you'll get the patches anyway.

mailpatch speaking for $USER
EOM
    echo $cmd FAILED >> $HOME/.patchlog
    exit 1
    ;;
esac

case "$version" in
$curversion) ;;
*)
    /bin/mail $dest $USER <<EOM
I don't know how to send patches for version $version of $system.  Sorry.

If $NAME can figure out what you meant you'll get the patches anyway.

mailpatch speaking for $USER
EOM
    echo $cmd FAILED >> $HOME/.patchlog
    exit 1
    ;;
esac

args="$*"

set `$HOME/bin/rangeargs -m "$maxnum" $*`

case $# in
0) set `$HOME/bin/rangeargs -m 100 $args`
    set $1
    ;;
esac

for num do
    /bin/cat <<EOM >/tmp/mp$$
Subject: $system version $version patch #$num

[The latest patch for $system version $version is #$maxnum.]

mailpatch speaking for $USER

EOM
    if test -f patch$num; then
	/bin/cat <patch$num >>/tmp/mp$$
    fi
    /bin/mail $dest </tmp/mp$$
done
rm -f /tmp/mp$$
echo $cmd OK >> $HOME/.patchlog
