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

srcdir=
progname=$0
config_file=defs.mk
srctrigger=ocrad.png

# 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 argument
	case ${option} in
	--srcdir* | --sr*) srcdir=$(echo ${optarg} | sed -e 's,/$,,') ;;
	esac
done

# Find the source files, if location was not specified.
if [ x${srcdir} == x ]; then
	srcdirdefaulted=1 ; 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
	echo -n '***' "${progname}: Can't find sources in " 1>&2
	if [ x${srcdirdefaulted} == x1 ] ; then
		echo ${srcdir} or . or .. 1>&2
	else	echo ${srcdir} 1>&2
	fi
	echo '***' "(At least ${srctrigger} is missing)." 1>&2
	exit 1
fi

# Set srcdir to . if that's what it is.
if [ $(pwd) == $(cd ${srcdir} ; pwd) ] ; then
	srcdir=.
elif ! cp ${srcdir}/Makefile Makefile ; then exit 1
fi

# write variables to config file.
rm -f ${config_file}
cat > ${config_file} <<EOF
# This file was generated automatically by configure. Do not edit.

VPATH = ${srcdir}
EOF

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