#!/bin/sh

#  Mini-configure script for PDCMAC for the benefit of UNIX users

version="1.0 Time-stamp: <pdc 1995-03-24>"

#  Takes arguments a bit like the GNU autoconf, but only uses them to
#  try to guess the TeX root directory

package=pdcmac
date=`date +19%y-%m-%d`
files=makefile
unique=pdcmisc.dtx
cmd="$0 $@"
me=`basename $0`

# The flag used with "test" to see if a directory is writable.
# For debugging, I sometimes set writable to -d so that 
#  the searches will pretend that I have write-access to system
#  directories (so I can simulate running the script as root)
writable=-w

echo "This is configure for $package $version $cfgdate"

tmp=${TMPDIR-/tmp}/cfg$$
trap 'rm -f $tmp; exit 1' 1 2 15

while test $# -gt 0; do
	case $1 in
	    -n*|--n*)
		nocreate=t ;;
	    -v*|--v*)
		echo "configure for $package $version <$cfgauthor $cfgdate>" 1>&2
		exit 0 ;;
	    -h*|--h*)
		cat 1>&2 <<@EOF
USAGE:  configure [ option ]...

Running this script creates config.status and optionally 
$files.

Options:
    -h, -help, --help, -v, -version, --version
	print a message and exit

    -p DIR, -prefix DIR, --prefix=DIR
	prefix for installed file names, for example /usr/local

    -e DIR, -exec_prefix DIR, --exec_prefix=DIR
	ignored

    -t DIR, -texmf DIR, --texmf=DIR
	prefix for all TeX-related installed file names, for
	example /usr/texmf or /usr/local/lib/tex

    -w tds, -with tds, --with-tds
	Assume a TDS-like directory tree
@EOF
		exit 0 ;;
	    --p*=*)
		prefix=`expr $1 : '--p.*=\(.*\)'` ;;
	    -p|-prefix|--p*)
		prefix=$2; shift ;;
	    -p*)
		prefix=`expr $1 : '-p\(.*\)$'` ;;
	    --s*=*)
		srcdir=`expr $1 : '--s.*=\(.*\)'` ;;
	    -s|-srcdir|--s*)
		srcdir=$2; shift ;;
	    -s*)
		srcdir=`expr $1 : '-s\(.*\)$'` ;;
	    --t*=*)
		texmf=`expr $1 : '--t.*=\(.*\)'` ;;
	    -t|-texmf|--t*)
		texmf=$2; shift ;;
	    -t*)
		texmf=`expr $1 : '-t\(.*\)$'` ;;
	    --f*=*)
		fonts=`expr $1 : '--f.*=\(.*\)'` ;;
	    -f|-fonts|--f*)
		fonts=$2; shift ;;
	    -f*)
		fonts=`expr $1 : '-f\(.*\)$'` ;;
	    --w*-*)
		with=$with/`expr $1 : '--w.*-\(.*\)'`/ ;;
	    -w|-with|--w*)
		with=$with/$2/; shift ;;
	    -w*)
		with=$with/`expr $1 : '-w\(.*\)$'`/ ;;
	    -e*|--e*=*)
		;;
	    -e|--e*)
		shift ;;
	    *)
		echo $me: ignoring $1 1>&2
		;;
	esac
	shift
done

#  Check for the "with" keywords we're interested in:
case $with in */tds/*) tds=t ;; esac
#  case $with in */bigendian/*) bigendian=t ;; esac
	#  endianness doesn't matter because no fonts in this package


#  Find out how to print a dot with no newline
n=-n c=
echo $n x$c > $tmp
case "`cat $tmp`" in
    -n*)
	n= c='\\c' ;;
esac
dot="$n .$c"


#  INSTALL -- oh, I can't be bothered trying to be clever.
if install --version 2>&1 | grep GNU >/dev/null; then
	INSTALL="install"
else
	INSTALL='$(srcdir)/pinstall'
fi
INSTALL_DATA='$(INSTALL) --mode=644'
echo $me: install files with $INSTALL


# find srcdir
if test -z "$srcdir" ; then
	echo $n "$me: searching for srcdir (package directory)..$c"
	for d in . `dirname $0` ..; do
		echo $dot
		if test -f $d/$unique; then
			echo srcdir=$d
			srcdir=$d
			break
		fi
	done
fi

if test -z "$srcdir"; then
	echo ""
	echo $me: fatal error: could not find sourcdir
	exit 1
fi

#  User might use --prefix to give the TeX prefix:

if test -z "$texmf"; then
	case "$prefix" in
	    */texmf|*/tex)
		texmf=$prefix ;;
	esac
