#!/bin/bash

CWD=`pwd`
NAME="kismet"
VERSION="2010-07-R1"
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1as}
TMP=${TMP:-/tmp/txz/$NAME}
PKG=$TMP/package
SOURCE="http://www.kismetwireless.net/code/$NAME-$VERSION.tar.gz"

echo -n "Enabling suid-install? [N,y]  "
read SUID
if [ "$SUID" == "Y" ] || [ "$SUID" == "y" ]; then
	SUID_G=""
	while [ "$SUID_G" == "" ]; do
		echo -n "Insert suid group: "
		read SUID_G
	done
fi


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

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i486"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"; CHOST="x86_64"
fi

rm -rf $PKG
mkdir -p $TMP $PKG

cd $TMP
tar xvf $CWD/$NAME-$VERSION.tar.gz || exit 1

echo "t" | svn co https://dedected.org/svn/trunk/kismet-dect/client_module plugin-dect_client
echo "t" | svn co https://dedected.org/svn/trunk/kismet-dect/server_module plugin-dect_server


echo -e "\E[0;32m+-------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild KISMET |\E[0;0m"
echo -e "\E[0;32m+-------------------------+\E[0;0m"

cd $NAME-$VERSION

find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
chown -R root:root .

if [ ! -r configure ]; then
	sh autogen.sh
fi

./configure \
	--prefix=/usr \
	--sysconfdir=/etc/kismet \
	--localstatedir=/var \
	--mandir=/usr/man \
	--program-prefix= \
	--program-suffix= \
	--with-suidgroup=$SUID_G \
	--build=$ARCH-slackware-linux
echo
echo -n "Press \"ENTER\" to start compiling..."
echo
read
make dep
make all-with-plugins || exit 1
if [ "$SUID" == "Y" ] || [ "$SUID" == "y" ]; then
	make suidinstall DESTDIR=$PKG
	echo
	echo "Remeber to add your user to group \"$SUID_G\"."
	echo
	sleep 3
else
	make install DESTDIR=$PKG
fi
make plugins-install DESTDIR=$PKG

( cd ../plugin-dect_client
  KIS_SRC_DIR=$TMP/$NAME-$VERSION make
  KIS_DEST_DIR=$PKG/usr make install
)
( cd ../plugin-dect_server
  KIS_SRC_DIR=$TMP/$NAME-$VERSION make
  KIS_DEST_DIR=$PKG/usr make install
)

DOCFILES="ABOUT ABOUT-NLS ANNOUNCE AUTHORS CONFIGURATION CHANGES COPYING COPYRIGHT CREDITS \
	ChangeLog CHANGELOG CONTRIBUTORS *FAQ* FEATURES FILES HACKING History HISTORY \
	INSTALL* LICENSE LSM MANIFEST NEWS *README* *Readme* SITES *RELEASE* RELNOTES \
	THANKS TIPS TODO VERSION CONFIGURATION* GPL License Doc doc Docs* docs* Roadmap \
	ROADMAP *BUGS* index.ht* *INDEX*"
mkdir -p $PKG/usr/doc/$NAME-${VERSION//-/.}
for i in ${DOCFILES} ; do
	if [ -e $i ]; then
		if ! [ -L $i ]; then
			cp -a $i $PKG/usr/doc/$NAME-${VERSION//-/.}
		else
			cp -LRp $i $PKG/usr/doc/$NAME-${VERSION//-/.}
		fi
	fi
done
chmod 644 $PKG/usr/doc/$NAME-${VERSION//-/.}/*
chown -R root:root $PKG/usr/doc/$NAME-${VERSION//-/.}/*

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

( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $(find . -type l) ; do ln -s $(readlink $i).gz $i.gz ; rm $i ; done
)


mkdir -p $PKG/install
if [ ! -e $CWD/slack-desc ]; then
	cat > $PKG/install/slack-desc << END
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|' on
# the right side marks the last column you can put a character in. You must make
# exactly 11 lines for the formatting to be correct.  It's also customary to
# leave one space after the ':'.

     |-----------------handy-ruler-------------------------------------------|
$NAME: $NAME $VERSION
$NAME: 
$NAME: Kismet is an 802.11 layer2 wireless network detector, sniffer, and
$NAME: intrusion detection system. Kismet will work with any wireless card 
$NAME: which supports raw monitoring (rfmon) mode, and can sniff 802.11b,
$NAME: 802.11a, and 802.11g traffic. 
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END
else
	cat $CWD/slack-desc > $PKG/install/slack-desc
fi

if [ ! -e $CWD/doinst.sh ]; then
	cat > $PKG/install/doinst.sh << __END__
#!/bin/sh
config() {
  NEW="\$1"
  OLD="\`dirname \$NEW\`/\`basename \$NEW .new\`"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\`cat \$OLD | md5sum\`" = "\`cat \$NEW | md5sum\`" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
if [ -e etc/kismet/kismet.conf.new ]; then
  config etc/kismet/kismet.conf.new
fi
if [ -e etc/kismet/kismet_ui.conf.new ]; then
  config etc/kismet/kismet_ui.conf.new
fi
if [ -e etc/kismet/kismet_drone.conf.new ]; then
  config etc/kismet/kismet_drone.conf.new
fi
if [ -e etc/kismet/client_manuf.new ]; then
  config etc/kismet/client_manuf.new
fi
if [ -e etc/kismet/ap_manuf.new ]; then
  config etc/kismet/ap_manuf.new
fi
__END__
else
	cat $CWD/doinst.sh > $PKG/install/doinst.sh
fi

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


if [ -e $PKG/etc/kismet/kismet.conf ]; then
	mv $PKG/etc/kismet/kismet.conf $PKG/etc/kismet/kismet.conf.new
fi
if [ -e $PKG/etc/kismet/kismet_ui.conf ]; then
	mv $PKG/etc/kismet/kismet_ui.conf $PKG/etc/kismet/kismet_ui.conf.new
fi
if [ -e $PKG/etc/kismet/kismet_drone.conf ]; then
	mv $PKG/etc/kismet/kismet_drone.conf $PKG/etc/kismet/kismet_drone.conf.new
fi
if [ -e $PKG/etc/kismet/ap_manuf ]; then
	mv $PKG/etc/kismet/ap_manuf $PKG/etc/kismet/ap_manuf.new
fi
if [ -e $PKG/etc/kismet/client_manuf ]; then
	mv $PKG/etc/kismet/client_manuf $PKG/etc/kismet/client_manuf.new
fi

cd $PKG
if which requiredbuilder &> /dev/null ; then
	requiredbuilder -v -y -s $CWD $PKG
fi
makepkg -l y -c n $CWD/$NAME-${VERSION//-/.}-$ARCH-$BUILD.txz


if [ "$1" = "--cleanup" ]; then
	rm -rf $TMP
fi
