#!/bin/sh
#
# Heavily based on the Slackware 12.1 SlackBuild
# http://www.privoxy.org
# Packager neongen ( neongen at libero.it)
# http://www.slacky.it

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

NAME=privoxy
VERSION=3.0.10
ARCH=${ARCH:-i486}
CHOST=i486
BUILD=1ng
PKG=$TMP/package-$NAME
SOURCE=http://dfn.dl.sourceforge.net/sourceforge/ijbswa/$NAME-$VERSION-stable-src.tar.gz

# It is strongly recommended to not run Privoxy as root. You should
# configure/install/run Privoxy as an unprivileged user, preferably by creating a
# "privoxy" user and group just for this purpose. See your local documentation
# for the correct command line to do add new users and groups (something like
# adduser, but the command syntax may vary from platform to platform).
#
# /etc/passwd might then look like:
#
#   privoxy:*:7777:7777:privoxy proxy:/no/home:/no/shell
#
#
# And then /etc/group, like:
#
#   privoxy:*:7777:

PRIVOXY_USER=privoxy
PRIVOXY_GROUP=privoxy

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

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

echo "+=============================================================================="
echo "| $NAME-$VERSION"
echo "+=============================================================================="
echo
echo " It is strongly recommended to not run Privoxy as root. You should"
echo " configure/install/run Privoxy as an unprivileged user, preferably by creating a"
echo " privoxy user and group just for this purpose. See your local documentation"
echo " for the correct command line to do add new users and groups (something like"
echo " adduser, but the command syntax may vary from platform to platform)."
echo " "
echo " /etc/passwd might then look like:"
echo " "
echo "   privoxy:*:7777:7777:privoxy proxy:/no/home:/no/shell"
echo " "
echo " "
echo " And then /etc/group, like:"
echo " "
echo "   privoxy:*:7777:"
echo " "
echo " Please change PRIVOXY_USER PRIVOXY_GROUP in this slackbuild according your"
echo " settings and rebuild"
echo

if grep -i "$PRIVOXY_GROUP:" /etc/group > /dev/null; then
	echo "Group $PRIVOXY_GROUP found.";
else 
	groupadd -g 7777 $PRIVOXY_GROUP
	#groupadd $PRIVOXY_GROUP
	echo "Group $PRIVOXY_GROUP missing: added."
fi

if grep -i "$PRIVOXY_USER:" /etc/passwd > /dev/null; then
	echo "User $PRIVOXY_GROUP found.";
else
	useradd -g $PRIVOXY_GROUP  -u 7777 -d /no/home -s /bin/false -c "Privoxy User" $PRIVOXY_USER
	#useradd -g $PRIVOXY_GROUP -d /no/home -s /bin/false -c "Privoxy User" $PRIVOXY_USER
	echo "User $PRIVOXY_USER missing: added."
fi
echo

if [ ! -e $NAME-$VERSION-stable-src.tar.gz ]; then
echo "Downloading source"
wget -c $SOURCE
fi

cd $TMP

echo "Unpacking $NAME..."
tar -xzf $CWD/$NAME-$VERSION-stable-src.tar.gz

cd $NAME-$VERSION-stable

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
#cd $NAME
#chown -R root:root .

autoheader
autoconf

CFLAGS="$SLKCFLAGS" \
./configure --prefix=$PKG/usr \
--disable-toggle  \
--disable-editor  \
--disable-force \
--sysconfdir=$PKG/etc/privoxy \
--build=$CHOST-slackware-linux
#--docdir=/usr/doc/$NAME-$VERSION \

make
make -s install  USER=$PRIVOXY_USER GROUP=$PRIVOXY_GROUP

sed -i "s#$PKG##g" $PKG/etc/privoxy/config
mv $PKG/etc/privoxy/config $PKG/etc/privoxy/config.new
mv $PKG/etc/privoxy/user.action $PKG/etc/privoxy/user.action.new
mv $PKG/etc/privoxy/user.filter $PKG/etc/privoxy/user.filter.new

mkdir -p $PKG/etc/rc.d
cat slackware/rc.privoxy.orig | \
sed "s/%PROGRAM%/privoxy/" | \
sed "	s/%SBIN_DEST%/\/usr\/sbin/" | \
sed "	s/%CONF_DEST%/\/etc\/privoxy/" | \
sed "	s/%USER%/$PRIVOXY_USER/" | \
sed "	s/%GROUP%/$PRIVOXY_GROUP/" | \
sed "	s/\/var\/run\/\$PRIVOXY_PRG/\/var\/run/	" > $PKG/etc/rc.d/rc.privoxy.new

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a $PKG/usr/share/doc/$NAME/* $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh.in | \
sed "s/%USR%/$PRIVOXY_USER/" | \
sed "s/%GRP%/$PRIVOXY_GROUP/" > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

chmod 644 $PKG/usr/doc/$NAME-$VERSION/*

mkdir -p $PKG/usr/man/man1
cp -a $PKG/usr/share/man/man1/* $PKG/usr/man/man1
rm -rf $PKG/usr/share
gzip -9 $PKG/usr/man/*/*

cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/../$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$NAME-$VERSION
  rm -rf $PKG
  rm -rf $TMP/$NAME-$VERSION.tar.gz*
fi
