#!/bin/sh
#
# Copyright (c) 2008 Marco A. Frias B. <marcofrias@linuxpackages.net>
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
#
# Based on Template SlackBuild written by Eric Hameleers <alien@slackware.com>
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild Script
# ===========================
#
# By:    Marco A. Frias B. <marcofrias@linuxpackages.net>
# For:   squid
# Descr: Proxy Caching Server
# URL:   http://www.squid-cache.org/
# Needs: -.-
#
# Changelog:
#
# -- Slackware 12.1
#
# 3.0.STABLE9-1: 15/Sep/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#                * Initial build.
#
# NOTE:
#      Run 'sh squid.SlackBuild --cleanup' to build a Slackware package.
# -----------------------------------------------------------------------------

## Name and version of the package
PRGNAME=squid
VERSION=3.0.STABLE9

## The arch we will build for
ARCH=${ARCH:-i486}

## Build tag
RELEASE=1
BUILDTAG=mfb

## The documentation files:
DOCFILES="ChangeLog CONTRIBUTORS COPYING COPYRIGHT CREDITS INSTALL QUICKSTART \
          README SPONSORS TODO RELEASENOTES.html"

## Where do we look for sources?
CWD=$(pwd)
SRCDIR=$(dirname $0)
[ "${SRCDIR:0:1}" == "." ] && SRCDIR=$CWD/${SRCDIR}

## Place to build (TMP) package (PKG) and output (OUTPUT) the program:
TMP=${TMP:-/tmp/build}
PKG=$TMP/package-${PRGNAME}
OUTPUT=${OUTPUT:-/usr/src/slackpackages}

## Full path of source:
SOURCE="$SRCDIR/${PRGNAME}-${VERSION}.tar.bz2"

##
## --- with a little luck, you won't have to edit below this point ---
##

## Exit the script on errors:
set -e
trap 'echo "$0 FAILED at line ${LINENO}" | tee $TMP/error-${PRGNAME}.log' ERR

## Catch unitialized variables:
set -u
P1=${1:-1}

case "$ARCH" in
  i386)      SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  i486)      SLKCFLAGS="-O2 -march=i486 -mtune=i686"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  s390)      SLKCFLAGS="-O2"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  powerpc)   SLKCFLAGS="-O2"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  x86_64)    SLKCFLAGS="-O2 -fPIC"
             SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
             ;;
  athlon-xp) SLKCFLAGS="-march=athlon-xp -O3 -pipe -fomit-frame-pointer"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
esac

## Setup the number of jobs (commands) to run simultaneously:
NUMCPU=$(egrep -c ^cpu[0-9]+ /proc/stat || :)

if [ "$NUMCPU" = "0" ]; then
  NUMCPU=1
fi

## Create working directories:
mkdir -p $OUTPUT             # place for the package to be saved
mkdir -p $TMP/tmp-${PRGNAME} # location to build the source
mkdir -p $PKG                # place for the package to be built

