#!/bin/sh

PREFIX=/usr/local
VER=1.0.0

### LIBRND_PREFIX ###
if test -z "$LIBRND_PREFIX"
then
	for P in /usr/local /opt/librnd /usr $HOME/usr
	do
		if test -e "$P/include/librnd/core/config.h"
		then
			LIBRND_PREFIX="$P"
			break
		fi
	done
fi


case "$LIBRND_PREFIX" in
	"") 
		echo "

ERROR: Can not find librnd.

If librnd is not installed, please read INSTALL for details.

If librnd is installed at a non-standard prefix, please read
http://www.repo.hu/projects/pcb-rnd/INSTALL.librnd.txt and set LIBRND_PREFIX
accordingly (e.g. export LIBRND_PREFIX=/home/foo/sw/librnd) before running
./configure

" >&2
		exit 1
		;;
	/usr/include) ;;
	/usr/local/include) ;;
	*) LIBRND_EXTRA_INCLUDE="$LIBRND_PREFIX/include"
esac

### command line args ###
while test $# -gt 0
do
	cmd="$1"
	shift 1
	case "$cmd" in
		--prefix=*) PREFIX=${cmd##--prefix=} ;;
		--confdir=*) confdir=${cmd##--confdir=} ;;
		--LDFLAGS=*) CFG_LDFLAGS=${cmd##--LDFLAGS=} ;;
		--CFLAGS=*) CFG_CFLAGS=${cmd##--CFLAGS=} ;;
		*) echo "Unknown argument $cmd" >&2; exit 1;;
	esac
done

if test -z "$confdir"
then
	case "$PREFIX" in
		/usr) confdir=/etc/camv-rnd; confdirh=$confdir ;;
		*) confdir='$(PREFIX)/etc/camv-rnd'; confdirh="$PREFIX/etc/camv-rnd" ;;
	esac
else
	confdirh=$confdir
fi

echo "# Generated by ./configure

LIBRND_PREFIX=$LIBRND_PREFIX
PREFIX=$PREFIX

BINDIR=\$(install_root)\$(DESTDIR)\$(PREFIX)/bin
CONFDIR=\$(install_root)\$(DESTDIR)$confdir
LIBDIR=\$(install_root)\$(DESTDIR)\$(PREFIX)/lib
SHAREDIR=\$(install_root)\$(DESTDIR)\$(PREFIX)/share
DOCDIR=\$(install_root)\$(DESTDIR)\$(PREFIX)/share/doc/camv-rnd

CFG_LDFLAGS=$CFG_LDFLAGS
CFG_CFLAGS=$CFG_CFLAGS
" > Makefile.conf


echo "/* Generated by ./configure */

#define LIBRND_PREFIX \"$LIBRND_PREFIX\"
#define CONFDIR       \"$confdirh\"
#define LIBDIR        \"$PREFIX/lib\"
#define SHAREDIR      \"$PREFIX/share\"
#define CAMV_VERS     \"$VER\"
" > config.h


echo "
Done.
librnd prefix: $LIBRND_PREFIX
camv-rnd prefix: $PREFIX
"
