#! /bin/sh

# local options:  ac_help is the help message that describes them
# and LOCAL_AC_OPTIONS is the script that interprets them.  LOCAL_AC_OPTIONS
# is a script that's processed with eval, so you need to be very careful to
# make certain that what you quote is what you want to quote.

ac_help='--path=PATH		look for conversion programs in PATH
--filterdir=DIR		where to put printer filters (prefix/sbin/printers)
--traditional		build a more traditional sort of magicfilter
--use-local-magic	install a private magic file'

LOCAL_AC_OPTIONS='
case X"$1" in
X--path=*)  AC_PATH=`echo "$1" | sed -e 's/^[^=]*=//'`
	    shift 1
	    ;;
X--path)    AC_PATH="$2"
	    shift 2
	    ;;
X--use-local-magic|X--use-local)
	    ac_local_magic=T
	    shift 1
	    ;;
X--filterdir=*)
	    AC_FILTER=`echo "$1" | sed -e 's/^[^=]*=//'`
	    shift 1
	    ;;
X--filterdir)
	    AC_FILTER=$2
	    shift 2
	    ;;
X--traditional)
	    TARGET=traditional
	    shift
	    ;;
*)	    ac_error=1
	    ;;
esac'

# load in the configuration file
#
TARGET=magicfilter
. ./configure.inc

# and away we go
#
AC_INIT magicfilter

AC_PROG_CC

AC_SUB filterdir ${AC_FILTER:-$AC_PREFIX/sbin/printers}

if [ ! "$ac_local_magic" ]; then
    case $ac_os in
    [Ff]ree[Bb][Ss][Dd])magicpath=/etc:/usr/etc:/usr/share/misc ;;
    *)			magicpath=/etc:/usr/etc: ;;
    esac

    # check to see if the system magic file is moderately recent
    #
    trap "rm -f $$" 1 2 3 9 15
    echo "@PJL JOB" > $$
    F=`file $$ 2>/dev/null | grep -i pjl`
    echo "%PDF-1.1" > $$
    G=`file $$ 2>/dev/null | grep -i pdf`

    if [ "$F" -a "$G" ]; then
	save_AC_PATH=$AC_PATH
	AC_PATH=$magicpath MF_PATH_INCLUDE MAGIC -r magic || ac_local_magic=T
	AC_PATH=$save_AC_PATH
    else
	echo "file(1) is too old -- using private magic file" | tee config.log
	ac_local_magic=T
    fi
else
    echo "Using private magic file $AC_CONF/mf.magic" | tee config.log
fi
rm -f $$
trap 1 2 3 9 15

if [ "$ac_local_magic" ]; then
    # if we're using local magic, manually write the substitution
    # information into the config files
    AC_CONFIG MAGIC "$AC_CONF/mf.magic"
    AC_SUB INSTALL_MAGIC ""
else
    AC_SUB INSTALL_MAGIC ": "
fi
AC_SUB DO_WHAT install-$TARGET

# AC_PROG_LN_S
# AC_PROG_YACC


if AC_CHECK_HEADERS basis/options.h; then
    if LIBS="-lbasis" AC_CHECK_FUNCS x_getopt; then
	AC_LIBS="$AC_LIBS -lbasis"
	AC_SUB XGETOPT
	HAVE_XGETOPT=T
    fi
fi
test "$HAVE_XGETOPT" || AC_SUB XGETOPT options.o


AC_CHECK_FUNCS basename

MF_PATH_INCLUDE RANLIB ranlib true || AC_CONFIG RANLIB ':'
MF_PATH_INCLUDE M4 m4 || AC_FAIL "magicfilter requires m4"

MF_PATH_INCLUDE GS gs
MF_PATH_INCLUDE GZIP gzip gzcat || MF_PATH_INCLUDE ZCAT zcat
MF_PATH_INCLUDE BZIP bzip2
MF_PATH_INCLUDE UNCOMPRESS uncompress
MF_PATH_INCLUDE DVIPS dvips
MF_PATH_INCLUDE PNMTOPS pnmtops
MF_PATH_INCLUDE GIFTOPPM giftopnm giftoppm
MF_PATH_INCLUDE DJPEG djpeg
MF_PATH_INCLUDE PNGTOPNM pngtopnm
MF_PATH_INCLUDE SGITOPNM sgitopnm
MF_PATH_INCLUDE TIFFTOPNM tifftopnm
MF_PATH_INCLUDE BMPTOPPM bmptopnm bmptoppm
MF_PATH_INCLUDE RASTTOPNM rasttopnm
MF_PATH_INCLUDE PSTEXT pstext
MF_PATH_INCLUDE NENSCRIPT nenscript enscript
MF_PATH_INCLUDE FIG2DEV fig2dev
MF_PATH_INCLUDE ACROREAD acroread
 
# MF_PROG_GNU_ZCAT($ZCAT)

save_AC_PATH=$AC_PATH
AC_PATH=/usr/lib:/usr/sbin:/usr/bin:/bin:/sbin MF_PATH_INCLUDE SENDMAIL sendmail smail mail Mail
AC_PATH=$save_AC_PATH

AC_CHECK_HEADERS memory.h
AC_CHECK_HEADERS paths.h
AC_CHECK_HEADERS stdlib.h
AC_CHECK_HEADERS unistd.h
AC_HEADER_SYS_WAIT

AC_C_CONST
AC_TYPE_PID_T
AC_CHECK_FUNCS dup2
AC_CHECK_FUNCS tmpnam
AC_CHECK_FUNCS waitpid
AC_CHECK_FUNCS wait4
AC_CHECK_FUNCS unsetenv
AC_CHECK_FUNCS setenv

AC_SUB MAGICFILTER ${AC_EXEC}/magicfilter

AC_OUTPUT Makefile file/Makefile magicfilter.8 magicfilter.5 magicfilter.h magicfilter-t.5

