#! /bin/sh

# configure script for apsfilter
# $ApsCVS: src/apsfilter/configure,v 1.1.2.8 2002/01/10 20:44:48 andreas Exp $

# these are the ".in" files
DOT_IN_FILES="\
		HOWTO-BUGREPORTS \
		SETUP \
		Makefile \
		bin/apsfilter \
		bin/aps2file \
		bin/apsfilter-bug \
		bin/apspreview \
		doc/handbook.html \
		man/apsfilter.1 \
		man/apsfilterrc.5 \
		man/aps2file.1 \
		man/apspreview.1 \
		man/apsfilter-bug.1 \
		setup/test.ps \
		template/apsfilterrc \
		"

prefix_default=/usr/local
old_ifs="${IFS= 	}" # one space and one tab character
confopts="$@"

unset prefix bindir datadir sysconfdir mandir docdir with_shell with_awk
unset with_sendmail quiet with_printcap with_spooldir

for option in "$@"; do
    case "$option" in
	-*=*)	optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
	*)	optarg= ;;
    esac

    case "$option" in
	--prefix=*)		prefix="$optarg" ;;
	--prefix)		shift; prefix="$1" ;;

	--bindir=*)		bindir="$optarg" ;;
	--bindir)		shift; bindir="$1" ;;

	--datadir=*)		datadir="$optarg" ;;
	--datadir)		shift; datadir="$1" ;;

	--sysconfdir=*)		sysconfdir="$optarg" ;;
	--sysconfdir)		shift; sysconfdir="$1" ;;

	--mandir=*)		mandir="$optarg" ;;
	--mandir)		shift; mandir="$1" ;;

	--docdir=*)		docdir="$optarg" ;;
	--docdir)		shift; docdir="$1" ;;

	--with-shell=*)		with_shell="$optarg" ;;
	--with-shell)		shift; with_shell="$1" ;;

	--with-awk=*)		with_awk="$optarg" ;;
	--with-awk)		shift; with_awk="$1" ;;

	--with-sendmail=*)	with_sendmail="$optarg" ;;
	--with-sendmail)	shift; with_sendmail="$1" ;;

	--with-printcap=*)	with_printcap="$optarg" ;;
	--with-printcap)	shift; with_printcap="$1" ;;

	--with-spooldir=*)	with_spooldir="$optarg" ;;
	--with-spooldir)	shift; with_spooldir="$1" ;;

	--quiet|--silent)	quiet=set ;;

	--help)			cat <<EOF
Usage: configure [options]
Options: [defaults in brackets after descriptions]
Configuration:
  --help                  print this message
  --quiet, --silent       do not print informational messages
Directory and file names:
  --prefix=PREFIX         install architecture-independent files in PREFIX
			  [$prefix_default]
  --bindir=DIR            user executables in DIR [PREFIX/bin]
  --datadir=DATADIR       read-only architecture-independent data in DATADIR
                          [PREFIX/share]
  --sysconfdir=DIR        read-only single-machine data in DIR [PREFIX/etc]
  --mandir=DIR            man documentation in DIR [PREFIX/man]
  --docdir=DIR            regular documentation in DIR [DATADIR/doc]
Features and packages:
  --with-shell=NAME       use NAME as shell executable [autodetected]
  --with-awk=NAME         use NAME as awk executable [autodetected]
  --with-sendmail=NAME    use NAME as sendmail executable [autodetected]
  --with-printcap=NAME    use NAME as printcap database [/etc/printcap]
  --with-spooldir=DIR     use DIR as lpd spool directory [/var/spool/lpd]
EOF
				exit 0 ;;

	*)	echo "configure: error: $option: invalid option" 1>&2
		echo "use --help to show usage" 1>&2
		exit 1 ;;
    esac
done


# verbose output?

if test "$quiet"; then
    exec 3>/dev/null
else
    exec 3>&1
fi

# set variables according to defaults/arguments

. ./VERSION
echo "configuring apsfilter version $version-$branch..." 1>&3

test "$prefix"		|| prefix="$prefix_default"
test "$bindir"		|| bindir="$prefix/bin"
test "$datadir"		|| datadir="$prefix/share"
test "$sysconfdir"	|| sysconfdir="$prefix/etc"
test "$mandir"		|| mandir="$prefix/man"
test "$docdir"		|| docdir="$datadir/doc"
test "$with_printcap"	|| with_printcap=/etc/printcap
test "$with_spooldir"	|| with_spooldir=/var/spool/lpd

