#!/bin/sh

# paths to used programs:
ncat=cat                  # regular cat
zcat=zcat                 # gunzip to stdout
bzcat="bzip2 -dc"         # bunzip2 to stdout
sed=sed

filelist=FILELIST         # names for "special" files
apply=APPLY

patchfs_list ()
{
    date=`date +"%b %d %k:%M"`
    perm="-r--r--r--"
    xperm="-r-xr-xr-x"
    uid=00000000
    gid=00000000
    size=00000000
    nlink="  1"

#    echo "$xperm $nlink $uid $gid $size $date $apply"
    echo "$perm $nlink $uid $gid $size $date $filelist"
    $cat $1 | egrep '^(--- )|(diff )' |
    $sed -n "{
	s|^.* \([^ ]*\)$|$perm $nlink $uid $gid $size $date \1|gp
    }"
}

patchfs_copyout ()
{
    if [ "$2" = "$filelist" ]; then  # list of all affected files
	$cat $1 | 
	$sed -n "/^diff /{
	    s|^.* \([^ ]*\)$|\1|gp
	}" > $3
	exit 0
     fi

    $cat $1 | 
    $sed -n "/^diff .*$2/,/^diff /{
	/^diff ./{
	    /$2/p
	    d
	}
	p
    }" > $3
}

patchfs_run ()
{
    exit 0
}

type=`file $2`
case $type in
    *bzip*) cat=$bzcat ;;
    *gzip*) cat=$zcat ;;
    *text*) cat=$ncat ;;
    *) 
	exec 2>&1 
	echo "don't know how to handle $type"
	exit 1
esac

case "$1" in
    list) patchfs_list $2; exit 0;;
    copyout) patchfs_copyout $2 $3 $4; exit 0;;
    run) patchfs_run; exit 0;;
esac

exit 1

