#!/bin/sh -e
prefix=$HOME/squid
srcdirs="$HOME/SRC/squid/SQUIDCACHE $HOME/SRC/squid/SF"

version=$1

shift
if [ -z "$version" ]; then
    echo "Usage: $0 version [configure arguments...]" >&2
    exit 1
fi
# Normal compile warnings
#	CFLAGS="$CFLAGS -ansi"			# naa.. we need library extensions
#	CFLAGS="$CFLAGS -pedantic"		# naa.. we need library extensions
	CFLAGS="$CFLAGS -Werror"		# required
	CFLAGS="$CFLAGS -Wall" 			# required
#	CFLAGS="$CFLAGS -W" 			# good in time...
#	CFLAGS="$CFLAGS -Wchar-subscript"	# good, but does not work
	CFLAGS="$CFLAGS -Wcomment"		# good
#	CFLAGS="$CFLAGS -Wshadow"		# good in audit, not normally..
	CFLAGS="$CFLAGS -Wpointer-arith"	# good
#	CFLAGS="$CFLAGS -Wbad-function-cast"	# naa.. this is stupid
#	CFLAGS="$CFLAGS -Wcast-qual"		# good in audit, not normally..
	CFLAGS="$CFLAGS -Wcast-align"		# good
	CFLAGS="$CFLAGS -Wwrite-strings"	# good
#	CFLAGS="$CFLAGS -Wconversion"		# maybe..
#	CFLAGS="$CFLAGS -Wsign-compare"		# good in time..
#	CFLAGS="$CFLAGS -Waggregate-return"	# naa.. we rely on this
	CFLAGS="$CFLAGS -Wstrict-prototypes"	# good
	CFLAGS="$CFLAGS -Wmissing-prototypes"	# required
	CFLAGS="$CFLAGS -Wmissing-declarations"	# good
#	CFLAGS="$CFLAGS -Wredundant-decls"	# good but fails with glibc
#	CFLAGS="$CFLAGS -Wnested-externs"	# naa.. I like them
#	CFLAGS="$CFLAGS -Winline"		# don't care

# Helper compile warnings
#	H_CFLAGS="$H_CFLAGS -ansi"		# naa.. we need library extensions
#	H_CFLAGS="$H_CFLAGS -pedantic"		# naa.. we need library extensions
	H_CFLAGS="$H_CFLAGS -Werror"		# required
	H_CFLAGS="$H_CFLAGS -Wall" 		# required
#	H_CFLAGS="$H_CFLAGS -W" 		# good in time...
#	H_CFLAGS="$H_CFLAGS -Wchar-subscript"	# good, but does not work
	H_CFLAGS="$H_CFLAGS -Wcomment"		# good
#	H_CFLAGS="$H_CFLAGS -Wshadow"		# good in audit, not normally..
	H_CFLAGS="$H_CFLAGS -Wpointer-arith"	# good
#	H_CFLAGS="$H_CFLAGS -Wbad-function-cast"# naa.. this is stupid
#	H_CFLAGS="$H_CFLAGS -Wcast-qual"	# good in audit, not normally..
	H_CFLAGS="$H_CFLAGS -Wcast-align"	# good
#	H_CFLAGS="$H_CFLAGS -Wwrite-strings"	# good
#	H_CFLAGS="$H_CFLAGS -Wconversion"	# maybe..
#	H_CFLAGS="$H_CFLAGS -Wsign-compare"	# good in time..
#	H_CFLAGS="$H_CFLAGS -Waggregate-return"	# naa.. we rely on this
#	H_CFLAGS="$H_CFLAGS -Wstrict-prototypes"	# good
#	H_CFLAGS="$H_CFLAGS -Wmissing-prototypes"	# required
#	H_CFLAGS="$H_CFLAGS -Wmissing-declarations"	# good
#	H_CFLAGS="$H_CFLAGS -Wredundant-decls"	# good but fails with glibc
#	H_CFLAGS="$H_CFLAGS -Wnested-externs"	# naa.. I like them
#	H_CFLAGS="$H_CFLAGS -Winline"		# don't care

if [ -f $version/build ]; then
    exec $version/build
fi
for dir in $srcdirs; do
    if [ -d $dir/$version ]; then
	srcdir="$dir/$version"
	break;
    fi
done
if [ -z "$srcdir" ]; then
    echo "Error: Cannot find squid version $version" >&2
    exit 2
fi
build=$PWD/`basename $0`
mkdir -p $version
cd $version
options="
    --disable-dependency-tracking
    --with-aio
    --with-pthreads
    --with-aufs-threads=48
    --enable-debug-cbdata
    --enable-storeio
    --enable-removal-policies
    --enable-stacktraces
    --prefix=/home/henrik/squid/$version
    --enable-ssl
    --enable-auth
    --enable-basic-auth-helpers
    --enable-ntlm-auth-helpers
    --enable-digest-auth-helpers
    --enable-cache-digests
    --enable-auth-on-acceleration
    --enable-snmp
    --enable-x-accelerator-vary
    --enable-delay-pools
    --enable-arp-acl
    --enable-large-files
    --enable-htcp
    "
#   --datadir=\${prefix}/share/squid
#   --libexecdir=\${prefix}/libexec/squid
if [ -f options ]; then
    options="$options `cat options`"
fi
builddir=$PWD

(
# Check autoconf scripts
cd $srcdir
#if [ ! -f configure -o configure.in -nt configure ]; then
#    autoconf
#fi
#if [ ! -f include/autoconf.h.in -o acconfig.h -nt include/autoconf.h.in -o configure.in -nt include/autoconf.h.in ]; then
#   autoheader
#fi
# Check icons
if [ ! -f icons/anthony-text.gif ]; then
  cd icons
  sh icons.shar
fi
cd $builddir

if [ -f build.sh ]; then
    exec ./build.sh
fi
if [ -f $srcdir/build.sh ]; then
    exec $srcdir/build.sh
fi

# Configure
do_config=
if [ ! -f config.status -o $srcdir/configure -nt config.status ]; then
    do_config=1
fi
#if [ ! $do_config ] && [ $build -nt config.status ]; then
#    rm -f config.status
#    do_config=1
#fi
#if [ ! $do_config ] && [ -f config.status ]; then
#    files=`grep "CONFIG_FILES=" config.status`
#    eval $files
#    for file in $CONFIG_FILES; do
#	if [ ! -f $file -o $srcdir/$file.in -nt $file -o config.status -nt $file ]; then
#	   do_status=1
#	   break
#	fi
#    done
#fi

if [ $do_config ] && [ -f config.status ] && [ config.status -nt $build ]; then
    ./config.status --recheck
    ./config.status
elif [ $do_config ]; then
    $srcdir/configure --silent $options
elif [ $do_status ]; then
    ./config.status
fi

# Build
set -x
if [ -d src/auth ]; then
    make -C lib AM_CFLAGS="$CFLAGS"
    make -C src/auth/basic/helpers AM_CFLAGS="$H_CFLAGS"
    make -C src/auth/ntlm/helpers AM_CFLAGS="$H_CFLAGS"
    make -C src/auth/digest/helpers AM_CFLAGS="$H_CFLAGS"
fi
make "$@" install AM_CFLAGS="$CFLAGS"
) 2>&1 | tee build.log

