#!/bin/sh

# default settings
PREFIX="/usr/local"

show_help() {
cat << EOF
Configure script for sagator.

Usage: ./configure [OPTION]...

Defaults for the options are specified in brackets.

Configuration:
	--help			display this help and exit
	--prefix DIR		install files in DIR directory [/usr/local]
	--libdir DIR		architecture dependent libraries [$PREFIX/lib]
	--python PATH		full path to python binary [autodetected]
	--apache-root DIR	apache root directory [autodetected]
	--logwatch-conf	X	logwatch config directory [autodetected]
	--logwatch-scripts X	logwatch scripts directory [autodetected]
	--webdir DIR		MRTG/RRDtool web directory [autodetected]
				(without /sagator at end)
	--filelist		create ./filelist file for packages
EOF
}

# check args
while [ "$1" ]; do
  if [ "$1" = "--help" ]; then
    show_help
    exit
  elif [ "$1" = "--prefix" ]; then
    PREFIX="$2"
    shift 1
  elif [ "$1" = "--libdir" ]; then
    LIBDIR="$2"
    shift 1
  elif [ "$1" = "--python" ]; then
    PYTHON="$2"
    shift 1
  elif [ "$1" = "--apache-root" ]; then
    APACHE_ROOT="$2"
    shift 1
  elif [ "$1" = "--logwatch-conf" ]; then
    LOGWATCH_CONF="$2"
    shift 1
  elif [ "$1" = "--logwatch-scripts" ]; then
    LOGWATCH_SCRIPTS="$2"
    shift 1
  elif [ "$1" = "--webdir" ]; then
    WEBDIR="$2"
    shift 1
  elif [ "$1" = "--filelist" ]; then
    FILELIST="filelist"
  else
    echo "Unknown command line parameter: $1"
    exit
  fi
  shift 1
done

echo -n "checking for my version... "
. ./version.py
if echo $RELEASE | grep "^[0-9]*$" >/dev/null; then
  STABILITY="stable"
  UNSTABLE_RELEASE=""
else
  STABILITY="unstable"
  UNSTABLE_RELEASE="-$RELEASE"
fi
echo "$VERSION-$RELEASE ($STABILITY)"

# check for OS
echo -n "checking for your operating system... "
OS=other
if [ -f /etc/fedora-release -o -f /etc/redhat-release ]; then
  OS=rhfc
elif [ -f /etc/SuSE-release ]; then
  OS=suse
elif [ -f /etc/debian_version ]; then
  OS=debian
elif [ -f /etc/gentoo-release ]; then
  OS=gentoo
fi
echo "$OS"

echo -n "checking for libdir... "
if [ -z "$LIBDIR" ]; then
  LIBDIR="$PREFIX/lib"
fi
LIBARCH=`basename $LIBDIR`
echo "$LIBDIR ($LIBARCH)"

# check for python
echo -n "checking for python... "
if [ -z "$PYTHON" ]; then
  for v in "" 2 2.2 2.3 2.4; do
    which python$v >/dev/null 2>&1 && \
    python$v -c "import sys; sys.exit(sys.version_info[0]>2)" 2>/dev/null && break
  done
  PYTHON="python$v"
fi
PYTHON_VER=`$PYTHON -c '
import sys
print "python"+str(sys.version_info[0])+"."+str(sys.version_info[1])
'`
for PYTHON_PREFIX in /usr /usr/local; do
  if [ -f $PYTHON_PREFIX/include/$PYTHON_VER/Python.h ]; then
    break
  fi
done
echo "$PYTHON, version $PYTHON_VER, in $PYTHON_PREFIX"

# check for pygettext
PYGETTEXT=""
for i in /usr/bin/pygettext /usr/bin/pygettext.py \
         /usr/share/doc/packages/python/Tools/i18n/pygettext.py;
do
  if [ -x $i ]; then
    PYGETTEXT=$i
    break
  fi
done

