#!/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" = "--show" ]; then
    SHOW="$2"
    shift 1
  fi
  if [ "$1" = "--filelist" ]; then
    FILELIST="filelist"
  fi
  shift 1
done

. ./version.py
if echo $RELEASE | grep "^[0-9]*$" ; then
  STABILITY="stable"
else
  STABILITY="unstable"
fi

# check for OS
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
fi

if [ -z "$LIBDIR" ]; then
  LIBDIR="$PREFIX/lib"
fi
LIBARCH=`basename $LIBDIR`

# check for python
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
if [ -z "$PYTHON" ]; then
  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

# check for clamav include
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

# check for dspam include
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

# check for apache configuration files
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

# check for logwatch configuration files
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

if [ "$SHOW" ]; then
  eval echo "\$$SHOW"
  exit 0
fi

echo "Configured settings:"
echo "  SAGATOR:  $VERSION-$RELEASE ($STABILITY)"
echo "  OS:       $OS"
echo "  Prefix:   $PREFIX"
echo "  Libdir:   $LIBDIR ($LIBARCH)"
echo "  Python:   $PYTHON, version $PYTHON_VER, in $PYTHON_PREFIX"
echo "  ClamAV:   $CLAMAV"
if [ "$DSPAM" = "yes" ]; then
  echo "  Dspam:    $DSPAM, in $DSPAM_PREFIX"
else
  echo "  Dspam:    $DSPAM"
fi
echo "  Apache:   $APACHE_ROOT"
echo "  Logwatch: $LOGWATCH_CONF, $LOGWATCH_SCRIPTS"

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

for mf in Makefile libclamav/Makefile pydspam/Makefile \
          scripts/sagator.spec debian/changelog; do
  echo -n "Generating $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
