#! /bin/sh
#
# mkmpp: make the mpp shell script
#

if [ ! -r config.h ]; then
    echo "$0: configure.sh must be run before mkmpp" 1>&2
    exit 1
fi

exec 1>mpp

cat - << \EOF
#! /bin/sh
#
# mpp: magicfilter preprocessor, to build files for magicfilter-traditional
#

if [ $# -lt 2 ]; then
    echo "usage: $0 path-to-traditional path-to-filter" 1>&2
    exit 1
fi

if [ ! -r "$2" ]; then
    echo "cannot open printer file [$2]" 1>&2
    exit 1
fi

path=$1
def=$2
shift 2
while [ $# -gt 1 ]; do
    defines="$defines -D$1=$2"
    shift 2
done

( m4 $defines $def - << \JEOF
EOF
if test -r config.h; then
    grep PATH_ config.h | grep '"' | sed -e 's/^#define */define(/' \
			                 -e 's/ *"\([^"]*\)".*$/,\1)dnl/'
fi
cat magic.m4
echo JEOF
echo ') | sed -e s,@MAGICFILTER@,$path,'
chmod +x mpp
exit 0
