#!/bin/sh
#
# This file is part of SnarfNews
# Copyright (C) 1991,1992,1993,1994,1995,1996 Alec Muffett
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# ensure tags contain some non-b64 data (ie: "_")
BEGIN="_begin_"
END="_end_"

# generic FD for the PGP passphrase
PGPPASSFD=9
export PGPPASSFD

# Note: must use 'gzip' here in order to support multiple batches
# within one input; 'gzip' supports concatenated input - 'compress'
# does not.  Moreover 'gzip' is more suited to the overall task due to
# its adaptive compression.

case $0 in
    *batchencode)
	echo "$BEGIN" $@
	cat $@ | gzip -9 | b64encode
	echo "$END"
	;;

    *batchdecode)
	cat $@ | awk "/^$BEGIN/,/^$END/" | b64decode | gzip -d
	;;

    *batchencrypt)
	ppfile=$SNARFSECRET/$1
	if [ ! -f $ppfile ]
	then
	    echo "batchencrypt: $ppfile: no such file" 1>&2
	    exit 1
	fi
	shift

	cat $@ | pgp +batchmode +verbose=0 -fca 9<$ppfile
	;;

    *batchdecrypt)
	ppfile=$SNARFSECRET/$1
	if [ ! -f $ppfile ]
	then
	    echo "batchdecrypt: $ppfile: no such file" 1>&2
	    exit 1
	fi
	shift

	cat $@ | pgp +batchmode +verbose=0 -f 9<$ppfile
	;;
esac

exit 0
