#!/bin/sh
#
# ConfigureMe: apply appropriate default local configuration options
#
# (c) 2001 Alan Robertson <alanr@unix.sh>
#	Licensed under the GNU GPL
#
Usage() {
  cat <<-!

	Usage: $0 [configure|make|install|package|flags]

	$0 configures the system with options that match common conventions
	associated with this machine (i.e. $CFENV)

	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.
	!
  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...
#

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"
    DFLAGS="--with-ucd-snmp-devel=ucdsnmp --with-group-id=90 --with-ccmuser-id=90"
  elif
    [ -f /etc/redhat-release -a -s /etc/redhat-release ] 
  then
    distro="RedHat Linux"
    PACKAGECMD="$MAKE_CMD rpm"
  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"
  else
    distro="Generic Linux"
  fi
  CFENV="$distro"
  FLAGS="--prefix=/usr --sysconfdir=/etc --localstatedir=/var $DFLAGS"
  case `uname -m` in
    x86_64)
		# Hmmm... Let's check for building from chrooted 32-bit environment...
		case `file /bin/uname` in
		  *Intel*80?86*)	;;
		  *)	FLAGS="$FLAGS --libexecdir=/usr/lib64 --libdir=/usr/lib64";;
		esac
		;;
   *)		;;
  esac
}

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

ConfigureFreeBSD() {
  FLAGS="--prefix=/usr/local --sysconfdir=/usr/local/etc --localstatedir=/usr/local/var"
  CFENV="FreeBSD"
}

ConfigureOpenBSD() {
  FLAGS="--prefix=/usr/local --sysconfdir=/usr/local/etc --localstatedir=/usr/local/var"
  CFENV="OpenBSD"
}

ConfigureNetBSD() {
  FLAGS="--prefix=/usr/local --sysconfdir=/usr/local/etc --localstatedir=/usr/local/var"
  CFENV="NetBSD"
}

ConfigureGenericBSD() {
  FLAGS="--prefix=/usr/local --sysconfdir=/usr/local/etc --localstatedir=/usr/local/var"
  CFENV="Generic BSD"
}

ConfigureSolaris() {
  FLAGS="--prefix=/opt --sysconfdir=/opt/etc --localstatedir=/opt/var"
  CFENV="Solaris"
  PACKAGECMD="$MAKE_CMD pkg"
}

ConfigureDarwin() {
  FLAGS="--prefix=/sw --sysconfdir=/sw/etc --localstatedir=/sw/var --with-initdir=/private/etc/mach_init.d --enable-fatal-warnings=yes"
  CFENV="Darwin"
}

ConfigureGenericUNIX() {
  echo "Configuring for generic UNIX system"
  FLAGS="--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
}

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)		
  	Run "${pathtotop}/bootstrap" $FLAGS $@
	;;
  make|build)			
  	Run "${pathtotop}/bootstrap" $FLAGS $@ && \
  	Run $MAKE_CMD;;
  install)			
  	Run "${pathtotop}/bootstrap" $FLAGS $@ && \
	Run $MAKE_CMD install
	;;
  dist)				
  	Run echo ./bootstrap $FLAGS $@ && \
  	Run $MAKE_CMD dist
	;;
  distcheck)			
  	Run "${pathtotop}/bootstrap" $FLAGS $@ && \
	source ./heartbeat/lib/ha_config && \
	Run $MAKE_CMD DESTDIR="$PWD/heartbeat-$VERSION/=inst" distcheck
	;;
  pkg|package|rpm|deb|dpkg)	
  	Run "${pathtotop}/bootstrap" $FLAGS $@ && \
	PackageItUp
	;;
  *)	
  	Usage
	;;
esac
