#!/bin/sh
# Configure script
# $Id: configure,v 1.9 2007/10/18 07:40:56 maxim Exp $
#

prefix="/var/0W";
owner="nobody";
group="nobody";
proxy="ON";
antiflood="ON";
access="ON";
country="OFF";
patterns="OFF";
cache="OFF";

filter="OFF";
throttle="OFF";
remap="OFF";
elapsed="OFF";
languages="OFF";
debug="OFF";
headers="15";

cflags="-I.."
ldflags=""
opt_flags="-s -O3"
deb_flags="-g -O0"

for option do
	case "$option" in
        --*=*) value=`echo "$option" | sed 's/^[^=]*=//'`
               VALUE=`echo "$value" | tr '[:lower:]' '[:upper:]'`;;
        esac
	case "$option" in
	--help|-h) help="yes";;
	--prefix=*) prefix="$value";;
	--model=*) model="$VALUE";;
	--transfer=*) transfer="$VALUE";;
	--owner=*) owner="$value";;
	--group=*) group="$value";;
	--proxy) proxy="ON";;
	--proxy=*) proxy="$VALUE";;
	--country) country="ON";;
	--country=*) country="$VALUE";;
	--antiflood) antiflood="ON";;
	--antiflood=*) antiflood="$VALUE";;
	--patterns) patterns="ON";;
	--patterns=*) patterns="$VALUE";;
	--access) access="ON";;
	--access=*) access="$VALUE";;
	--cache) cache="ON";;
	--cache=*) cache="$VALUE";;

        # undocumented
	--filter) filter="ON";;
	--filter=*) filter="$VALUE";;
	--remap) remap="ON";;
	--remap=*) remap="$VALUE";;
	--elapsed) elapsed="ON";;
	--elapsed=*) elapsed="$VALUE";;
	--headers) headers=15;;
	--headers=*) headers="$VALUE";;
	--without-headers) headers=0;;
	--debug) debug="ON";;
	--throttle) throttle="ON";;
	--throttle=*) throttle="$VALUE";;
	--languages) languages="ON";;
	--languages=*) languages="$VALUE";;

        *) echo "unknown option: $option"
           echo "try './configure --help' for help"
           exit;;
	esac
done

if [ -n "$help" ] ; then

cat << EOH

Usage: configure [options]

Options: [defaults in brackets after descriptions]


 --help, -h        print this message

 --prefix=PREFIX   install 0W-httpd to directory PREFIX [$prefix]

 --owner=OWNER     run 0W-httpd as OWNER [$owner]
 --group=GROUP     run 0W-httpd as GROUP [$group]


Support options:  --name[=ON]  or  --name=OFF

 --proxy           reverse proxy support [$proxy]

 --country         country support (write TLD to log and
                   set X-Country  header for backend) [$country]

 --antiflood       antiflood support (limit number of
                   simultaneous connections from one IP) [$antiflood]

 --access          support for access options [$access]

 --patterns        shell-patterns support for server names [$patterns]

 --cache           support for in-memory cache [$cache]


Some options it's safely to not set:

 --model=MODEL     force model to MODEL [$model]
                   RTSIG: real-time signals (for Linux)
                   KEVENT: kevent (for FreeBSD)
                   POLL: poll (for others)

 --transfer=MODE   force transfer mode to MODE [$transfer]
                   SENDFILE: use sendfile (for Linux and FreeBSD)
                   MMAP: use mmap (poorly tested)
                   PLAIN: user read/write (poorly tested)

EOH
exit
fi;

test "$proxy" = "ON" && PROXY="1" && SUPPORT="proxy $SUPPORT" || PROXY="0"
test "$country" = "ON" && COUNTRY="1" && SUPPORT="country $SUPPORT" || COUNTRY="0"
test "$antiflood" = "ON" && ANTIFLOOD="1"&& SUPPORT="antiflood $SUPPORT" || ANTIFLOOD="0"
test "$throttle" = "ON" && THROTTLE="1" && SUPPORT="throttle $SUPPORT" || THROTTLE="0"
test "$languages" = "ON" && LANGUAGES="1" && SUPPORT="languages $SUPPORT" || LANGUAGES="0"
test "$filter" = "ON" && FILTER="1" && SUPPORT="filter $SUPPORT" || FILTER="0"
test "$patterns" = "ON" && PATTERNS="1" && SUPPORT="patterns $SUPPORT" || PATTERNS="0"
test "$access" = "ON" && ACCESS="1"&& SUPPORT="access $SUPPORT" || ACCESS="0"
test "$remap" = "ON" && REMAP="1"&& SUPPORT="remap $SUPPORT" || REMAP="0"
test "$elapsed" = "ON" && ELAPSED="1"&& SUPPORT="elapsed $SUPPORT" || ELAPSED="0"
test "$cache" = "ON" && CACHE="1"&& SUPPORT="cache $SUPPORT" || CACHE="0"

if [ "$debug" = "ON" ]; then
	DEBUG="1"
        cflags="$cflags $deb_flags"
        ldflags="$ldflags $deb_flags"
	SUPPORT="debug $SUPPORT"
else
	DEBUG="0"
        cflags="$cflags $opt_flags"
        ldflags="$ldflags $opt_flags"
fi



test -n "$model" && MODEL="#define MODEL MODEL_$model"
test -n "$transfer" && TRANSFER="#define TRANSFER_MODE TRANSFER_$transfer"

CONFIGURE=`echo "$*" | tr -d '"'`

cat > src/configure.h << EOF  || exit
/*
  Automatically generated by:

  $0 $*

 */

#ifndef CONFIGURE_H
#define CONFIGURE_H


#define CONFIGURE         "$CONFIGURE"

#define ROOT              "$prefix"

#define SUPPORT_PROXY     $PROXY
#define SUPPORT_COUNTRY   $COUNTRY
#define SUPPORT_ANTIFLOOD $ANTIFLOOD
#define SUPPORT_THROTTLE  $THROTTLE
#define SUPPORT_LANGUAGES $LANGUAGES
#define SUPPORT_FILTER    $FILTER
#define SUPPORT_PATTERNS  $PATTERNS
#define SUPPORT_ACCESS    $ACCESS
#define SUPPORT_REMAP     $REMAP
#define SUPPORT_ELAPSED   $ELAPSED
#define SUPPORT_CACHE     $CACHE

#define MAX_OTHER_HEADERS $headers

#define DEBUGLOG          $DEBUG

#define SUPPORT_RUS       0

$MODEL
$TRANSFER

#endif

EOF

case `uname -s` in
	Linux)		cflags="$cflags -pipe -Wall"
				ldflags="$ldflags -lcrypt";;
	FreeBSD)	cflags="$cflags -pipe -Wall"
				ldflags="$ldflags -lcrypt";;
	Darwin)		cflags="$cflags -pipe -Wall -traditional-cpp"
				ldflags="$ldflags";;
	*)			cflags="$cflags -pipe -Wall"
				ldflags="$ldflags -lcrypt";;
esac;

sed "\
s|^ROOT=.*|ROOT=$prefix|;\
s|^OWNER=.*|OWNER=$owner|;
s|^GROUP=.*|GROUP=$group|;\
s|^CFLAGS=.*|CFLAGS=$cflags|;\
s|^LDFLAGS=.*|LDFLAGS=$ldflags|;\
" \
src/Rules.make > .rules && mv .rules src/Rules.make || exit


cat << EOF
Configured:
 Root directory: $prefix
 Run as: $owner:$group
 Supports: $SUPPORT

Run: make install
EOF

