#!/bin/sh
# aircrack-ng slackbuild - Dani - dani@slacky.it
# Heavily based on the Slackware 12.1 SlackBuild

NICE=10
NAME=aircrack-ng
VERSION=1.0
RC=rc1
ARCH=${ARCH:-i486}
CHOST=${CHOST:-i486}
BUILD=1dd
SOURCE=http://download.aircrack-ng.org/$NAME-$VERSION-$RC.tar.gz

CWD=`pwd`
PKG=/tmp/$NAME-build
TGZ=$PKG/TGZ

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"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -e $CWD/$NAME-$VERSION-$RC.tar.gz ]; then
 wget -c $SOURCE || exit 1
fi

rm -rf $PKG
mkdir -p $TGZ

cd $PKG
tar xzvf $CWD/$NAME-$VERSION-$RC.tar.gz || exit 1
cd $NAME-$VERSION-$RC || exit 1

chown -R root.root .
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 {} \;

mkdir -p $TGZ/usr/doc/$NAME-${VERSION}${RC}
cp -fav AUTHORS ChangeLog INSTALLING  LICENSE* \
       README VERSION                          \
       $TGZ/usr/doc/$NAME-${VERSION}${RC}

CFLAGS="$SLKCFLAGS"          \
CXXFLAGS="$SLKCFLAGS"        \
nice -n$NICE make sqlite=true -j2 || exit 1
make sqlite=true install DESTDIR=$TGZ || exit 2

( cd $TGZ 
  mv usr/local/* usr/
  rmdir usr/local
)

gzip -9 $TGZ/usr/man/man?/*.?

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

cd $TGZ
mkdir -p $TGZ/install
cat $CWD/slack-desc > $TGZ/install/slack-desc < $TGZ/usr/doc/$NAME-${VERSION}${RC}
cat $CWD/$NAME.SlackBuild > $TGZ/usr/doc/$NAME-${VERSION}${RC}/$NAME.SlackBuild
requiredbuilder -v -y -s $CWD $TGZ
makepkg -l y -c n $CWD/$NAME-${VERSION}${RC}-$ARCH-$BUILD.tgz

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

exit 0;