# autodetection

if test "$with_shell"; then
    if test -x "$with_shell"; then
	echo "using $with_shell as the shell executable" 1>&3
    else
	echo "configure: error: shell not found at $with_shell" 1>&2
	exit 1
    fi
else
    # these shells should work (TM)
    for name in bash bash2 pdksh zsh; do
	IFS=":"
	for dir in $PATH; do
	    test -x "$dir/$name" && { with_shell="$dir/$name"; break; }
	done
	IFS="$old_ifs"
	echo "looking for a suitable shell: $name... $with_shell" 1>&3
	test "$with_shell" && break
    done
    if test -z "$with_shell"; then
	# fall back
	with_shell=/bin/sh
	echo "configure: warning: falling back to /bin/sh" 1>&2
	echo "configure: you might want to use --with-shell" 1>&2
    fi
fi

if test "$with_awk"; then
    if test -x "$with_awk"; then
	echo "using $with_awk as the awk executable" 1>&3
    else
	echo "configure: error: awk not found at $with_awk" 1>&2
	exit 1
    fi
else
    # these awks should work (TM)
    for name in gawk mawk nawk; do
	IFS=":"
	for dir in $PATH; do
	    test -x "$dir/$name" && { with_awk="$dir/$name"; break; }
	done
	IFS="$old_ifs"
	echo "looking for a suitable awk: $name... $with_awk" 1>&3
	test "$with_awk" && break
    done
    if test -z "$with_awk"; then
	# fall back
	IFS=":"
	for dir in $PATH; do
	    test -x "$dir/awk" && { with_awk="$dir/awk"; break; }
	done
	IFS="$old_ifs"
	if test "$with_awk"; then
	    echo "configure: warning: falling back to $with_awk" 1>&2
	    echo "configure: you might want to use --with-awk" 1>&2
	else
	    echo "configure: error: no awk found; use --with-awk" 1>&2
	    exit 1
	fi
    fi
fi

if test "$with_sendmail"; then
    if test -x "$with_sendmail"; then
	echo "using $with_sendmail as the sendmail executable" 1>&3
    else
	echo "configure: error: sendmail not found at $with_sendmail" 1>&2
	exit 1
    fi
else
    IFS=":"
    for path in /usr/sbin /usr/lib $PATH; do
	test -x "$path/sendmail" && with_sendmail="$path/sendmail"
	echo "looking for sendmail in $path... $with_sendmail" 1>&3
	test "$with_sendmail" && break
    done
    IFS="$old_ifs"
    if test ! -x "$with_sendmail"; then
	echo "configure: error: no sendmail found; use --with-sendmail" 1>&2
	exit 1
    fi
fi


# substitute variables in files (foo.in -> foo)

for file in $DOT_IN_FILES
do
    echo "creating $file..." 1>&3
    sed < "${file}.in" > "$file" "s?@prefix@?$prefix?g; s?@bindir@?$bindir?g; \
s?@datadir@?$datadir?g; s?@sysconfdir@?$sysconfdir?g; s?@mandir@?$mandir?g;   \
s?@shell@?$with_shell?g; s?@awk@?$with_awk?g; s?@sendmail@?$with_sendmail?g;  \
s?@printcap@?$with_printcap?g; s?@spooldir@?$with_spooldir?g;                 \
s?@docdir@?$docdir?g; s?@VERSION@?$version?g; s?@branch@?$branch?g;           \
s?@confopts@?${confopts:-none}?g; s?@DOT_IN_FILES@?$DOT_IN_FILES?g"
done

# make scripts executable

chmod 755 SETUP bin/apsfilter bin/aps2file bin/apsfilter-bug bin/apspreview


# summary

cat 1>&3 <<EOF

Using following values as defaults:
    prefix		$prefix
    bindir		$bindir
    datadir		$datadir
			(will become $datadir/apsfilter)
    sysconfdir		$sysconfdir
			(will become $sysconfdir/apsfilter)
    mandir		$mandir
    docdir		$docdir
			(will become $docdir/apsfilter)

    shell		$with_shell
    awk			$with_awk
    sendmail		$with_sendmail
    printcap		$with_printcap
    spooldir		$with_spooldir

Now run "make install" to install apsfilter.
EOF
