#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://wifi-radar.systemimager.org
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it
# Required: PyGtk2

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/wifi-radar
NAME=wifi-radar
VERSION=1.9.9
ARCH=${ARCH:-noarch}
BUILD=1as

SOURCE=http://wifi-radar.systemimager.org/pub/$NAME-$VERSION.tar.bz2

if [ ! -e $NAME-$VERSION.tar.bz2 ]; then
wget -c $SOURCE
fi

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

cd $TMP
tar xvjf $CWD/$NAME-$VERSION.tar.bz2

echo -e "\E[0;32m+-----------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+-----------------------------------+\E[0;0m"

cd $NAME-$VERSION

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 {} \;

chown -R root:root .

# Prepare the package build directory
mkdir -p $PKG/install
mkdir -p $PKG/etc/rc.d
mkdir -p $PKG/etc/wifi-radar
mkdir -p $PKG/usr/sbin
mkdir -p $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/usr/share/applications
mkdir -p $PKG/usr/man/man1
mkdir -p $PKG/usr/man/man5
mkdir -p $PKG/usr/share/pixmaps

make
install -m0755 wifi-radar.localized $PKG/usr/sbin/wifi-radar
install -m0644 wifi-radar.1 $PKG/usr/man/man1
install -m0644 wifi-radar.conf.5 $PKG/usr/man/man5
install -m0644 wifi-radar.conf $PKG/etc/wifi-radar/wifi-radar.conf
install -m0755 $CWD/rc.wifi-radar $PKG/etc/rc.d/rc.wifi-radar
install -m0644 pixmaps/wifi-radar.svg $PKG/usr/share/pixmaps
install -m0644 pixmaps/wifi-radar.png $PKG/usr/share/pixmaps
install -m 644 wifi-radar.desktop $PKG/usr/share/applications

( 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
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

# Rectify the menu entry
sed -i -e 's/TryExec=wifi-radar//g' -e '/^$/ d' $PKG/usr/share/applications/wifi-radar.desktop

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
 CHANGE.LOG COPYING CREDITS DEVELOPER_GUIDELINES INSTALL README README.WPA-Mini-HOWTO.txt  TODO  VERSION \
  $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

	echo "if [ -x /usr/bin/scrollkeeper-update ]; then
  /usr/bin/scrollkeeper-update -p /var/lib/scrollkeeper 1> /dev/null 2> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh	
	echo "if [ -x /usr/bin/update-mime-database ]; then
  /usr/bin/update-mime-database /usr/share/mime &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh
	echo "if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database 1> /dev/null 2> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh
	echo "if [ -x /usr/bin/gtk-update-icon-cache ]; then
  /usr/bin/gtk-update-icon-cache  -f -q /usr/share/icons/hicolor 1> /dev/null 2> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh

gzip -9 $PKG/usr/man/man*/*
chown -R root:root $PKG/usr/sbin

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
fi
