#!/bin/bash
# packager          : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage			: http://humdi.net/vnstat/
# source location	: http://humdi.net/vnstat/vnstat-1.7.tar.gz
# dependency        : 
# last revised      : 13.10.2009

softname='vnstat'
softversion='1.9'
packageversion='1fb'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversion.tar.gz"
src="$buildir/$softname-$softversion"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://humdi.net/vnstat/$softname-$softversion.tar.gz

# prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/$prefix/bin
mkdir -p $dest/etc
mkdir -p $dest/usr/doc/$softname-$softversion
mkdir -p $dest/usr/src/$softname-$softversion

# Slac-desc
cat <<EOF > $dest/install/slack-desc
      |-----handy-ruler------------------------------------------------------|
vnstat: vnstat (A console-based network traffic monitor)
vnstat: 
vnstat: Vnstat uses the network interface statistics provided by the kernel 
vnstat: as information source. This means that vnStat won't actually be 
vnstat: sniffing any traffic and also ensures light use of system resources.
vnstat: 
vnstat: 
vnstat: 
vnstat: 
vnstat: 
vnstat: 
EOF

# check if source is present
if [ -f $softname-$softversion.tar.gz ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

# extract the source code
cd $buildir
tar xzvf $srcpkg
cd $src

# build
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"

sed -i 's|MAN = $(DESTDIR)/usr/share/man|MAN = $(DESTDIR)/usr/man|g' Makefile
sed -i 's|CFLAGS = -O2|CFLAGS = -O2 -march=i486 -mtune=i686|g' src/Makefile
make
make install DESTDIR=$dest
mv $dest/etc/vnstat.conf $dest/etc/vnstat.conf.new
chmod 644 $dest/etc/vnstat.conf.new

# replace the new file if needed
cat << "EOF" > $dest/install/doinst.sh
#! /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...
}
config /etc/vnstat.conf.new
EOF

# add 'default' files
cp -a \
   UNINSTALL COPYING CHANGES README FAQ UPGRADE INSTALL \
   $dest/usr/doc/$softname-$softversion
mkdir  -p $dest/usr/doc/$softname-$softversion/examples
cp $src/examples/* $dest/usr/doc/$softname-$softversion/examples

# strip
( cd $dest
  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
)

# compress man pages
( cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
)

# SalixBuild
cp $buildir/build-$softname.sh $dest/$prefix/src/$softname-$softversion

# set target permissions
chown -R root:root $dest

cd $dest
find . -perm 664 -exec chmod 644 {} \;
find . -perm 640 -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 . -type d -exec chmod 755 {} \;

# real packaging work
cd $dest
makepkg -l y -c n $dest.txz

# computes the md5sum signature for the package
cd $buildir
md5sum $package.txz > $package.md5

# .dep
echo -e "" > $package.dep

# .src
rm -rf $src
rm -rf $dest

# erase
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/ap/vnstat/build-vnstat.sh > $package.src
echo $source >>$package.src

# log
) 2>&1 | tee "$softname-$softversion-build.log"
