#!/bin/sh
# NAME
#	default-configure : execute "configure" script with standard options
# VERSION
#	$Id$
# CHANGELOG
#	$Log$
# USAGE
#	default-configure location-rule package-name
# COPYRIGHT
#  Copyright (C) 2000,2001 Steel Wheels Project.
#  This file is a apart of the papaya utilities. 
#  If you need the information of copyright of this file, see COPYING
#  file distributed with file or see http://www.asahi-net.or.jp/~em7t-hmd
#  web page.

this="default-configure"
helpfile="default-configure-help.list" 
configure_args="" ;
location=$1 ;
package=$2 ;

error(){
	echo "$this [ERROR] $1" >&2 ;
	exit 1 ;
}

usage(){
	echo "usage: default-configure location-rule package-name" ;
	exit 1 ;
}

add_arg(){
	if grep -e $1 $helpfile > /dev/null ; then
		# echo "option: ${1}${2}" ;
		configure_args="$configure_args ${1}${2}" ;
	fi
}

add_cflags(){
	if test -d $1 ; then
		CFLAGS="$CFLAGS -I$1"
		CPPFLAGS="$CPPFLAGS -I$1"
	fi
}

add_libs(){
	if test -d $1 ; then
		LIBS="$LIBS -L$1"
	fi
}

if [ -z "$location" ] ; then
	usage ;
fi

case $location in
  user)
	PREFIX="/usr"
	EPREFIX="/usr" 
	SYSCONF="/etc" ;;
  boot)
	PREFIX="/usr"
	EPREFIX="/usr" 
	SYSCONF="/etc" ;;
  local)
	PREFIX="/usr/local"
	EPREFIX="/usr/local" 
	SYSCONF="/etc/local" ;;
  X11R6)
	PREFIX="/usr/X11R6"
	EPREFIX="/usr/X11R6"
	SYSCONF="/etc/X11" ;;
  gnu)
	PREFIX="/usr/gnu"
	EPREFIX="/usr/gnu" 
	SYSCONF="/etc/gnu" ;;
  gnome)
	PREFIX="/usr/gnome"
	EPREFIX="/usr/gnome" 
	SYSCONF="/etc/gnome" ;;
  gnustep)
	PREFIX="/usr/GNUstep"
	EPREFIX="/usr/GNUstep" 
	SYSCONF="/etc/GNUstep" ;;
  *) 
	PREFIX="/usr/local"
	EPREFIX="/usr/local" 
	SYSCONF="/etc/local" ;;
esac

if [ -z "$package" ] ; then
	error "option \"package\" is not given" ;
fi

if test -f ../configure ; then
	CONFIGURE="../configure"
elif test -f ./configure ; then
	CONFIGURE="./configure"
else
	error "configure script is not found. in . and .."
fi
	
if ! $CONFIGURE --help > $helpfile ; then
	error "execution of \"$CONFIGURE --help\" was failed" 
fi

LOCALSTATE="/var/state" ;
HTDOCDIR="/pub/www/manuals/$package"

add_arg "--prefix" "=$PREFIX" ;
add_arg "--exec-prefix" "=$EPREFIX" ;
add_arg "--sysconfdir" "=$SYSCONF" ;
add_arg "--localstatedir" "=$LOCALSTATE" ;
# thread option is not required. it is detected automatically
#add_arg "--with-threads" ""
#add_arg "--enable-threads" ""
add_arg "--enable-shared" ""
add_arg "--with-html-dir" "=$HTDOCDIR"
add_arg "--with-GL-prefix" "=/usr/X11R6"

# for BSD file system
if [ -d ${PREFIX}/share/man ] ; then
	add_arg "--mandir" "=${PREFIX}/share"
fi

rm $helpfile

# setup environment variable
add_cflags "/usr/local/include"
add_cflags "/usr/X11R6/include"
add_cflags "/usr/gnome/include"
add_cflags "/usr/gnu/include"
add_cflags "/usr/pkg/include"
add_cflags "/opt/sfw/include"

LIBS="" ;
add_libs   "/usr/local/lib"
add_libs   "/usr/X11R6/lib"
add_libs   "/usr/gnome/lib"
add_libs   "/usr/gnu/lib"
add_libs   "/usr/pkg/lib"
add_libs   "/opt/sfw/lib"
LDFLAGS="$LIBS"

echo "$CONFIGURE $configure_args" ;
echo "CFLAGS: $CFLAGS"
echo "CPPFLAGS: $CPPFLAGS"
echo "LDFLAGS: $LDFLAGS"
echo "LIBS: $LIBS"
export CFLAGS CPPFLAGS LDFLAGS LIBS

if $CONFIGURE $configure_args ; then
	exit 0 ;
else
	exit 1 ;
fi

