#!/bin/sh
# configure script for GNU OCRAD - Optical Character Recognition program
# Copyright (C) 2003 Antonio Diaz Diaz.

progname=$0
srctrigger=ocrad.png

# clear some things potentially inherited from environment.
prefix=/usr/local
srcdir=

# Loop over all args
while true ; do

	# Break out if there are no more args
	if [ $# == 0 ]; then break ; fi

	# Get the first arg, and shuffle
	option=$1
	shift

	# Split out the argument for options that take them
	case ${option} in
	--*=*) optarg=$(echo ${option} | sed -e 's,^[^=]*=,,') ;;
	esac

	# Process the options
	case ${option} in
	--help | --he*)
		exec 1>&2
		echo Usage: configure [options]
		echo
		echo Options: [defaults in brackets]
		echo " --help			print this message"
		echo " --prefix=<directory>	install into <directory> [${prefix}]"
		echo " --srcdir=<directory>	find the sources in <directory> [. or ..]"
		echo
		exit 0 ;;
	--prefix* | --pr*)
		prefix=$(echo ${optarg} | sed -e 's,/$,,') ;;
	--srcdir* | --sr*)
		srcdir=$(echo ${optarg} | sed -e 's,/$,,') ;;
	*)
		echo "configure: Unrecognized option: \"${option}\"; use --help for usage." 1>&2
		exit 1 ;;
	esac
done

# Find the source files, if location was not specified.
srcdirtext=
if [ x${srcdir} == x ]; then
	srcdirtext="or . or .." ; srcdir=.
	if [ ! -r ${srcdir}/${srctrigger} ] ; then srcdir=.. ; fi
	if [ ! -r ${srcdir}/${srctrigger} ] ; then
		## the sed command below emulates the dirname command
		srcdir=$(cd $(echo ${progname} | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,') ; pwd)
	fi
fi

if [ ! -r ${srcdir}/${srctrigger} ] ; then
	exec 1>&2
	echo "configure: Can't find sources in ${srcdir} ${srcdirtext}"
	echo "configure: (At least ${srctrigger} is missing)."
	exit 1
fi

# Set srcdir to . if that's what it is.
if [ $(pwd) == $(cd ${srcdir} ; pwd) ] ; then srcdir=. ; fi

# write variables to config file.
rm -f Makefile
cat > Makefile <<EOF
# Makefile for GNU OCRAD - Optical Character Recognition program
# Copyright (C) 2003 Antonio Diaz Diaz.
# This file was generated automatically by configure. Do not edit.

VPATH = ${srcdir}
prefix = \$(DESTDIR)${prefix}
EOF

cat ${srcdir}/Makefile.in >> Makefile

echo
echo VPATH = ${srcdir}
echo prefix = ${prefix}
echo OK. Now you can run make.