rm -rf $PKG/*                # always erase old package's contents
rm -rf $TMP/tmp-${PRGNAME}/* # remove the remnants of previous build

## Source file availability:
if ! [ -f ${SOURCE} ]; then
  echo -e "\E[1;31m"
  echo -ne "File '$(basename ${SOURCE})' not available... aborting the build."
  echo -e "\E[0m\n"
  exit 1
fi

## --- PACKAGE BUILDING ---
cd $TMP/tmp-${PRGNAME}

echo -e "\E[1;32m"
echo -ne "Extracting the source archive(s) for $PRGNAME..."
echo -e "\E[0m\n"
sleep 1

if $(file ${SOURCE} | grep -q ": bzip2"); then
  tar -xjvf ${SOURCE}
elif $(file ${SOURCE} | grep -q ": gzip"); then
  tar -xzvf ${SOURCE}
fi

cd ${PRGNAME}-${VERSION}
find . \( -perm 777 -o -perm 775 -o -perm 711 \) -exec chmod 755 {} \;
find . \( -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
find . \( -perm 666 -o -perm 664 -o -perm 600 \) -exec chmod 644 {} \;
find . \( -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
chown -R root.root .

echo -e "\E[1;32m"
echo -ne "Building $PRGNAME..."
echo -e "\E[0m\n"
sleep 1

LDFLAGS="$SLKLDFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
CFLAGS="$SLKCFLAGS" \
./configure \
    --prefix=/usr \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --sysconfdir=/etc/squid \
    --localstatedir=/var/log/squid \
    --datadir=/usr/share/squid \
    --mandir=/usr/man \
    --enable-shared \
    --disable-static \
    --enable-async-io \
    --enable-delay-pools \
    --enable-icap-client \
    --enable-snmp \
    --enable-arp-acl \
    --enable-ssl \
    --enable-linux-netfilter \
    --enable-auth \
    --with-large-files \
    --program-prefix="" \
    --program-suffix="" \
    --build=$ARCH-slackware-linux 2>&1 | tee $TMP/configure-${PRGNAME}.log

make -j$NUMCPU 2>&1 | tee $TMP/make-${PRGNAME}.log

echo -e "\E[1;32m"
echo -ne "Installing $PRGNAME..."
echo -e "\E[0m\n"
sleep 1

make DESTDIR=$PKG install 2>&1 | tee $TMP/install-${PRGNAME}.log

rm -rf $PKG/var
rm -rf $PKG/etc/squid/squid.conf

# Installing squid config files:
mv $PKG/etc/squid/cachemgr.conf $PKG/etc/squid/cachemgr.conf.new
mv $PKG/etc/squid/mime.conf $PKG/etc/squid/mime.conf.new
cp $SRCDIR/squid.conf $PKG/etc/squid/squid.conf.new
find $PKG/etc -type d -exec chmod 755 {} \;
find $PKG/etc -type f -exec chmod 644 {} \;

# Installing logrotate config files:
mkdir -p $PKG/etc/logrotate.d
install -m 644 $SRCDIR/squid.logrotate $PKG/etc/logrotate.d/squid

# Installing squid service:
mkdir -p $PKG/etc/rc.d
install -m 644 $SRCDIR/rc.squid $PKG/etc/rc.d/rc.squid.new

## Add documentation:
mkdir -p $PKG/usr/doc/${PRGNAME}-${VERSION}/sample
cp -a $DOCFILES $PKG/usr/doc/${PRGNAME}-${VERSION} || true
cp -a $SRCDIR/files.executable.acl $SRCDIR/files.multimedia.acl \
      $SRCDIR/squid.files.conf $SRCDIR/squid.squidguard.conf \
      $PKG/usr/doc/${PRGNAME}-${VERSION}/sample || true
find $PKG/usr/doc/${PRGNAME}-${VERSION} -type d -exec chmod 755 {} \;
find $PKG/usr/doc/${PRGNAME}-${VERSION} -type f -exec chmod 644 {} \;
chown -R root.root $PKG/usr/doc/${PRGNAME}-${VERSION}

## Compress man pages:
if [ -d $PKG/usr/man ]; then
  (cd $PKG/usr/man
   for manpagedir in $(find . -type d -name "man*"); do
   (cd $manpagedir
    for eachpage in $(find . -type l -maxdepth 1); do
      ln -s $( readlink $eachpage ).gz $eachpage.gz ; rm $eachpage
    done
    gzip -9 *.?
   )
   done
  )
fi

# Strip binaries:
find $PKG | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

echo -e "\E[1;32m"
echo -ne "Building package..."
echo -e "\E[0m\n"
sleep 1

## Add slackbuild files:
mkdir -p $PKG/usr/src/slackbuilds/${PRGNAME}-${VERSION}

(cd $PKG/usr/src/slackbuilds/${PRGNAME}-${VERSION}
install -m 644 $SRCDIR/slack-desc .
install -m 644 $SRCDIR/doinst.sh .
install -m 644 $SRCDIR/${PRGNAME}.info .
install -m 644 $SRCDIR/${PRGNAME}.SlackBuild .
install -m 644 $SRCDIR/rc.${PRGNAME} .
install -m 644 $SRCDIR/${PRGNAME}.logrotate .
install -m 644 $SRCDIR/${PRGNAME}.conf .
install -m 644 $SRCDIR/files.executable.acl .
install -m 644 $SRCDIR/files.multimedia.acl .
install -m 644 $SRCDIR/${PRGNAME}.files.conf .
install -m 644 $SRCDIR/${PRGNAME}.squidguard.conf .
)

# Add a package description:
mkdir -p $PKG/install
install -m 644 $SRCDIR/slack-desc $PKG/install
install -m 644 $SRCDIR/doinst.sh $PKG/install

## Generated slack-required:
requiredbuilder -y -v $PKG

## Build the package:
cd $PKG
makepkg -l y -c n $OUTPUT/${PRGNAME}-${VERSION}-${ARCH}-${RELEASE}${BUILDTAG}.tgz \
  2>&1 | tee $TMP/makepkg-${PRGNAME}.log

echo -e "\E[1;32m"
echo -ne "Your $PRGNAME package is complete. It is located in $OUTPUT"
echo -e "\E[0m\n"

if [ "$P1" = "--cleanup" ]; then
  rm -rf $TMP/tmp-${PRGNAME}
  rm -rf $PKG
fi
