#!/bin/sh
#
# Slackware 12.1
#
# pacchettizzatore Giovanni Venturi
# giovanni@ksniffer.org
# http://www.chillispot.org/

CWD=`pwd`
if ["$TMP" = ""]; then
TMP=/tmp
fi
PKG=$TMP/package-chillispot
NAME=chillispot
VERSION=1.1.0
ARCH=i486
BUILD=1ks

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

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

echo "| Start SlackBuild $NAME-$VERSION |"

cd $TMP
tar xzvf $CWD/$NAME-$VERSION.tar.gz
cd $NAME-$VERSION
chown -R root:root .
find . -perm 775 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;

CFLAGS="-O2 -march=i486 -mtune=i686" ./configure --prefix=/usr --disable-debug

make

make install DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS NEWS COPYING ChangeLog README CREDITS INSTALL $PKG/usr/doc/$NAME-$VERSION
cp -a doc/dictionary.chillispot doc/firewall.* doc/freeradius.users $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/var/www/cgi-bin
cp -a doc/hotspotlogin.cgi $PKG/var/www/cgi-bin
`which chmod` 0755 $PKG/var/www/cgi-bin/hotspotlogin.cgi

mkdir -p $PKG/etc/rc.d
cp -a doc/chilli.conf $PKG/etc

