#!/bin/sh
#
# ConfigureMe: apply appropriate default local configuration options
#
# Copyright: 2001 Alan Robertson <alanr@unix.sh>
# License:   GNU General Public License (GPL)
#
Usage() {
  cat <<-!

	Usage: $0 {configure|make|install|dist|distcheck|package|flags|bootstrap} [--xxx=yyy]

	$0 is a wrapper to invoke GNU 'configure' with options that match
	common conventions associated with this machine (i.e. $CFENV)

	You may also, if you wish, supply additional 'configure' options in
	their usual '--xxx=yyy' form.

	It will also build, make, install or create packages for the
	build environment.

	$0 does not know how to create packages for every environment,
	nor is the information on "common conventions" necessarily correct.
	Patches to this process are solicited -- especially in these areas.

	!
  if
    [ -x ./configure ]
  then
    echo "Legal configure arguments are:"
    ./configure --help
  fi
  exit 1
}
#
#	The vast majority of cases here have not been tested yet...
#	If you don't think the treatment of your favorite OS is right,
#	then submit a patch.  Some of these conventions were wild guesses...
#

#	autoconf "--enable-XXX" options generally have their default {yes|no}
#	set, and perhaps acted upon, in "configure.in".
#
#	But we will also allow many of them to take a "try" setting,
#	interpreted there as:
#		"try to act as if 'yes' had been specified, but if this proves
#		troublesome, then continue as if 'no' had been specified".
#	By using these from here in "ConfigureMe", this allows the beginner
#	to make rapid, successful progress (albeit suboptimal) with an
#	implicit sense of achievement, rather than the demoralisation from
#	an unnecessarily failed 'yes' specification.
#	(David Lee, 2005)

cmd=$0
pathtotop=`dirname ${cmd}`


PACKAGECMD=""


ConfigureLinux() {
  DFLAGS=""
  if
    [ -f /etc/UnitedLinux-release -a -s /etc/UnitedLinux-release ]
  then
    distro="United Linux"
    PACKAGECMD="$MAKE_CMD rpm"
    DFLAGS="--with-ucd-snmp-devel=ucdsnmp --with-group-id=90 --with-ccmuser-id=90"
  elif
    [ -f /etc/SuSE-release -a -s /etc/SuSE-release ]
  then
    distro="SuSE Linux"
    PACKAGECMD="$MAKE_CMD rpm"
    # -fno-unit-at-a-time is replaced by -fno-toplevel-reorder in gcc4.2
    # But apparently it shouldn't be required
    # http://www.gnu.org/software/gcc/gcc-4.2/changes.html
    #export CFLAGS="$CFLAGS -fno-unit-at-a-time"
    DFLAGS="--with-group-id=90 --with-ccmuser-id=90"
    R=`cat /etc/SuSE-release | grep 'VERSION *= *' | sed -e 's%.*= *%%'`
    case $R in
      [78].*) DFLAGS="$DFLAGS --mandir=/usr/share/man --disable-snmp-subagent --disable-swig --with-ucd-snmp-devel=ucdsnmp";;
    esac
  elif
    [ -f /etc/redhat-release -a -s /etc/redhat-release ] 
  then
    distro="RedHat Linux"
    PACKAGECMD="$MAKE_CMD rpm"
    DFLAGS="--mandir=/usr/share/man"
  elif
    [ -f /etc/conectiva-release -a -s /etc/conectiva-release ]
  then
    distro="Conectiva Linux"
    PACKAGECMD="$MAKE_CMD rpm"
    DFLAGS="--with-group-id=17 --mandir=/usr/share/man --infodir=/usr/share/info --with-ccmuser-id=17"
  elif
    [ -f /etc/debian_version -a -s /etc/debian_version ]
  then
    distro="Debian GNU/Linux"
    PACKAGECMD="$MAKE_CMD deb"
    DFLAGS="--mandir=/usr/share/man"
  elif
    [ -f /etc/gentoo-release -a -s /etc/gentoo-release ]
  then
    distro="Gentoo Linux"
    PACKAGECMD="$MAKE_CMD dist"
    DFLAGS="--with-group-name=cluster --with-ccmuser-name=cluster --with-group-id=65 --with-ccmuser-id=65"
  else
    distro="Generic Linux"
  fi
  CFENV="$distro"
  FLAGS="--prefix=/usr --sysconfdir=/etc --localstatedir=/var $DFLAGS --disable-rpath"
}

ConfigureAIX() {
  CFENV="AIX (freeware toolbox)"
  FLAGS="--disable-ldirectord --prefix /opt/freeware"
}

ConfigureFreeBSD() {
  FLAGS="--prefix=/usr/local --sysconfdir=/usr/local/etc --localstatedir=/var --enable-all  --with-group-id=90 --with-ccmuser-id=90 --disable-rpath"
  CFENV="FreeBSD"
}

ConfigureOpenBSD() {
  FLAGS="--prefix=/usr/local --sysconfdir=/etc --localstatedir=/var  --with-group-id=584 --with-ccmuser-id=584 --disable-rpath --with-group-name=_heartbeat --with-ccmuser-name=_heartbeat --with-ocf-root=/usr/local/lib/ocf/ --enable-fatal-warnings=no" 
  export LDFLAGS="-liconv -L/usr/local/lib/libnet-1.0"
  export LIBNETCONFIG=/usr/local/bin/libnet-config-1.0
  export AUTOCONF_VERSION=2.61
  CFENV="OpenBSD"
}

