#!/bin/sh
CWD=$(pwd)
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-wireless-tools
rm -rf $PKG
mkdir -p $PKG

VERSION=29
ARCH=${ARCH:-i486}
BUILD=4

cd $TMP
rm -rf wireless_tools.$VERSION
tar xzvf $CWD/wireless_tools.$VERSION.tar.gz
cd wireless_tools.$VERSION
zcat $CWD/wireless_tools.nowhine.diff.gz | patch -p1 --verbose --suffix=.orig --backup || exit 1
zcat $CWD/wireless_tools.static.diff.gz | patch -p1 --verbose --suffix=.orig --backup || exit 1
chown -R root:root .
make || exit 1
mkdir -p $PKG/sbin
cp -a ifrename iwconfig iwevent iwgetid iwlist iwpriv iwspy $PKG/sbin
chmod 755 $PKG/sbin/*
# The library seems to be used by kdenetwork, but since the .soname in the shared library
# doesn't seem to be forward-thinking we'll leave that out still:
mkdir -p $PKG/usr/lib
cp -a libiw.a $PKG/usr/lib

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

mkdir -p $PKG/usr/include
cp -a iwlib.h wireless.h $PKG/usr/include
chmod 644 $PKG/usr/include/*.h

mkdir -p $PKG/usr/man/man5
cat iftab.5 | gzip -9c > $PKG/usr/man/man5/iftab.5.gz
mkdir -p $PKG/usr/man/man7
cat wireless.7 | gzip -9c > $PKG/usr/man/man7/wireless.7.gz
mkdir -p $PKG/usr/man/man8
for file in *.8 ; do
  cat $file | gzip -9c > $PKG/usr/man/man8/$file.gz
done

mkdir -p $PKG/usr/doc/wireless-tools-$VERSION
cp -a \
  COPYING INSTALL *.txt README* \
  $PKG/usr/doc/wireless-tools-$VERSION
chmod 644 $PKG/usr/doc/wireless-tools-$VERSION/*

# Add init scripts from Eric Hameleers:
mkdir -p $PKG/etc/rc.d
cat $CWD/scripts/rc.wireless > $PKG/etc/rc.d/rc.wireless.new
chmod 755 $PKG/etc/rc.d/rc.wireless.new
# This might contain a secret key, so we'll use chmod 600:
cat $CWD/scripts/rc.wireless.conf > $PKG/etc/rc.d/rc.wireless.conf.new
chmod 600 $PKG/etc/rc.d/rc.wireless.conf.new

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

cd $PKG
makepkg -l y -c n ../wireless-tools-$VERSION-$ARCH-$BUILD.tgz