#!/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]
	--datadir DIR		data directory [$PREFIX/share]
	--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
	--distribution X        set target distribution to X
EOF
}

parse_arg() {
  if [ "$1" = "--help" ]; then
    show_help
    exit
  elif [ "$1" = "--prefix" ]; then
    PREFIX="$2"
    SHIFT=1
  elif [ "$1" = "--datadir" ]; then
    DATADIR="$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"
    SHIFT=0
  elif [ "$1" = "--distribution" ]; then
    TARGET_DISTRIBUTION="$2"
    SHIFT=1
  else
    echo "Unknown command line parameter: $1"
    exit
  fi
}

# check args
while [ "$1" ]; do
  SHIFT=1
  ARG1="$1"
  ARG1M="`echo $1 | sed 's/^\(--[a-z-]*\)=\(.*\)$/\\1 \\2/'`"
  shift 1
  parse_arg $ARG1M "$@"
  if [ "$ARG1M" = "$ARG1" ]; then
    shift $SHIFT
  fi
done

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

# 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
elif [ -f /etc/os-release ]; then
  if grep -qi suse /etc/os-release; then
    OS=suse
  fi
fi
echo "$OS"

echo -n "checking for datadir... "
if [ -z "$DATADIR" ]; then
  DATADIR="share"
fi
PROGDIR="$PREFIX/$DATADIR/sagator"
echo "$DATADIR ($DATADIR)"

# check for python
echo -n "checking for python... "
if [ -z "$PYTHON" ]; then
  for v in 2 3; do
    which python$v >/dev/null 2>&1
  done
  PYTHON="python$v"
fi
PYTHON_VER=`$PYTHON -c '
import sys
print("python"+str(sys.version_info[0])+"."+str(sys.version_info[1]))
'`
PYTHON_PREFIX=`$PYTHON-config --prefix 2>/dev/null || echo /usr`
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/dist.conf ]; then
    LOGWATCH_CONF=/usr/share/logwatch/dist.conf
  elif [ -d /etc/log.d/conf ]; then
    LOGWATCH_CONF=/etc/log.d/conf
  elif [ -d /etc/logwatch/conf ]; then
    LOGWATCH_CONF=/etc/logwatch/conf
  elif [ -d /usr/share/logwatch/default.conf ]; then
    LOGWATCH_CONF=/usr/share/logwatch/default.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
  elif [ -d /etc/logwatch/scripts ]; then
    LOGWATCH_SCRIPTS=/etc/logwatch/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|%DOWNLOAD_DIR%|$DOWNLOAD_DIR|g" \
    -e "s|%TARGET_DISTRIBUTION%|$TARGET_DISTRIBUTION|g" \
    -e "s|%TIME_AND_DATE%|`LANG=C date "+%a, %d %b %Y %T %z"`|g" \
    -e "s|%MONTH%|`LANG=C date '+%b'`|g" \
    -e "s|%DAY%|`LANG=C date '+%e'`|g" \
    -e "s|%YEAR%|`LANG=C date '+%Y'`|g" \
    -e "s|%WD%|`LANG=C date '+%a'`|g" \
    -e "s|%OS%|$OS|g" \
    -e "s|%PREFIX%|$PREFIX|g" \
    -e "s|%DATADIR%|$DATADIR|g" \
    -e "s|%PROGDIR%|$PROGDIR|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)"
    echo "$SPEC_CONF $LOGWATCH_CONF/services/sagator.conf"
    echo "$SPEC_CONF $LOGWATCH_CONF/logfiles/sagator.conf"
    echo "$LOGWATCH_SCRIPTS/services/sagator"
    #if [ "$OS" = "suse" ]; then
    #  echo "/usr/sbin/rcsagator" # rc script symlink
    #fi
    if [ "$WEBDIR" ]; then
      echo "%attr(0775,vscan,vscan) %dir $WEBDIR"
      echo "$SPEC_CONF $WEBDIR/index.html"
    fi
  ) > $FILELIST
fi

exit 0