fi

#  If $texmf is not set, then search for it
#  We look in all sorts of prefixes like /usr/local (with or without extra
#  lib), using all sorts of directiry names like "tex"
#  

if test -z "$texmf"; then
	prefixes="$prefix 
		`echo $TEXINPUTS:$TEXFONTS | tr : \\\\12 | 
		sed -n -e 's,/[Tt]e[Xx].*,,p' -e 's,/font.*,,p' | uniq` 
		/ /usr /usr/local /opts /p /packages /package
		/pd /usr/pd /usr/local/pd /usr/contrib /usr/TeX
		$HOME"
	echo $n $me: searching for \$texmf..$c
	for d in texmf tex TeX tex3.14 tex3 tex82; do
		for p in $prefixes; do
			echo $dot
			if test -d $p/$d -a $writable $p/$d; then
				echo texmf=$p/$d
				texmf=$p/$d
				break 2
			fi
			if test -d $p/lib/$d -a $writable $p/lib/$d; then
				echo texmf=$p/lib/$d
				texmf=$p/lib/$d
				break 2
			fi
		done
	done
fi
if test -z "$texmf"; then
	echo ""
	echo $me: fatal error: could not guess a texmf directory 1>&2
	exit 1
fi
if test ! -d "$texmf" -o ! -w "$texmf"; then
	echo $me: warning: $texmf needs to be a writable directory 1>&2
fi


#  If --with-tds not set, try to guess whether we have found
#   a TDS directory or not:
if test -n "$tds" || test -d $texmf/tex/plain; then
	tds=t 
	echo "$me: using TWG-TDS 0.61 directory structure"
else
	echo "$me: using old-style directory structure :-("
fi


#  Start making the config.status file

f=config.status

# Special care is taken with the -n option
#  -- -n is not to be included by default when configure is run with 
#     "config.status -r", so it must be removed from $cmd
#  -- -n (or other options) may be included on the command line 
#      of "config.status -r" and will be passed to configure
cmd2=`echo $cmd | sed 's/ --*n[a-z-]*//g'`
cat >$f <<@EOF
# config.status -- configuration status file
# creator: configure for $package $version <$cfgauthor $cfgdate>
# created: $date
# command: $cmd

case "\$1" in
    -r*|--r*)
	shift
	echo \$0: rerunning $cmd2 1>&2
	exec sh $cmd2 \$* ;;
esac

echo This is config.status, generated by configure for $package on $date

srcdir=$srcdir
date=`date +19%y-%m-%d`
files="\${*-$files}"
sedscript='
	s,@package@,$package,
	s,@srcdir@,$srcdir,
	s,@texmf@,$texmf,
	s,@configure@,$0,
	s,@INSTALL@,$INSTALL,
	s,@INSTALL_DATA@,$INSTALL_DATA,
@EOF

if test -n "$tds"; then
	echo >>$f "	s,@texdir@,\$(texmf)/tex/plain/\$(package),"
	echo >>$f "	s,@docdir@,\$(texmf)/doc/plain/\$(package),"
else
	echo $n $me: searching for TeX macro dir..$c; found=
	for d in inputs macros; do
		echo $dot
		if test -d $texmf/$d -a $writable $texmf/$d; then
			echo $texmf/$d
			echo >>$f "	s,@texdir@,$texmf/$d,"
			found=t
			break
		fi
	done
	if test -z "$found"; then 
		echo ""
		echo $0: fatal error: TeX macro dir NOT FOUND 1>&2
		exit 1
	fi

	# now same again for a documentation directory
	echo $n $me: searching for documentation dir..$c; found=
	for p in $texmf $fonts $prefixes; do
		for d in doc doc/tex docs docs/tex ; do
			echo $dot
			if test -d $p/$d -a $writable $p/$d; then
				echo $p/$d/$package
				echo >>$f "	s,@docdir@,$p/$d/\$(package),"
				found=t
				break 2
			fi
		done
	done
	if test -z "$found"; then 
		echo ""
		echo $0: warning: documentation dir NOT FOUND 1>&2
	fi
fi		


# Finish off the config.status file:

cat >>$f <<'@EOF'
'

for file in $files; do
	echo $0: writing $file... 1>&2
	sed -e 's,Template for configure,Generated from '$file'.in on '$date' by '$0, \
		-e "$sedscript" $srcdir/$file.in > $file || exit 1
done

exit 0
@EOF

echo $me: configuration status written on config.status

if test -z "$nocreate"; then
	sh config.status
fi

rm -f $tmp
exit 0