# check for apache configuration files
echo -n "checking for apache... "
if [ -z "$APACHE_ROOT" ]; then
  APACHE_ROOT=/etc/httpd
  for ar in /etc/httpd /etc/apache /etc/httpd2 /etc/apache2; do
    if [ -d $ar/conf.d ]; then
      APACHE_ROOT=$ar
    fi
  done
fi
echo "$APACHE_ROOT"

# check for logwatch configuration files
echo -n "checking for logwatch... "
if [ -z "$LOGWATCH_CONF" ]; then
  if [ -d /usr/share/logwatch/default.conf ]; then
    LOGWATCH_CONF=/usr/share/logwatch/default.conf
  elif [ -d /etc/log.d/conf ]; then
    LOGWATCH_CONF=/etc/log.d/conf
  else
    LOGWATCH_CONF=/etc/logwatch/conf
  fi
fi
if [ -z "$LOGWATCH_SCRIPTS" ]; then
  if [ -d /usr/share/logwatch/scripts ]; then
    LOGWATCH_SCRIPTS=/usr/share/logwatch/scripts
  elif [ -d /etc/log.d/scripts ]; then
    LOGWATCH_SCRIPTS=/etc/log.d/scripts
  else
    LOGWATCH_SCRIPTS=/etc/logwatch/scripts
  fi
fi
echo "$LOGWATCH_CONF, $LOGWATCH_SCRIPTS"

# check for web directory
echo -n "checking for webdir... "
if [ -z "$WEBDIR" ]; then
  WEBDIR="/var/www/html/sagator"
  for dir in /var/www/htdocs /srv/www/htdocs; do
    if [ -d $dir ]; then
      WEBDIR="$dir/sagator"
    fi
  done
fi
echo "$WEBDIR"

echo "configured prefix... $PREFIX"

for mf in Makefile po/Makefile \
          scripts/sagator.spec scripts/cron scripts/graphs/mrtg.cfg \
          scripts/apache.conf debian/changelog; do
  echo "creating $mf"
  sed \
    -e "s|%SAGATOR_VERSION%|$VERSION|g" \
    -e "s|%SAGATOR_RELEASE%|$RELEASE|g" \
    -e "s|%SAGATOR_STABILITY%|$STABILITY|g" \
    -e "s|%UNSTABLE_RELEASE%|$UNSTABLE_RELEASE|g" \
    -e "s|%TIME_AND_DATE%|`date -R`|g" \
    -e "s|%OS%|$OS|g" \
    -e "s|%PREFIX%|$PREFIX|g" \
    -e "s|%LIBARCH%|$LIBARCH|g" \
    -e "s|%LIBDIR%|$LIBDIR|g" \
    -e "s|%PYTHON%|$PYTHON|g" \
    -e "s|%PYTHON_VER%|$PYTHON_VER|g" \
    -e "s|%PYTHON_PREFIX%|$PYTHON_PREFIX|g" \
    -e "s|%PYGETTEXT%|$PYGETTEXT|g" \
    -e "s|%APACHE_ROOT%|$APACHE_ROOT|g" \
    -e "s|%WEBDIR%|$WEBDIR|g" \
    -e "s|%LOGWATCH_CONF%|$LOGWATCH_CONF|g" \
    -e "s|%LOGWATCH_SCRIPTS%|$LOGWATCH_SCRIPTS|g" \
    < ${mf}.in > $mf
done

if [ "$FILELIST" ]; then
  (
    SPEC_CONF="%config(noreplace) %verify(not md5 size mtime)"
    if [ "$OS" = "suse" ]; then
      echo "/usr/sbin/rcsagator" # rc script symlink
    else
      echo "$SPEC_CONF $LOGWATCH_CONF/services/sagator.conf"
      echo "$SPEC_CONF $LOGWATCH_CONF/logfiles/sagator.conf"
      echo "$LOGWATCH_SCRIPTS/services/sagator"
    fi
    if [ "$WEBDIR" ]; then
      echo "%attr(0775,vscan,vscan) %dir $WEBDIR"
      echo "$SPEC_CONF $WEBDIR/index.html"
    fi
  ) > $FILELIST
fi

exit 0