echo "#!/bin/sh" > $PKG/etc/rc.d/rc.chillispot
echo "#" >> $PKG/etc/rc.d/rc.chillispot
echo "# chilli      This shell script takes care of starting and stopping" >> $PKG/etc/rc.d/rc.chillispot
echo "#             chilli." >> $PKG/etc/rc.d/rc.chillispot
echo "#" >> $PKG/etc/rc.d/rc.chillispot
echo "# description: ChilliSpot" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "# Wireless authenticator daemon." >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "[ -f /usr/sbin/chilli ] || exit 0" >> $PKG/etc/rc.d/rc.chillispot
echo "[ -f /etc/chilli.conf ] || exit 0" >> $PKG/etc/rc.d/rc.chillispot
echo "[ -f /var/www/cgi-bin/hotspotlogin.cgi ] || exit 0" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "RETVAL=0" >> $PKG/etc/rc.d/rc.chillispot
echo "prog=\"chilli\"" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "start() {" >> $PKG/etc/rc.d/rc.chillispot
echo "        # Start daemons." >> $PKG/etc/rc.d/rc.chillispot
echo "        echo -n \$\"Starting \$prog: \"" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "        # Load tun module" >> $PKG/etc/rc.d/rc.chillispot
echo "        /sbin/modprobe tun >/dev/null 2>&1" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "        # Enable routing of packets: WARNING!!!" >> $PKG/etc/rc.d/rc.chillispot
echo "        # Users should enable this explicitly" >> $PKG/etc/rc.d/rc.chillispot
echo "        # echo 1 > /proc/sys/net/ipv4/ip_forward" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "        /usr/sbin/chilli" >> $PKG/etc/rc.d/rc.chillispot
echo "        RETVAL=\$?" >> $PKG/etc/rc.d/rc.chillispot
echo "        [ \$RETVAL -eq 0 ] && touch /var/lock/subsys/chilli" >> $PKG/etc/rc.d/rc.chillispot
echo "        if [ \$RETVAL -eq 0 ] ; then echo \"OK\" ; fi" >> $PKG/etc/rc.d/rc.chillispot
echo "        return \$RETVAL" >> $PKG/etc/rc.d/rc.chillispot
echo "}" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "startdebug() {" >> $PKG/etc/rc.d/rc.chillispot
echo "        # Start daemons." >> $PKG/etc/rc.d/rc.chillispot
echo "        echo -n \$\"Starting \$prog: \"" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "        # Load tun module" >> $PKG/etc/rc.d/rc.chillispot
echo "        /sbin/modprobe tun >/dev/null 2>&1" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "        # Enable routing of packets: WARNING!!!" >> $PKG/etc/rc.d/rc.chillispot
echo "        # Users should enable this explicitly" >> $PKG/etc/rc.d/rc.chillispot
echo "        # echo 1 > /proc/sys/net/ipv4/ip_forward" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "        /usr/sbin/chilli --fg --debug" >> $PKG/etc/rc.d/rc.chillispot
echo "        RETVAL=\$?" >> $PKG/etc/rc.d/rc.chillispot
echo "        echo" >> $PKG/etc/rc.d/rc.chillispot
echo "        [ \$RETVAL -eq 0 ] && touch /var/lock/subsys/chilli" >> $PKG/etc/rc.d/rc.chillispot
echo "        return \$RETVAL" >> $PKG/etc/rc.d/rc.chillispot
echo "}" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "stop() {" >> $PKG/etc/rc.d/rc.chillispot
echo "        # Stop daemons." >> $PKG/etc/rc.d/rc.chillispot
echo "        echo -n \$\"Shutting down \$prog: \"" >> $PKG/etc/rc.d/rc.chillispot
echo "        killall chilli" >> $PKG/etc/rc.d/rc.chillispot
echo "        RETVAL=\$?" >> $PKG/etc/rc.d/rc.chillispot
echo "        echo" >> $PKG/etc/rc.d/rc.chillispot
echo "        [ \$RETVAL = 0 ] && rm -f /var/lock/subsys/chilli /var/run/chilli.pid" >> $PKG/etc/rc.d/rc.chillispot
echo "        if [ \$RETVAL -eq 0 ] ; then echo \"OK\"; fi" >> $PKG/etc/rc.d/rc.chillispot
echo "        return \$RETVAL" >> $PKG/etc/rc.d/rc.chillispot
echo "}" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "# See how we were called." >> $PKG/etc/rc.d/rc.chillispot
echo "case "\$1" in" >> $PKG/etc/rc.d/rc.chillispot
echo "  start)" >> $PKG/etc/rc.d/rc.chillispot
echo "        start" >> $PKG/etc/rc.d/rc.chillispot
echo "        ;;" >> $PKG/etc/rc.d/rc.chillispot
echo "  startdebug)" >> $PKG/etc/rc.d/rc.chillispot
echo "        startdebug" >> $PKG/etc/rc.d/rc.chillispot
echo "        ;;" >> $PKG/etc/rc.d/rc.chillispot
echo "  stop)" >> $PKG/etc/rc.d/rc.chillispot
echo "        stop" >> $PKG/etc/rc.d/rc.chillispot
echo "        ;;" >> $PKG/etc/rc.d/rc.chillispot
echo "  restart|reload)" >> $PKG/etc/rc.d/rc.chillispot
echo "        stop" >> $PKG/etc/rc.d/rc.chillispot
echo "        start" >> $PKG/etc/rc.d/rc.chillispot
echo "        RETVAL=\$?" >> $PKG/etc/rc.d/rc.chillispot
echo "        ;;" >> $PKG/etc/rc.d/rc.chillispot
echo "  condrestart)" >> $PKG/etc/rc.d/rc.chillispot
echo "        if [ -f /var/lock/subsys/chilli ] ; then" >> $PKG/etc/rc.d/rc.chillispot
echo "            stop" >> $PKG/etc/rc.d/rc.chillispot
echo "            start" >> $PKG/etc/rc.d/rc.chillispot
echo "            RETVAL=\$?" >> $PKG/etc/rc.d/rc.chillispot
echo "        fi" >> $PKG/etc/rc.d/rc.chillispot
echo "        ;;" >> $PKG/etc/rc.d/rc.chillispot
echo "  status)" >> $PKG/etc/rc.d/rc.chillispot
echo "        if [ -f /var/run/chilli.pid ]" >> $PKG/etc/rc.d/rc.chillispot
echo "           then echo Chilli is running" >> $PKG/etc/rc.d/rc.chillispot
echo "           else echo Chilli is NOT running" >> $PKG/etc/rc.d/rc.chillispot
echo "        fi" >> $PKG/etc/rc.d/rc.chillispot
echo "        ;;" >> $PKG/etc/rc.d/rc.chillispot
echo "  *)" >> $PKG/etc/rc.d/rc.chillispot
echo "        echo \$\"Usage: \$0 {start|startdebug|stop|restart|condrestart|status}\"" >> $PKG/etc/rc.d/rc.chillispot
echo "        exit 1" >> $PKG/etc/rc.d/rc.chillispot
echo "esac" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot
echo "exit \$RETVAL" >> $PKG/etc/rc.d/rc.chillispot
echo "" >> $PKG/etc/rc.d/rc.chillispot

( 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
)

gzip -9 $PKG/usr/man/*/*
mv $PKG/etc/chilli.conf $PKG/etc/chilli.conf.new

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $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
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $TMP/$NAME-$VERSION-$ARCH-$BUILD.tgz

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

