#!/bin/sh

[ -z $PREPKG ] && exit 1

PACKAGE=squid
VERSION=2.6.STABLE17
FILES=$(cd $(dirname $0) ; pwd)

# required:
# user:group = proxy:proxy = 23:23

# auth not built:
#   negotiate
# basic-auth-helpers not built:
#   DB,PAM,POP3,mswin_sspi,squid_radius_auth
# digest-auth-helpers not built:
#   eDirectory
# external-acl-helpers not built:
#   mswin_lm_group,session
# negotiate-auth-helpers not built:
#   mswin_sspi,squid_kerb_auth
# ntlm-auth-helpers not built:
#   mswin_sspi

# my sanity patch
PATCHES="
$PACKAGE-$VERSION-defaults.diff.gz
"

for PATCH in $PATCHES ; do
  zcat $FILES/patches/$PATCH | patch -p1 &> $PATCH.output
done

mkdir -p $PREPKG/{install,etc/{logrotate.d,rc.d},usr/doc/$PACKAGE-$VERSION/{contrib,examples},var/cache/$PACKAGE}/

chmod -R u+w,go-w .
chown -R root:root .
CFLAGS="-O2 -march=i486 -mtune=i686" \
  ./configure \
  --prefix=/usr \
  --datadir=/usr/share/$PACKAGE \
  --libexecdir=/usr/lib/$PACKAGE \
  --localstatedir=/var \
  --mandir=/usr/man \
  --sysconfdir=/etc/$PACKAGE \
  --enable-arp-acl \
  --enable-auth=basic,digest,ntlm \
  --enable-basic-auth-helpers=LDAP,MSNT,NCSA,SASL,SMB,YP,getpwnam,multi-domain-NTLM \
  --enable-cache-digests \
  --enable-carp \
  --enable-delay-pools \
  --enable-digest-auth-helpers=ldap,password \
  --enable-external-acl-helpers=ip_user,ldap_group,unix_group,wbinfo_group \
  --enable-htcp \
  --enable-icmp \
  --enable-linux-netfilter \
  --enable-ntlm-auth-helpers=SMB,fakeauth,no_check \
  --enable-poll \
  --enable-referer-log \
  --enable-removal-policies=heap,lru \
  --enable-snmp \
  --enable-ssl \
  --enable-storeio=aufs,coss,diskd,null,ufs \
  --enable-useragent-log \
  --enable-wccp \
  --enable-wccpv2 \
  --with-pthreads \
  --build=i486-slackware-linux &> configure.output || exit 1
make $MAKEJOBS all &> make.output || exit 1
make DESTDIR=$PREPKG install &> make_install.output || exit 1

# post install tweaks

# 'install-pinger' target doesn't grok DESTDIR, do things manually
chmod 4711 $PREPKG/usr/lib/$PACKAGE/pinger

# we do not want these in the package, if they don't exist on the system
# then we copy the default one in place as the real file
mv $PREPKG/etc/$PACKAGE/cachemgr.conf{,.default}
rm -f $PREPKG/etc/$PACKAGE/{mime,msntauth,squid}.conf

# contributed tidbits
( cd contrib/
  cp -p rredir.c rredir.pl url-normalizer.pl user-agents.pl \
    $PREPKG/usr/doc/$PACKAGE-$VERSION/contrib/ )

# example config(s)
cp -p helpers/external_acl/ip_user/example.conf \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/examples/ip_user-example.conf
cp -p helpers/external_acl/ip_user/example-deny_all_but.conf \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/examples/ip_user-example-deny_all_but.conf

# READMEs for authentication modules (incomplete, but it'll do for now)
cp -p helpers/basic_auth/LDAP/README \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.ldap_auth
cp -p helpers/basic_auth/MSNT/README.html \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.msnt_auth.html
head -n +18 helpers/basic_auth/NCSA/ncsa_auth.c > \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.ncsa_auth
cp -p helpers/basic_auth/SASL/README \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.sasl_auth
cp -p helpers/basic_auth/SMB/README \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.smb_auth
head -n +20 helpers/basic_auth/getpwnam/getpwnam_auth.c > \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.getpwname_auth
cp -p helpers/basic_auth/multi-domain-NTLM/README.txt \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.smb_auth_pl
head -n +31 helpers/digest_auth/password/digest_pw_auth.c > \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.digest_pw_auth
cp -p helpers/external_acl/ip_user/README \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.ip_user
cp -p helpers/external_acl/ldap_group/README \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.ldap_group
cp -p helpers/external_acl/unix_group/README \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.unix_group
cp -p helpers/ntlm_auth/no_check/README.no_check_ntlm_auth \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/README.auth_module.no_check_ntlm_auth

# base docs
cp -p CONTRIBUTORS COPYING COPYRIGHT CREDITS ChangeLog \
  INSTALL QUICKSTART README RELEASENOTES.html SPONSORS \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/

gzip -r9 $PREPKG/usr/man/

chmod o-rwx $PREPKG/var/{cache,log}/$PACKAGE/
chown proxy:proxy $PREPKG/var/{cache,log}/$PACKAGE/

cp $FILES/{doinst.sh,slack-desc} $PREPKG/install/
cp $FILES/logrotate.squid.new $PREPKG/etc/logrotate.d/squid.new
cp $FILES/rc.squid.new $PREPKG/etc/rc.d/

find $PREPKG -exec file {} \; \
  | awk -F : '/ ELF / {print $1}' \
  | xargs strip -p --strip-unneeded
