#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://www.gnome.org/projects/NetworkManager
# Thanks to GSB script.
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it
#
#
# Required: linl, policykit, libtelepathy
#
# Exit on most errors
set -e 




CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/NetworkManager
NAME=NetworkManager
VERSION=0.7.0
CHOST=i486
ARCH=${ARCH:-i486}
BUILD=1as

SOURCE=http://ftp.gnome.org/pub/GNOME/sources/NetworkManager/0.7/$NAME-$VERSION.tar.gz

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


if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-DHEADERS_KERNEL -O2 -march=i486   -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-DHEADERS_KERNEL -O3 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-DHEADERS_KERNEL-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then 
  SLKCFLAGS="-DHEADERS_KERNEL -O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-DHEADERS_KERNEL -O2"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-DHEADERS_KERNEL -O2"
fi


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

cd $TMP
tar xvzf $CWD/$NAME-$VERSION.tar.gz

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 666 -exec chmod 644 {} \;
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 {} \;

chown -R root:root .


## NetworkManager requires dhcpcd4 in order to function properly; rather than
## include another dhcpcd package, we'll put our NM dhcpcd4 privately in libexec
## since on a Slackware system, pretty much only NM will use it.  

# Grab our internal dhcpcd4
mkdir dhcpcd4 &&
cd dhcpcd4 &&
if [ ! -f dhcpcd-4.0.3.tar.bz2 ]; then
	wget -c http://roy.marples.name/downloads/dhcpcd/dhcpcd-4.0.3.tar.bz2 ;
fi;
tar xvjf dhcpcd-4.0.3.tar.bz2 &&
cd dhcpcd-4.0.3 &&
make  SYSCONFDIR=/usr/libexec/dhcpcd4 \
BINDIR=/usr/libexec/dhcpcd4 \
LIBEXECDIR=/usr/libexec/dhcpcd4 \
DESTDIR=$PKG install || exit 1
rm -fr $PKG/usr/share 
# Done dhcpcd4 build

## Restore proper CWD
cd $TMP/$NAME-$VERSION



CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
NSS_CFLAGS="-I/usr/include/seamonkey/nss" \
NSS_LIBS="-L/usr/lib/seamonkey" \
./configure  --prefix=/usr --sysconfdir=/etc --localstatedir=/var/lib \
--disable-static \
--mandir=/usr/man \
--with-distro=slackware \
--with-mdns-provider=avahi \
--with-crypto=gnutls \
--with-ip=/sbin/ip \
--with-dbus-sys=/etc/dbus-1/system.d \
--enable-notification-icon \
--with-dhcp-client=/usr/libexec/dhcpcd4/dhcpcd \
--build=$CHOST-slackware-linux



make
make install DESTDIR=$PKG


## Add default nm-setting configuration
mkdir -p $PKG/etc/NetworkManager &&
cp $CWD/nm-system-settings.conf $PKG/etc/NetworkManager || exit 1


## Add wpa_supplicant dbus support
mkdir -p $PKG/usr/share/dbus-1/services &&
cp $CWD/fi.epitest.hostap.WPASupplicant.service $PKG/usr/share/dbus-1/services || exit 1


##  Add custom startup script
rm $PKG/etc/rc.d/rc.networkmanager &&
install -m 644 $CWD/rc.networkmanager $PKG/etc/rc.d/rc.networkmanager.new || exit 1


## Fix for Slackware non-PAM system.
## Users now can add themselves to the "netdev" group to use networkmanager.
sed -i "s/at_console=\"true\"/group=\"netdev\"/g" $PKG/etc/dbus-1/system.d/NetworkManager.conf


mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
  AUTHORS CONTRIBUTING COPYING ChangeLog INSTALL NEWS README TODO \
  $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
  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
)




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/doinst.sh > $PKG/install/doinst.sh
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

		
	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/*/*



cd $PKG
ADD="libtelepathy >= 0.3.3-i486-1as" \
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/networkmanager-$VERSION-$ARCH-$BUILD.tgz


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