#!/bin/sh

# default settings
PREFIX="/usr/local"

# check args
while [ "$1" ]; do
  if [ "$1" = "--prefix" ]; then
    PREFIX="$2"
    shift 1
  fi
  if [ "$1" = "--libdir" ]; then
    LIBDIR="$2"
    shift 1
  fi
  if [ "$1" = "--python" ]; then
    PYTHON="$2"
    shift 1
  fi
  if [ "$1" = "--without-clamav" -o "$1" = "--disable-clamav" ]; then
    CLAMAV="no"
  fi
  if [ "$1" = "--without-dspam" -o "$1" = "--disable-dspam" ]; then
    DSPAM="no"
  fi
  if [ "$1" = "--logwatch-conf" ]; then
    LOGWATCH_CONF="$2"
    shift 1
  fi
  if [ "$1" = "--logwatch-scripts" ]; then
    LOGWATCH_SCRIPTS="$2"
    shift 1
  fi
  if [ "$1" = "--filelist" ]; then
    FILELIST="filelist"
  fi
  shift 1
done

echo -n "checking for my version... "
. ./version.py
if echo $RELEASE | grep "^[0-9]*$" >/dev/null; then
  STABILITY="stable"
else
  STABILITY="unstable"
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 clamav include
echo -n "checking for clamav... "
if [ -z "$CLAMAV" ]; then
CLAMAV="no"
cat > config.c << EOF
#include <clamav.h>
int main() {
  return CL_CLEAN;
}
EOF
gcc -lclamav config.c -o config.bin >config.log 2>&1
if [ -x ./config.bin ] && ./config.bin; then
  CLAMAV="yes"
fi
rm -f config.c config.bin config.log
fi
echo "$CLAMAV"

# check for dspam include
echo -n "checking for dspam... "
if [ -z "$DSPAM" ]; then
DSPAM="no"
for DSPAM_PREFIX in /usr /usr/local; do
cat > config.c << EOF
#include <libdspam.h>
int main() {
  return (LIBDSPAM_VERSION_MAJOR>3);
}
EOF
gcc -ldspam -I$DSPAM_PREFIX/include/dspam config.c -o config.bin >config.log 2>&1
if [ -x ./config.bin ] && ./config.bin; then
  DSPAM="yes"
  break
fi
done
rm -f config.c config.bin config.log
fi
if [ "$DSPAM" = "yes" ]; then
  echo "$DSPAM, in $DSPAM_PREFIX"
else
  echo "$DSPAM"
fi

# check for apache configuration files
echo -n "checking for apache... "
if [ -d /etc/apache/conf.d ]; then
  APACHE_ROOT=/etc/apache
fi
if [ -x /usr/sbin/apachectl ]; then
  APACHE_ROOT=`/usr/sbin/apachectl -V | grep HTTPD_ROOT | cut -d'"' -f2`
fi
if [ -z "$APACHE_ROOT" ]; then
  APACHE_ROOT=/etc/httpd
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"

echo "configured prefix... $PREFIX"

backslash() {
  echo "$@" | sed 's/\//\\\//g'
}

for mf in Makefile libclamav/Makefile pydspam/Makefile \
          scripts/sagator.spec debian/changelog; do
  echo -n "creating $mf ... "
  sed \
    -e "s/%SAGATOR_VERSION%/$VERSION/g" \
    -e "s/%SAGATOR_RELEASE%/$RELEASE/g" \
    -e "s/%SAGATOR_STABILITY%/$STABILITY/g" \
    -e "s/%TIME_AND_DATE%/`date -R`/g" \
    -e "s/%OS%/$OS/g" \
    -e "s/%PREFIX%/`backslash $PREFIX`/g" \
    -e "s/%LIBARCH%/`backslash $LIBARCH`/g" \
    -e "s/%LIBDIR%/`backslash $LIBDIR`/g" \
    -e "s/%PYTHON%/$PYTHON/g" \
    -e "s/%PYTHON_VER%/$PYTHON_VER/g" \
    -e "s/%PYTHON_PREFIX%/`backslash $PYTHON_PREFIX`/g" \
    -e "s/%CLAMAV%/$CLAMAV/g" \
    -e "s/%APACHE_ROOT%/`backslash $APACHE_ROOT`/g" \
    -e "s/%LOGWATCH_CONF%/`backslash $LOGWATCH_CONF`/g" \
    -e "s/%LOGWATCH_SCRIPTS%/`backslash $LOGWATCH_SCRIPTS`/g" \
    -e "s/%DSPAM%/$DSPAM/g" \
    -e "s/%DSPAM_PREFIX%/`backslash $DSPAM_PREFIX`/g" \
    < ${mf}.in > $mf
  echo "done."
done

if [ "$FILELIST" ]; then
echo "" > $FILELIST
[ "$OS" != "suse" ] && cat > $FILELIST << EOF
$LOGWATCH_CONF/services/sagator.conf
$LOGWATCH_CONF/logfiles/sagator.conf
$LOGWATCH_SCRIPTS/services/sagator
EOF
fi

exit 0