ConfigureNetBSD() {
  FLAGS="--disable-ldirectord --prefix=/usr/local --sysconfdir=/usr/local/etc --localstatedir=/var  --with-group-id=90 --with-ccmuser-id=90 --disable-rpath"
  CFENV="NetBSD"
}

ConfigureGenericBSD() {
  FLAGS="--disable-ldirectord --prefix=/usr/local --sysconfdir=/usr/local/etc --localstatedir=/var  --with-group-id=90 --with-ccmuser-id=90 --disable-rpath"
  CFENV="Generic BSD"
}

ConfigureSolaris() {
  # PKGNAME: see comment in "configure.in"
  PKGNAME="LXHAhb"
  FLAGS="--disable-ldirectord --prefix=/opt/$PKGNAME --sysconfdir=/etc/opt/$PKGNAME --localstatedir=/var/opt/$PKGNAME --with-pkgname=$PKGNAME --disable-rpath"
  CFENV="Solaris"
  PACKAGECMD="$MAKE_CMD pkg"
}

ConfigureDarwin() {

  for dir in / /sw /opt/local; do
     if [ -d $dir ]; then
        install_prefix=$dir
     fi
  done

  FLAGS="--prefix=${install_prefix}"
  FLAGS="$FLAGS --with-initdir=/private/etc/mach_init.d"
  FLAGS="$FLAGS  --localstatedir=${install_prefix}/var"
  FLAGS="$FLAGS --with-group-name=admin --with-ccmuser-name=daemon"
  FLAGS="$FLAGS --enable-fatal-warnings=yes"
  FLAGS="$FLAGS --disable-rpath"
  export CFENV="Darwin"
}

ConfigureGenericUNIX() {
  echo "Configuring for generic UNIX system"
  FLAGS="--disable-ldirectord --prefix=/usr --sysconfdir=/etc --localstatedir=/var"
  CFENV="Generic UNIX"
}

lcase() {
  #  Convert to lower-case in a portable way
  if
    [ X"`echo A | dd conv=lcase 2>/dev/null`" = Xa ]
  then
    dd conv=lcase 2>/dev/null
  else
    tr ['A-Z'] ['a-z']
  fi
}

GetConfigureFLAGS() {
  if 
      [ "X$MAKE" != "X" ]
  then
      MAKE_CMD="$MAKE"
  elif
      which gmake > /dev/null
  then
      MAKE_CMD="gmake"
  else
      MAKE_CMD="make"
  fi
  case $CROSSCOMPILE in
    yes)	GetCrossConfigureFlags;;
    *)		GetNativeConfigureFlags;;
  esac
}

GetNativeConfigureFlags() {
  case `uname -s | lcase` in
    linux)	ConfigureLinux;;
    aix)	ConfigureAIX;;
    freebsd)	ConfigureFreeBSD;;
    openbsd)	ConfigureOpenBSD;;
    netbsd)	ConfigureOpenBSD;;
    *bsd)	ConfigureGenericBSD;;
    sunos)	ConfigureSolaris;;
    darwin)	ConfigureDarwin;;
    *)		ConfigureGenericUNIX;;
  esac
}

GetCrossConfigureFlags() {
  case $CC in
           # Don't force endianness on ARM - it can be either type
    *arm*) FLAGS="--prefix=/usr/local/arm-linux/arm-linux --sysconfdir=/etc --localstatedir=/var";;
    *)	   echo "Error: Unsupported cross-compiler: [$CC]"; exit 1;;
  esac
}


Run() {
  echo "Running $@"
  "$@"
}

PackageItUp() {
  if
    [ "X$PACKAGECMD" = X ]
  then
    echo "Do not know how to build a package for $CFENV" >&2
    return 1
  else
    Run $PACKAGECMD
  fi
}

do_configure () {
	# Do autotools bootstrap if needed.
	# Should only be needed by developers and geeks because any
	# distributed stable versions (tar.gz etc.) should already have
	# "configure" etc. set up.
	if [ ! -x ${pathtotop}/configure ]
	then
		Run ${pathtotop}/bootstrap "$@"
	else
		Run ${pathtotop}/configure "$@"
	fi
}

cmd=`echo $1 | lcase`

case $cmd in
  cross-*)	CROSSCOMPILE=yes; cmd=`echo $cmd |cut -c7-`;;
  *)		CROSSCOMPILE=no;;
esac

GetConfigureFLAGS
echo ""
echo "Configure flags for $CFENV: $FLAGS" >&2

if
  [ $# -le 0 ]
then
  Usage
fi

shift


case $cmd in
  flags)
  	echo $FLAGS $@
	;;
  cf|conf|configure)		
  	do_configure $FLAGS $@
	;;
  boot|bootstrap)		
  	rm -f ${pathtotop}/configure
  	do_configure $FLAGS $@
	;;
  make|build)			
  	do_configure $FLAGS $@ && \
  	Run $MAKE_CMD;;
  install)			
  	do_configure $FLAGS $@ && \
	Run $MAKE_CMD install
	;;
  dist)				
  	do_configure $FLAGS $@ && \
  	Run $MAKE_CMD dist
	;;
  distcheck)			
  	do_configure $FLAGS $@ && \
	source ./heartbeat/lib/ha_config && \
	Run $MAKE_CMD DESTDIR="$PWD/heartbeat-$VERSION/=inst" distcheck
	;;
  pkg|package|rpm|deb|dpkg)	
  	do_configure $FLAGS $@ && \
	PackageItUp
	;;
  *)	
  	Usage
	;;
esac
