#!/bin/sh
#
# Based on the Slackware 10.2 SlackBuild
#
# http://www.squid-cache.org/ 
# modyfied by Xavier (cbastos at connection.com.br)

ulimit -n 8192

CWD=`pwd`
if ["$TMP" = ""]; then
TMP=/tmp
fi

PKGNAME=squid
VERSION=2.6.STABLE14
ARCH=${ARCH:-i486}
BUILD=1xav

PKG=$TMP/package-$PKGNAME
PACKAGE=$PKGNAME-$VERSION-$(echo $ARCH | tr "-" "_")-$BUILD.tgz

SLKCFLAGS="-O2 -march=$ARCH -pipe -fomit-frame-pointer"

case $ARCH in
  i[345]86) SLKCFLAGS="$SLKCFLAGS -mtune=i686" ;;
  pentium4|pentium-m) SLKCFLAGS="$SLKCFLAGS -mfpmath=sse -msse2" ;;
  athlon-xp) SLKCFLAGS="$SLKCFLAGS -mfpmath=sse"
esac

if [ ! -d $TMP ]; then
mkdir -p $TMP
fi

if [ ! -e $PKGNAME-$VERSION.tar.bz2 ]; then
  wget -nv http://www.squid-cache.org/Versions/v2/2.6/$PKGNAME-$VERSION.tar.bz2  || exit 1
fi

cd $TMP

if [ -d $PKG ]; then
rm -rf $PKG
fi

mkdir -p $PKG
mkdir $PKG/usr

rm -rf $PKGNAME-$VERSION
tar xjvf $CWD/$PKGNAME-$VERSION.tar.bz2 || exit 1

cd $PKGNAME-$VERSION


chown -R root.root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure --with-maxfd=8192 \
	--prefix=/usr \
	--sysconfdir=/etc/squid \
	--localstatedir=/var/log/squid \
	$ARCH-slackware-linux

make all
make install DESTDIR=$PKG

install -d $PKG/usr/doc/$PKGNAME-$VERSION
install -m 644 CONTRIBUTORS \
	       COPYING \
	       COPYRIGHT \
	       CREDITS \
	       ChangeLog \
	       INSTALL \
	       QUICKSTART \
	       README \
	       RELEASENOTES.html \
	       SPONSORS \
	       $PKG/usr/doc/$PKGNAME-$VERSION

gzip $PKG/usr/man/man8/*

chown -R root.bin $PKG/usr/sbin
chown -R root.bin $PKG/usr/bin

mv $PKG/etc/squid/mime.conf $PKG/etc/squid/mime.conf.new
mv $PKG/etc/squid/squid.conf $PKG/etc/squid/squid.conf.new
mv $PKG/etc/squid/cachemgr.conf $PKG/etc/squid/cachemgr.conf.new

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

install -d $PKG/install
install -m 644 $CWD/slack-desc $PKG/install/slack-desc
install -m 644 $CWD/doinst.sh $PKG/install/doinst.sh

install -d $PKG/usr/src/SlackBuilds/$PKGNAME
install -m 644 $CWD/$PKGNAME.SlackBuild \
	       $CWD/slack-desc \
	       $CWD/doinst.sh \
               $CWD/rc.squid \
	       $CWD/$NAME.SlackBuild \
               $PKG/usr/src/SlackBuilds/$PKGNAME

install -d $PKG/etc/rc.d
install -m 644 $CWD/rc.squid \
	       $PKG/etc/rc.d
 
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
install -m 644 $CWD/slack-required $PKG/install/slack-required

makepkg -l y -c n $CWD/$PKGNAME-$VERSION-$ARCH-$BUILD.tgz
cd $CWD
md5sum $PACKAGE > $PACKAGE.md5

if [ "$1" = "--cleanup" ]; then
rm -rf $TMP/$PKGNAME-$VERSION
rm -rf $PKG
fi


