#!/bin/sh

unset prefix bindir datadir mandir man1dir basedir gs psfremdir cross

help() {
cat << EOF
Usage: $0 [OPTIONS]...

Options:
	--help			display this message and exit
	--prefix=/path		installation prefix
	--datadir=/path		base directory for PostScript files [\$prefix/share]
	--basedir=/path		base directory for PostScript files [\$datadir/u2ps]
	--mandir=/path		base directory for manual pages [\$datadir/man]
	--man1dir=/path		directory for manual section 1 pages [\$mandir/man1]
	--with-gs=...		gs command to use [gs]

EOF
exit
}

# Use environment for default values
cc=${CC}
cflags=${CFLAGS:--O}

for arg in "$@"; do
	# Separate LHS and RHS in --option=value case
	case "$arg" in
		*=*)
			val=${arg#*=}
			arg=${arg%%=*}
			;;
		*)
			val=
			;;
	esac
	# strip leading --, equating "--help" and "help"
	arg=${arg#--}

	case "$arg" in
		help) help ;;
		prefix) prefix=$val ;;
		datadir) datadir=$val ;;
		basedir) basedir=$val ;;
		mandir) mandir=$val ;;
		man1dir) man1dir=$val ;;
		with-perl) perl=$val ;;
		with-gs) gs=$val ;;
		cc) cc="$val" ;;
		cflags) cflags="$val" ;;
		devel) cflags="-Wall -g"; basedir="res"; psfremdir="./" ;;
		target) cross="$val-" ;;
		*)
			echo "unexpected parameter $arg"
			exit 1
			;;
	esac
done

# Default paths
prefix=${prefix:-/usr}
bindir=${bindir:-$prefix/bin}
datadir=${datadir:-$prefix/share}
basedir=${basedir:-$datadir/u2ps}
mandir=${mandir:-$datadir/man}
man1dir=${man1dir:-$mandir/man1}

cc=${cc:-${cross}gcc}
gs=${gs:-gs}

# This check won't change anything, but it may be useful
# to warn the user if the value is clearly bogus.

if [ -z "$cross" ]; then
	if echo quit | $gs -sDEVICE=nullpage >/dev/null 2>&1; then
		echo "GhostScript seems to be working well"
	else
		echo "WARNING: cannot run $gs"
		echo "WARNING: resource reduction will not work without gs"
	fi
fi

echo "Updating configuration"

cat > config.h <<END
#define GS "$gs"
#define PATH "$psfremdir"
#define BASE "$basedir"
END

cat > config.mk <<END
CC = $cc
CFLAGS = $cflags

bindir = $bindir
basedir = $basedir
man1dir = $man1dir
END

echo
echo "C compiler: $cc $cflags"
echo "u2ps executable dir: $bindir"
echo "u2ps postscript dir: $basedir"
echo "man section 1 path: $man1dir"
echo "ghostscript command: $gs"
echo 
