#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# WWW: http://www.ipflow.utc.fr/index.php/Cisco_7200_Simulator
#      http://www.ipflow.utc.fr/blog/
#      http://hacki.at/7200emu/index.php
# Requirements: libelf
# http://www.slacky.it

NAME=dynamips
VERSION=0.2.8rc2
ARCH=${ARCH:-i686}
BUILD=2bj

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-$NAME
rm -rf $PKG
mkdir -p $PKG/usr/share/$NAME/{labs,images}

cd $TMP
rm -rf $NAME-$VERSION
tar xzf $CWD/$NAME-0.2.8-RC2.tar.gz || exit 1
cd $NAME-0.2.8-RC2
echo "Building $NAME-$VERSION ..."
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

#fix path of libpcap on makefile
sed  -e "s|PCAP_LIB=/usr/local/lib/libpcap.a|PCAP_LIB=/usr/lib/libpcap.a|g" -i Makefile

# Make
make || exit 1

# Install
make DESTDIR=$PKG/usr install || exit 1

strip $PKG/usr/bin/*
rmdir $PKG/usr/etc

#mkdir -p $PKG/etc/rc.d
#zcat $CWD/rc.dynamips.gz > $PKG/etc/rc.d/rc.dynamips

# Gzip the man pages:
find $PKG/usr/man -type f -exec gzip -9 {} \;

# Copy the docs
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a ChangeLog COPYING README* TODO $CWD/rc.dynamips.gz \
 $PKG/usr/doc/$NAME-$VERSION

# Build the package
cd $PKG
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/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
chown -R root:root .
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff
if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi
