#!/bin/sh
# az Sat May 31 18:23:16 2008
# a wrapper around recode to emulate mimencode
# mimencode is from metamail, which is...problematic
# but there's no one tool that does it all in debian >=etch.

usage () {
    cat >&2 <<-EOF

Usage: $0 [-q|-b] [-u] [-o outputfile] [file name]
-q: quoted-printable
-b: base64 (default)
-u: decode (default encode)
-o: target file (default stdout)
EOF
    exit 1 
}

B64=1;
DECODE=0;
while getopts "bquo:" OPT; do
    if [ "$OPT" = "b" ] ; then
	B64=1;
    elif [ "$OPT" = "q" ] ; then
	B64=0;
    elif [ "$OPT" = "u" ] ; then
	DECODE=1;
    elif [ "$OPT" = "o" ] ; then
	OUTPUT=$OPTARG
    else
	usage;
    fi
done
shift $(($OPTIND-1))
if [ $# -gt 1 ] ; then
    usage;
fi
INFILE="$1"

TARGET="b64";
if [ "$B64" = "0" ] ; then
    TARGET="qp"
fi
CMD="$TARGET..data"
if [ "$DECODE" = "0" ] ; then
    CMD="data..$TARGET"
fi

if [ -n "$INFILE" ] ; then
    exec < "$INFILE"
fi
if [ -n "$OUTPUT" ] ; then
    exec > "$OUTPUT";
fi

exec recode $CMD
echo "$0: could not run recode!" >&2
exit 1





