1#! /bin/sh
2
3# local options:  ac_help is the help message that describes them
4# and LOCAL_AC_OPTIONS is the script that interprets them.  LOCAL_AC_OPTIONS
5# is a script that's processed with eval, so you need to be very careful to
6# make certain that what you quote is what you want to quote.
7
8ac_help='--filterdir=DIR		where to put printer filters (prefix/sbin/printers)
9--traditional		build a more traditional sort of magicfilter
10--use-local-magic	install a private magic file
11--use-fifo		pipe the output from gs through a fifo
12--with-lprng		build a stupid filter for use with lprng
13--with-papersize=SIZE	set the default paper size for gs/pstext
14--with-pcl		use hp2pcl or lj2ps to print pcl on non-pcl printers'
15
16LOCAL_AC_OPTIONS='
17case X"$1" in
18X--filterdir=*)
19	    AC_FILTER=`echo "$1" | sed -e "s/^[^=]*=//"`
20	    shift 1
21	    ;;
22X--filterdir)
23	    AC_FILTER=$2
24	    shift 2
25	    ;;
26X--traditional)
27	    TARGET=traditional
28	    shift
29	    ;;
30*)	    ac_error=1
31	    ;;
32esac'
33
34# load in the configuration file
35#
36TARGET=magicfilter
37# #### configure.inc ####
38#   @(#) configure.inc 1.28@(#)
39#   Copyright (c) 1999 David Parsons. All rights reserved.
40#
41#   Redistribution and use in source and binary forms, with or without
42#   modification, are permitted provided that the following conditions
43#   are met:
44#  1. Redistributions of source code must retain the above copyright
45#     notice, this list of conditions and the following disclaimer.
46#  2. Redistributions in binary form must reproduce the above copyright
47#     notice, this list of conditions and the following disclaimer in
48#     the documentation and/or other materials provided with the
49#     distribution.
50#  3. My name may not be used to endorse or promote products derived
51#     from this software without specific prior written permission.
52#
53#  THIS SOFTWARE IS PROVIDED BY DAVID PARSONS ``AS IS'' AND ANY
54#  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
55#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56#  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
57#  PARSONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
59#  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
63#  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
64#  THE POSSIBILITY OF SUCH DAMAGE.
65#
66
67
68#
69# this preamble code is executed when this file is sourced and it picks
70# interesting things off the command line.
71#
72ac_default_path="/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin"
73
74ac_standard="--src=DIR		where the source lives (.)
75--prefix=DIR		where to install the final product (/usr/local)
76--execdir=DIR		where to put executables (prefix/bin)
77--libdir=DIR		where to put libraries (prefix/lib)
78--mandir=DIR		where to put manpages (prefix/man)
79--confdir=DIR		where to put configuration information (/etc)"
80
81__fail=exit
82
83if dirname B/A 2>/dev/null >/dev/null; then
84__ac_dirname() {
85    dirname "$1"
86}
87else
88__ac_dirname() {
89    echo "$1" | sed -e 's:/[^/]*$::'
90}
91fi
92
93ac_progname=$0
94ac_configure_command="$*"
95
96__d=`__ac_dirname "$ac_progname"`
97if [ "$__d" = "$ac_progname" ]; then
98    AC_SRCDIR=`pwd`
99else
100    AC_SRCDIR=`cd $__d;pwd`
101fi
102
103__ac_dir() {
104    if test -d "$1"; then
105	(cd "$1";pwd)
106    else
107	echo "$1";
108    fi
109}
110
111while [ $# -gt 0 ]; do
112    unset matched
113
114    case X"$1" in
115    X--src|X--srcdir)
116	AC_SRCDIR=`__ac_dir "$2"`
117	_set_srcdir=1
118	shift 2;;
119
120    X--src=*|X--srcdir=*)
121	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
122	AC_SRCDIR=`__ac_dir "$__d"`
123	_set_srcdir=1
124	shift 1 ;;
125
126    X--prefix)
127	AC_PREFIX=`__ac_dir "$2"`
128	_set_prefix=1
129	shift 2;;
130
131    X--prefix=*)
132	__d=`echo "$1"| sed -e 's/^[^=]*=//'`
133	AC_PREFIX=`__ac_dir "$__d"`
134	_set_prefix=1
135	shift 1;;
136
137    X--confdir)
138	AC_CONFDIR=`__ac_dir "$2"`
139	_set_confdir=1
140	shift 2;;
141
142    X--confdir=*)
143	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
144	AC_CONFDIR=`__ac_dir "$__d"`
145	_set_confdir=1
146	shift 1;;
147
148    X--lib|X--libdir)
149	AC_LIBDIR=`__ac_dir "$2"`
150	_set_libdir=1
151	shift 2;;
152
153    X--lib=*|X--libdir=*)
154	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
155	AC_LIBDIR=`__ac_dir "$__d"`
156	_set_libdir=1
157	shift 1;;
158
159    X--exec|X--execdir)
160	AC_EXECDIR=`__ac_dir "$2"`
161	_set_execdir=1
162	shift 2;;
163
164    X--exec=*|X--execdir=*)
165	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
166	AC_EXECDIR=`__ac_dir "$__d"`
167	_set_execdir=1
168	shift 1;;
169
170    X--man|X--mandir)
171	AC_MANDIR=`__ac_dir "$2"`
172	_set_mandir=1
173	shift 2;;
174
175    X--man=*|X--mandir=*)
176	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
177	AC_MANDIR=`__ac_dir "$__d"`
178	_set_mandir=1
179	shift 1;;
180
181    X--use-*=*)
182	_var=`echo "$1"| sed -n 's/^--use-\([A-Za-z][-A-Za-z0-9_]*\)=.*$/\1/p'`
183	if [ "$_var" ]; then
184	    _val=`echo "$1" | sed -e 's/^--use-[^=]*=\(.*\)$/\1/'`
185	    _v=`echo $_var | tr '[a-z]' '[A-Z]' | tr '-' '_'`
186	    case X"$_val" in
187	    X[Yy][Ee][Ss]|X[Tt][Rr][Uu][Ee]) eval USE_${_v}=T ;;
188	    X[Nn][Oo]|X[Ff][Aa][Ll][Ss][Ee]) eval unset USE_${_v} ;;
189	    *) echo "Bad value for --use-$_var ; must be yes or no"
190	       exit 1 ;;
191	    esac
192	else
193	    echo "Bad option $1.   Use --help to show options" 1>&2
194	    exit 1
195	fi
196	shift 1 ;;
197
198    X--use-*)
199        _var=`echo "$1"|sed -n 's/^--use-\([A-Za-z][-A-Za-z0-9_]*\)$/\1/p'`
200	_v=`echo $_var | tr '[a-z]' '[A-Z]' | tr '-' '_'`
201	eval USE_${_v}=T
202	shift 1;;
203
204    X--with-*=*)
205	_var=`echo "$1"| sed -n 's/^--with-\([A-Za-z][-A-Za-z0-9_]*\)=.*$/\1/p'`
206	if [ "$_var" ]; then
207	    _val=`echo "$1" | sed -e 's/^--with-[^=]*=\(.*\)$/\1/'`
208	    _v=`echo $_var | tr '[a-z]' '[A-Z]' | tr '-' '_'`
209	    eval WITH_${_v}=$_val
210	else
211	    echo "Bad option $1.   Use --help to show options" 1>&2
212	    exit 1
213	fi
214	shift 1 ;;
215
216    X--with-*)
217	_var=`echo "$1" | sed -n 's/^--with-\([A-Za-z][A-Za-z0-9_-]*\)$/\1/p'`
218	if [ "$_var" ]; then
219	    _v=`echo $_var | tr '[a-z]' '[A-Z]' | tr '-' '_'`
220	    eval WITH_${_v}=1
221	else
222	    echo "Bad option $1.   Use --help to show options" 1>&2
223	    exit 1
224	fi
225	shift 1 ;;
226
227    X--help)
228	echo "$ac_standard"
229	test "$ac_help" && echo "$ac_help"
230	exit 0;;
231
232    *)	if [ "$LOCAL_AC_OPTIONS" ]; then
233	    eval "$LOCAL_AC_OPTIONS"
234	else
235	    ac_error=T
236	fi
237	if [ "$ac_error" ]; then
238	    echo "Bad option $1.   Use --help to show options" 1>&2
239	    exit 1
240	fi ;;
241    esac
242done
243
244
245#
246# echo w/o newline
247#
248echononl()
249{
250    ${ac_echo:-echo} "${@}$ac_echo_nonl"
251}
252
253#
254# log something to the terminal and to a logfile.
255#
256LOG () {
257    echo "$@"
258    echo "$@" 1>&5
259}
260
261#
262# log something to the terminal without a newline, and to a logfile with
263# a newline
264#
265LOGN () {
266    echononl "$@" 1>&5
267    echo "$@"
268}
269
270#
271# log something to the terminal
272#
273TLOG () {
274    echo "$@" 1>&5
275}
276
277#
278# log something to the terminal, no newline
279#
280TLOGN () {
281    echononl "$@" 1>&5
282}
283
284
285#
286# AC_CONTINUE tells configure not to bomb if something fails, but to
287# continue blithely along
288#
289AC_CONTINUE () {
290    __fail="return"
291}
292
293#
294# Emulate gnu autoconf's AC_CHECK_HEADERS() function
295#
296AC_CHECK_HEADERS () {
297    AC_PROG_CC
298
299    echo "/* AC_CHECK_HEADERS */" > /tmp/ngc$$.c
300    for hdr in $*; do
301	echo "#include <$hdr>" >> /tmp/ngc$$.c
302    done
303    echo "main() { }" >> /tmp/ngc$$.c
304
305    test "$_sssh" || LOGN "checking for header $hdr"
306
307    if $AC_CC -o /tmp/ngc$$ /tmp/ngc$$.c; then
308	AC_DEFINE 'HAVE_'`echo $hdr | tr 'a-z' 'A-Z' | tr './' '_'` 1
309	test "$_sssh" || TLOG " (found)"
310	rc=0
311    else
312	test "$_sssh" || TLOG " (not found)"
313	rc=1
314    fi
315    unset _sssh
316    rm -f /tmp/ngc$$.c /tmp/ngc$$
317    return $rc
318}
319
320
321#
322# emulate GNU autoconf's AC_CHECK_FUNCS function
323#
324AC_CHECK_FUNCS () {
325    AC_PROG_CC
326
327    cat >> /tmp/ngc$$.c << EOF
328main()
329{
330    $1();
331}
332EOF
333
334    test "$_sssh" || LOGN "checking for the $1 function"
335
336    if $AC_CC -o /tmp/ngc$$ /tmp/ngc$$.c $LIBS; then
337	AC_DEFINE `echo ${2:-HAVE_$1} | tr 'a-z' 'A-Z'` 1
338	test "$_sssh" || TLOG " (found)"
339	rc=0
340    else
341	echo "offending command was:"
342	cat /tmp/ngc$$.c
343	echo "$AC_CC -o /tmp/ngc$$ /tmp/ngc$$.c $LIBS"
344	test "$_sssh" || TLOG " (not found)"
345	rc=1
346    fi
347    rm -f /tmp/ngc$$.c /tmp/ngc$$
348    unset _sssh
349    return $rc
350}
351
352
353#
354# check to see if some structure contains a field
355#
356# usage: AC_CHECK_FIELD structure field {include ...}
357#
358AC_CHECK_FIELD () {
359    AC_PROG_CC
360
361    struct=$1
362    field=$2
363    shift 2
364
365    rm -f /tmp/ngc$$.c
366
367    for include in $*;do
368	echo "#include <$include>" >> /tmp/ngc$$.c
369    done
370
371    cat >> /tmp/ngc$$.c << EOF
372main()
373{
374    struct $struct foo;
375
376    foo.$field;
377}
378EOF
379
380    test "$_sssh" || LOGN "checking that struct $struct has a $field field"
381
382    if $AC_CC -o /tmp/ngc$$ /tmp/ngc$$.c $AC_LIBS 2>>config.log; then
383	AC_DEFINE HAVE_`echo ${struct}_$field | tr 'a-z' 'A-Z'`
384	test "$_sssh" || TLOG " (yes)"
385	rc=0
386    else
387	test "$_sssh" || TLOG " (no)"
388	rc=1
389    fi
390    unset _sssh
391    rm -f /tmp/ngc$$.c /tmp/ngc$$
392    return $rc
393}
394
395
396#
397# check that the C compiler works
398#
399AC_PROG_CC () {
400    test "$AC_CC" && return 0
401
402    cat > /tmp/ngc$$.c << \EOF
403#include <stdio.h>
404main()
405{
406    puts("hello, sailor");
407}
408EOF
409
410    TLOGN "checking the C compiler"
411
412    AC_CC=${CC:-`acLookFor cc`}
413
414    if [ ! "$AC_CC" ]; then
415	TLOG " (no C compiler found)"
416	$__fail 1
417    fi
418    echo "checking out the C compiler"
419
420    $AC_CC -o /tmp/ngc$$ /tmp/ngc$$.c
421    status=$?
422
423    TLOGN " ($AC_CC)"
424    if [ $status -eq 0 ]; then
425	TLOG " ok"
426
427	# check that the CFLAGS and LDFLAGS aren't bogus
428
429	unset AC_CFLAGS AC_LDFLAGS
430
431	if [ "$CFLAGS" ]; then
432	    test "$CFLAGS" && echo "validating CFLAGS=${CFLAGS}"
433	    if $AC_CC $CFLAGS -o /tmp/ngc$$.o /tmp/ngc$$.c ; then
434		AC_CFLAGS=${CFLAGS:-"-g"}
435		test "$CFLAGS" && echo "CFLAGS=\"${CFLAGS}\" are okay"
436	    elif [ "$CFLAGS" ]; then
437		echo "ignoring bogus CFLAGS=\"${CFLAGS}\""
438	    fi
439	else
440	    AC_CFLAGS=-g
441	fi
442	if [ "$LDFLAGS" ]; then
443	    test "$LDFLAGS" && echo "validating LDFLAGS=${LDFLAGS}"
444	    if $AC_CC $LDFLAGS -o /tmp/ngc$$ /tmp/ngc$$.o; then
445		AC_LDFLAGS=${LDFLAGS:-"-g"}
446		test "$LDFLAGS" && TLOG "LDFLAGS=\"${LDFLAGS}\" are okay"
447	    elif [ "$LDFLAGS" ]; then
448		TLOG "ignoring bogus LDFLAGS=\"${LDFLAGS}\""
449	    fi
450	else
451	    AC_LDFLAGS=${CFLAGS:-"-g"}
452	fi
453	AC_SUB 'CFLAGS' "$AC_CFLAGS"
454	AC_SUB 'LDFLAGS' "$AC_LDFLAGS"
455    else
456	AC_SUB 'CFLAGS' ''
457	AC_SUB 'LDFLAGS' ''
458	AC_FAIL " does not compile code properly"
459    fi
460
461    AC_SUB 'CC' "$AC_CC"
462
463    rm -f /tmp/ngc$$ /tmp/ngc$$.c /tmp/ngc$$.o
464
465    return $status
466}
467
468
469#
470# acLookFor actually looks for a program, without setting anything.
471#
472acLookFor () {
473    path=${AC_PATH:-$ac_default_path}
474    case "X$1" in
475    X-[rx]) __mode=$1
476	    shift
477	    ;;
478    *)	    __mode=-x
479	    ;;
480    esac
481    oldifs="$IFS"
482    for program in $*; do
483	IFS=":"
484	for x in $path; do
485	    if [ $__mode $x/$program -a -f $x/$program ]; then
486		echo $x/$program
487		break 2
488	    fi
489	done
490    done
491    IFS="$oldifs"
492    unset __mode
493}
494
495
496#
497# check that a program exists and set its path
498#
499MF_PATH_INCLUDE () {
500    SYM=$1; shift
501
502    case X$1 in
503    X-[rx]) __mode=$1
504	    shift
505	    ;;
506    *)      unset __mode
507	    ;;
508    esac
509
510    TLOGN "looking for $1"
511
512    DEST=`acLookFor $__mode $*`
513
514    __sym=`echo "$SYM" | tr '[a-z]' '[A-Z]'`
515    if [ "$DEST" ]; then
516	TLOG " ($DEST)"
517	echo "$1 is $DEST"
518	AC_MAK $SYM
519	AC_DEFINE PATH_$__sym \""$DEST"\"
520	AC_SUB $__sym "$DEST"
521	eval CF_$SYM=$DEST
522	return 0
523    else
524	#AC_SUB $__sym ''
525	echo "$1 is not found"
526	TLOG " (not found)"
527	return 1
528    fi
529}
530
531
532#
533# AC_INIT starts the ball rolling
534#
535# After AC_INIT, fd's 1 and 2 point to config.log
536# and fd 5 points to what used to be fd 1
537#
538AC_INIT () {
539    __config_files="config.cmd config.sub config.h config.mak config.log"
540    rm -f $__config_files
541    __cwd=`pwd`
542    exec 5>&1 1>$__cwd/config.log 2>&1
543    AC_CONFIGURE_FOR=__AC_`echo $1 | sed -e 's/\..$//' | tr 'a-z' 'A-Z' | tr ' ' '_'`_D
544
545    # check to see whether to use echo -n or echo ...\c
546    #
547    echo -n hello > $$
548    echo world >> $$
549    if grep "helloworld" $$ >/dev/null; then
550	ac_echo="echo -n"
551	echo "[echo -n] works"
552    else
553	ac_echo="echo"
554	echo 'hello\c' > $$
555	echo 'world' >> $$
556	if grep "helloworld" $$ >/dev/null; then
557	    ac_echo_nonl='\c'
558	    echo "[echo ...\\c] works"
559	fi
560    fi
561    rm -f $$
562
563    LOG "Configuring for [$1]"
564
565    cat > $__cwd/config.h << EOF
566/*
567 * configuration for $1${2:+" ($2)"}, generated `date`
568 * by ${LOGNAME:-`whoami`}@`hostname`
569 */
570#ifndef $AC_CONFIGURE_FOR
571#define $AC_CONFIGURE_FOR 1
572
573
574EOF
575
576    AC_PREFIX=${AC_PREFIX:-/usr/local}
577    AC_EXECDIR=${AC_EXECDIR:-$AC_PREFIX/bin}
578    AC_LIBDIR=${AC_LIBDIR:-$AC_PREFIX/lib}
579    AC_MANDIR=${AC_MANDIR:-$AC_PREFIX/man}
580    AC_CONFDIR=${AC_CONFDIR:-/etc}
581
582    AC_PATH=${WITH_PATH:-$PATH}
583    AC_PROG_CPP
584    AC_PROG_INSTALL
585
586    ac_os=`uname -s`
587    _os=`echo $ac_os | tr '[a-z]' '[A-Z]'`
588    AC_DEFINE OS_$_os	1
589    eval OS_${_os}=1
590    unset _os
591}
592
593
594#
595# AC_LIBRARY checks to see if a given library exists and contains the
596# given function.
597# usage: AC_LIBRARY function library [alternate ...]
598#
599AC_LIBRARY() {
600    SRC=$1
601    shift
602
603    # first see if the function can be found in any of the
604    # current libraries
605    _sssh=T
606    AC_CHECK_FUNCS $SRC && return 0
607
608    # then search through the list of libraries
609    __libs="$LIBS"
610    for x in $*; do
611	_sssh=T
612	LIBS="$__libs $x"
613	if AC_CHECK_FUNCS $SRC; then
614	    AC_LIBS="$AC_LIBS $x"
615	    return 0
616	fi
617    done
618    return 1
619}
620
621
622#
623# AC_PROG_LEX checks to see if LEX exists, and if it's lex or flex.
624#
625AC_PROG_LEX() {
626    TLOGN "looking for lex "
627
628    DEST=`acLookFor lex`
629    if [ "$DEST" ]; then
630	AC_MAK LEX
631	AC_DEFINE PATH_LEX \"$DEST\"
632	AC_SUB 'LEX' "$DEST"
633	echo "lex is $DEST"
634    else
635	DEST=`acLookFor flex`
636	if [ "$DEST" ]; then
637	    AC_MAK FLEX
638	    AC_DEFINE 'LEX' \"$DEST\"
639	    AC_SUB 'LEX', "$DEST"
640	    echo "lex is $DEST"
641	else
642	    AC_SUB LEX ''
643	    echo "neither lex or flex found"
644	    TLOG " (not found)"
645	    return 1
646	fi
647    fi
648
649    if AC_LIBRARY yywrap -ll -lfl; then
650	TLOG "($DEST)"
651	return 0
652    fi
653    TLOG "(no lex library found)"
654    return 1
655}
656
657
658#
659# AC_PROG_YACC checks to see if YACC exists, and if it's bison or
660# not.
661#
662AC_PROG_YACC () {
663
664    TLOGN "looking for yacc "
665
666    DEST=`acLookFor yacc`
667    if [ "$DEST" ]; then
668	AC_MAK YACC
669	AC_DEFINE PATH_YACC \"$DEST\"
670	AC_SUB 'YACC' "$DEST"
671	TLOG "($DEST)"
672	echo "yacc is $DEST"
673    else
674	DEST=`acLookFor bison`
675	if [ "$DEST" ]; then
676	    AC_MAK BISON
677	    AC_DEFINE 'YACC' \"$DEST\"
678	    AC_SUB 'YACC' "$DEST -y"
679	    echo "yacc is $DEST -y"
680	    TLOG "($DEST -y)"
681	else
682	    AC_SUB 'YACC' ''
683	    echo "neither yacc or bison found"
684	    TLOG " (not found)"
685	    return 1
686	fi
687    fi
688    return 0
689}
690
691
692#
693# AC_PROG_LN_S checks to see if ln exists, and, if so, if ln -s works
694#
695AC_PROG_LN_S () {
696    test "$AC_FIND_PROG" || AC_PROG_FIND
697
698    test "$AC_FIND_PROG" || return 1
699
700    TLOGN "looking for \"ln -s\""
701    DEST=`acLookFor ln`
702
703    if [ "$DEST" ]; then
704	rm -f /tmp/b$$
705	$DEST -s /tmp/a$$ /tmp/b$$
706	if [ "`$AC_FIND_PROG /tmp/b$$ -type l -print`" ]; then
707	    TLOG " ($DEST)"
708	    echo "$DEST exists, and ln -s works"
709	    AC_SUB 'LN_S' "$DEST -s"
710	    rm -f /tmp/b$$
711	else
712	    AC_SUB 'LN_S' ''
713	    TLOG " ($DEST exists, but -s does not seem to work)"
714	    echo "$DEST exists, but ln -s doesn't seem to work"
715	    rm -f /tmp/b$$
716	    return 1
717	fi
718    else
719	AC_SUB 'LN_S' ''
720	echo "ln not found"
721	TLOG " (not found)"
722	return 1
723    fi
724}
725
726
727#
728# AC_PROG_FIND looks for the find program and sets the FIND environment
729# variable
730#
731AC_PROG_FIND () {
732    if test -z "$AC_FIND_PROG"; then
733	MF_PATH_INCLUDE FIND find
734	rc=$?
735	AC_FIND_PROG=$DEST
736	return $rc
737    fi
738    return 0
739}
740
741
742#
743# AC_PROG_SED looks for the sed program and sets the SED environment
744# variable
745#
746AC_PROG_SED () {
747    if test -z "$AC_SED_PROG"; then
748	MF_PATH_INCLUDE SED sed
749	rc=$?
750	AC_SED_PROG=$DEST
751	return $rc
752    fi
753    return 0
754}
755
756
757#
758# AC_HEADER_SYS_WAIT looks for sys/wait.h
759#
760AC_HEADER_SYS_WAIT () {
761    AC_CHECK_HEADERS sys/wait.h || return 1
762}
763
764#
765# AC_TYPE_PID_T checks to see if the pid_t type exists
766#
767AC_TYPE_PID_T () {
768    cat > /tmp/pd$$.c << EOF
769#include <sys/types.h>
770main() { pid_t me; }
771EOF
772
773    LOGN "checking for pid_t"
774
775    if $AC_CC -c /tmp/pd$$.c -o /tmp/pd$$.o; then
776	TLOG " (found)"
777	rc=0
778    else
779	echo "typedef int pid_t;" >> $__cwd/config.h
780	TLOG " (not found)"
781	rc=1
782    fi
783    rm -f /tmp/pd$$.o /tmp/pd$$.c
784    return $rc
785}
786
787
788#
789# AC_C_CONST checks to see if the compiler supports the const keyword
790#
791AC_C_CONST () {
792    cat > /tmp/pd$$.c << EOF
793const char me=1;
794EOF
795    LOGN "checking for \"const\" keyword"
796
797    if $AC_CC -c /tmp/pd$$.c -o /tmp/pd$$.o; then
798	TLOG " (yes)"
799	rc=0
800    else
801	AC_DEFINE 'const' '/**/'
802	TLOG " (no)"
803	rc=1
804    fi
805    rm -f /tmp/pd$$.o /tmp/pd$$.c
806    return $rc
807}
808
809
810#
811# AC_OUTPUT generates makefiles from makefile.in's
812#
813AC_OUTPUT () {
814    cd $__cwd
815    AC_SUB 'LIBS' "$AC_LIBS"
816    AC_SUB 'CONFIGURE_FILES' "$__config_files"
817    AC_SUB 'GENERATED_FILES' "$*"
818    AC_SUB 'srcdir'  "$AC_SRCDIR"
819    AC_SUB 'prefix'  "$AC_PREFIX"
820    AC_SUB 'exedir'  "$AC_EXECDIR"
821    AC_SUB 'libdir'  "$AC_LIBDIR"
822    AC_SUB 'confdir' "$AC_CONFDIR"
823    AC_SUB 'mandir'  "$AC_MANDIR"
824
825    if [ -r config.sub ]; then
826	test "$AC_SED_PROG" || AC_PROG_SED
827	test "$AC_SED_PROG" || return 1
828
829	echo                                   >> config.h
830	echo "#endif/* ${AC_CONFIGURE_FOR} */" >> config.h
831
832	rm -f config.cmd
833	Q=\'
834	cat - > config.cmd << EOF
835#! /bin/sh
836${CC:+CC=${Q}${CC}${Q}} ${CFLAGS:+CFLAGS=${Q}${CFLAGS}${Q}} $ac_progname $ac_configure_command
837EOF
838	chmod +x config.cmd
839
840	__d=$AC_SRCDIR
841	for makefile in $*;do
842	    if test -r $__d/${makefile}.in; then
843		LOG "generating $makefile"
844		./config.md `__ac_dirname ./$makefile` 2>/dev/null
845		$AC_SED_PROG -f config.sub < $__d/${makefile}.in > $makefile
846		__config_files="$__config_files $makefile"
847	    else
848		LOG "WARNING: ${makefile}.in does not exist!"
849	    fi
850	done
851	unset __d
852
853    else
854	echo
855    fi
856}
857
858
859#
860# AC_PROG_INSTALL finds the install program and guesses whether it's a
861# Berkeley or GNU install program
862#
863AC_PROG_INSTALL () {
864
865    DEST=`acLookFor install`
866
867    LOGN "checking for install"
868    unset IS_BSD
869    if [ "$DEST" ]; then
870	# BSD install or GNU install?  Let's find out...
871	touch /tmp/a$$
872
873	$DEST /tmp/a$$ /tmp/b$$
874
875	if test -r /tmp/a$$; then
876	    LOG " ($DEST)"
877	else
878	    IS_BSD=1
879	    LOG " ($DEST) bsd install"
880	fi
881	rm -f /tmp/a$$ /tmp/b$$
882    else
883	DEST=`acLookFor ginstall`
884	if [ "$DEST" ]; then
885	    LOG " ($DEST)"
886	else
887	    DEST="false"
888	    LOG " (not found)"
889	fi
890    fi
891
892    if [ "$IS_BSD" ]; then
893	PROG_INSTALL="$DEST -c"
894    else
895	PROG_INSTALL="$DEST"
896    fi
897
898    AC_SUB 'INSTALL' "$PROG_INSTALL"
899    AC_SUB 'INSTALL_PROGRAM' "$PROG_INSTALL -s -m 755"
900    AC_SUB 'INSTALL_DATA' "$PROG_INSTALL -m 444"
901
902    # finally build a little directory installer
903    # if mkdir -p works, use that, otherwise use install -d,
904    # otherwise build a script to do it by hand.
905    # in every case, test to see if the directory exists before
906    # making it.
907
908    if mkdir -p $$a/b; then
909	# I like this method best.
910	__mkdir="mkdir -p"
911	rmdir $$a/b
912	rmdir $$a
913    elif $PROG_INSTALL -d $$a/b; then
914	__mkdir="$PROG_INSTALL -d"
915	rmdir $$a/b
916	rmdir $$a
917    fi
918
919    __generated_files="$__generated_files $__cwd/config.md"
920    AC_SUB 'INSTALL_DIR' "$__cwd/config.md"
921    echo "#! /bin/sh"                                   > $__cwd/config.md
922    echo "# script generated" `date` "by configure.sh" >> $__cwd/config.md
923    echo                                               >> $__cwd/config.md
924    if [ "$__mkdir" ]; then
925	echo "test -d \"\$1\" || $__mkdir \"\$1\""     >> $__cwd/config.md
926	echo "exit $?"                                 >> $__cwd/config.md
927    else
928	cat - >> $__cwd/config.md << \EOD
929pieces=`IFS=/; for x in $1; do echo $x; done`
930dir=
931for x in $pieces; do
932    dir="$dir$x"
933    mkdir $dir || exit 1
934    dir="$dir/"
935done
936exit 0
937EOD
938    fi
939    chmod +x $__cwd/config.md
940}
941
942#
943# acCheckCPP is a local that runs a C preprocessor with a given set of
944# compiler options
945#
946acCheckCPP () {
947	cat > /tmp/ngc$$.c << EOF
948#define FOO BAR
949
950FOO
951EOF
952
953    if $1 $2 /tmp/ngc$$.c > /tmp/ngc$$.o; then
954	if grep -v '#define' /tmp/ngc$$.o | grep -s BAR >/dev/null; then
955	    echo "CPP=[$1], CPPFLAGS=[$2]"
956	    AC_SUB 'CPP' "$1"
957	    AC_SUB 'CPPFLAGS' "$2"
958	    rm /tmp/ngc$$.c /tmp/ngc$$.o
959	    return 0
960	fi
961    fi
962    rm /tmp/ngc$$.c /tmp/ngc$$.o
963    return 1
964}
965
966
967#
968# AC_PROG_CPP checks for cpp, then checks to see which CPPFLAGS are needed
969# to run it as a filter.
970#
971AC_PROG_CPP () {
972    if [ "$AC_CPP_PROG" ]; then
973	DEST=$AC_CPP_PROG
974    else
975	__ac_path="$AC_PATH"
976	AC_PATH="/lib:/usr/lib:${__ac_path:-$ac_default_path}"
977	DEST=`acLookFor cpp`
978	AC_PATH="$__ac_path"
979    fi
980
981    unset fail
982    LOGN "Looking for cpp"
983    if [ "$DEST" ]; then
984	TLOGN " ($DEST)"
985	acCheckCPP $DEST "$CPPFLAGS" || \
986		 acCheckCPP $DEST -traditional-cpp -E || \
987		 acCheckCPP $DEST -E || \
988		 acCheckCPP $DEST -traditional-cpp -pipe || \
989	         acCheckCPP $DEST -pipe || fail=1
990
991	if [ "$fail" ]; then
992	    AC_FAIL " (can't run cpp as a pipeline)"
993	else
994	    TLOG " ok"
995	    return 0
996	fi
997    fi
998    AC_FAIL " (not found)"
999}
1000
1001#
1002# AC_FAIL spits out an error message, then __fail's
1003AC_FAIL() {
1004    LOG "$*"
1005    $__fail 1
1006}
1007
1008#
1009# AC_SUB writes a substitution into config.sub
1010AC_SUB() {
1011    (   echononl "s;@$1@;"
1012	_subst=`echo $2 | sed -e 's/;/\\;/g'`
1013	echononl "$_subst"
1014	echo ';g' ) >> $__cwd/config.sub
1015}
1016
1017#
1018# AC_MAK writes a define into config.mak
1019AC_MAK() {
1020    echo "HAVE_$1 = 1" >> $__cwd/config.mak
1021}
1022
1023#
1024# AC_DEFINE adds a #define to config.h
1025AC_DEFINE() {
1026    echo "#define $1 ${2:-1}" >> $__cwd/config.h
1027}
1028
1029#
1030# AC_CONFIG adds a configuration setting to all the config files
1031AC_CONFIG() {
1032    AC_DEFINE "PATH_$1" \""$2"\"
1033    AC_MAK "$1"
1034    AC_SUB "$1" "$2"
1035}
1036# #### configure.sh #####
1037
1038# and away we go
1039#
1040AC_INIT magicfilter
1041
1042AC_PROG_CC
1043
1044AC_SUB filterdir ${AC_FILTER:-$AC_PREFIX/sbin/printers}
1045
1046if [ "$USE_FIFO" = "T" ]; then
1047    AC_SUB GSOUT '${FIFO}'
1048else
1049    AC_SUB GSOUT '-'
1050fi
1051
1052BUILD_MAGIC=T
1053if [ "$USE_LOCAL_MAGIC" ]; then
1054    LOG "Using private magic file $AC_CONFDIR/mf.magic"
1055else
1056    if AC_LIBRARY magic_open -lmagic; then
1057	LOG "Your system has a modern libmagic.  We'll use it."
1058	unset BUILD_MAGIC
1059    elif AC_LIBRARY magic_open -lmagic -lz; then
1060	LOG "Your system has a modern libmagic.  We'll use it."
1061	AC_LIBS="$AC_LIBS -lz"
1062	unset BUILD_MAGIC
1063    fi
1064    unset BUILD_MAGIC
1065
1066    if [ "$BUILD_MAGIC" ]; then
1067	case $ac_os in
1068	[Ff]ree[Bb][Ss][Dd])magicpath=/etc:/usr/etc:/usr/share/misc ;;
1069	*)			magicpath=/etc:/usr/etc: ;;
1070	esac
1071
1072	# check to see if the system magic file is moderately recent
1073	#
1074	trap "rm -f $$" 1 2 3 9 15
1075	echo "@PJL JOB" > $$
1076	F=`file $$ 2>/dev/null | grep -i pjl`
1077	echo "%PDF-1.1" > $$
1078	G=`file $$ 2>/dev/null | grep -i pdf`
1079
1080	if [ "$F" -a "$G" ]; then
1081	    save_AC_PATH=$AC_PATH
1082	    AC_PATH=$magicpath MF_PATH_INCLUDE MAGIC -r magic || USE_LOCAL_MAGIC=T
1083	    AC_PATH=$save_AC_PATH
1084	else
1085	    LOG "file(1) is too old -- using private magic file"
1086	    USE_LOCAL_MAGIC=T
1087	fi
1088    fi
1089fi
1090rm -f $$
1091trap 1 2 3 9 15
1092
1093if [ "$USE_LOCAL_MAGIC" ]; then
1094    # if we're using local magic, manually write the substitution
1095    # information into the config files
1096    AC_CONFIG MAGIC "$AC_CONFDIR"/mf.magic
1097    AC_SUB INSTALL_MAGIC "$PROG_INSTALL"
1098else
1099    AC_DEFINE PATH_MAGIC 0
1100    AC_SUB INSTALL_MAGIC ":"
1101fi
1102if [ "$BUILD_MAGIC" ]; then
1103    AC_SUB LIBMAGIC file/libmagic.a
1104    AC_SUB MAGIC_HEADER "-I$AC_SRCDIR/file"
1105    AC_SUB MAGIC_LIB    "-Lfile"
1106    AC_SUB MAKE_MAGIC	"cd file \&\& make"
1107    AC_SUB CFMAGIC	"cfmagic"
1108else
1109    AC_SUB LIBMAGIC ""
1110    AC_SUB MAGIC_HEADER "-I${LOCALBASE}/include"
1111    AC_SUB MAGIC_LIB "-L${LOCALBASE}/lib -Wl,-rpath -Wl,${LOCALBASE}/lib"
1112    AC_SUB MAKE_MAGIC ":"
1113    AC_SUB CFMAGIC	""
1114fi
1115
1116AC_SUB DO_WHAT install-$TARGET
1117
1118# AC_PROG_LN_S
1119# AC_PROG_YACC
1120
1121if [ ! "$WITH_LPRNG" ]; then
1122    S=`lpq -V 2>/dev/null | head -1 | grep -i lprng`
1123
1124    if [ "$S" ]; then
1125	LOG "Found LPRng -- building stupid filters"
1126	WITH_LPRNG=1
1127    fi
1128fi
1129if [ "$WITH_LPRNG" ]; then
1130    AC_SUB    "LPD_OPTS" "'s/^LPNG//p'"
1131    AC_DEFINE "WITH_LPRNG" "1"
1132else
1133    AC_SUB    "LPD_OPTS" "'s/^LPR //p'"
1134
1135    # don't bother to check for options.h unless we're not
1136    # being built against lprng -- remember that magicfilter
1137    # for lprng doesn't have any options.
1138    #
1139    if AC_CHECK_HEADERS basis/options.h; then
1140	if LIBS="-lbasis" AC_CHECK_FUNCS x_getopt; then
1141	    AC_LIBS="$AC_LIBS -lbasis"
1142	    HAVE_XGETOPT=T
1143	fi
1144    fi
1145fi
1146if [ "$WITH_LPRNG" -o "$HAVE_XGETOPT" ]; then
1147    AC_SUB XGETOPT ''
1148else
1149    AC_SUB XGETOPT options.o
1150fi
1151
1152AC_CHECK_HEADERS malloc.h || AC_CHECK_HEADERS sys/malloc.h
1153
1154AC_CHECK_FUNCS basename
1155
1156MF_PATH_INCLUDE RANLIB ranlib true || AC_CONFIG RANLIB ':'
1157MF_PATH_INCLUDE M4 m4 || AC_FAIL "magicfilter requires m4"
1158
1159MF_PATH_INCLUDE GS gs
1160if MF_PATH_INCLUDE NENSCRIPT nenscript enscript; then
1161    TLOGN "checking if $CF_NENSCRIPT supports the -q flag..."
1162    __v=`$CF_NENSCRIPT -q -p - </dev/null`
1163
1164    if [ \( "$?" -ne 0 \) -o "$__v" ]; then
1165	TLOG " no"
1166	AC_SUB NENSCRIPT_QUIET ''
1167    else
1168	TLOG " yes"
1169	AC_SUB NENSCRIPT_QUIET -q
1170    fi
1171else
1172    AC_SUB NENSCRIPT_QUIET ''
1173    MF_PATH_INCLUDE PSTEXT pstext
1174fi
1175
1176# if the user is brave, look for a pcl to generic format converter
1177#
1178test "$WITH_PCL" && MF_PATH_INCLUDE UNPCL hp2pbm lj2ps
1179
1180case X"$CF_UNPCL" in
1181X*hp2pbm)  AC_SUB UNPCL_FLAGS '-p' ;;
1182X*)        AC_SUB UNPCL_FLAGS '' ;;
1183esac
1184
1185paper=$WITH_PAPERSIZE
1186
1187if [ "$CF_GS" -a "$paper" ]; then
1188    if ! echo "quit" | $CF_GS -sPAPERSIZE=$paper -dNODISPLAY -; then
1189	AC_FAIL "$CF_GS cannot write to $paper sized paper"
1190	unset paper
1191    fi
1192fi
1193
1194if [ "${CF_PSTEXT:-$CF_NENSCRIPT}" -a "$paper" ]; then
1195    PROG=${CF_PSTEXT:-$CF_NENSCRIPT}
1196    if ! $PROG -T$paper </dev/null; then
1197	AC_FAIL "$PROG cannot write to $paper sized paper"
1198	unset paper
1199    fi
1200fi
1201
1202test "$paper" && AC_DEFINE PAPERSIZE \"$WITH_PAPERSIZE\"
1203
1204MF_PATH_INCLUDE GZIP gzip gzcat || MF_PATH_INCLUDE ZCAT zcat
1205MF_PATH_INCLUDE BZIP bzip2
1206MF_PATH_INCLUDE UNCOMPRESS uncompress
1207MF_PATH_INCLUDE DVIPS dvips
1208MF_PATH_INCLUDE PNMTOPS pnmtops
1209MF_PATH_INCLUDE GIFTOPPM giftopnm giftoppm
1210MF_PATH_INCLUDE G3TOPBM g3topbm
1211MF_PATH_INCLUDE DJPEG djpeg
1212MF_PATH_INCLUDE PNGTOPNM pngtopnm
1213MF_PATH_INCLUDE SGITOPNM sgitopnm
1214MF_PATH_INCLUDE TIFFTOPNM tifftopnm
1215MF_PATH_INCLUDE BMPTOPPM bmptopnm bmptoppm
1216MF_PATH_INCLUDE RASTTOPNM rasttopnm
1217MF_PATH_INCLUDE FIG2DEV fig2dev
1218MF_PATH_INCLUDE ACROREAD acroread
1219
1220# MF_PROG_GNU_ZCAT($ZCAT)
1221
1222#save_AC_PATH=$AC_PATH
1223#AC_PATH=/usr/lib:/usr/sbin:/usr/bin:/bin:/sbin MF_PATH_INCLUDE SENDMAIL sendmail smail mail Mail
1224#AC_PATH=$save_AC_PATH
1225
1226AC_CHECK_HEADERS memory.h
1227AC_CHECK_HEADERS paths.h
1228AC_CHECK_HEADERS stdlib.h
1229AC_CHECK_HEADERS unistd.h
1230AC_HEADER_SYS_WAIT
1231
1232AC_C_CONST
1233AC_TYPE_PID_T
1234AC_CHECK_FUNCS dup2
1235AC_CHECK_FUNCS tmpnam
1236AC_CHECK_FUNCS waitpid
1237AC_CHECK_FUNCS wait4
1238AC_CHECK_FUNCS unsetenv
1239AC_CHECK_FUNCS setenv
1240AC_CHECK_FUNCS setlinebuf
1241
1242AC_SUB MAGICFILTER ${AC_EXEC}/magicfilter
1243AC_SUB VERSION `test -f VERSION && cat VERSION`
1244AC_DEFINE STD_PATH "\"${AC_PATH}\""
1245
1246AC_OUTPUT Makefile file/Makefile magicfilter.8templ magicfilter.5 magicfilter.h magicfilter-t.5 magic.m4 contrib/mfconfig.pl args.c
1247
1248