#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild

NAME=gpsbabel
VERSION=1.3.4
EXT=tar.gz
ARCH=${ARCH:-i486}
BUILD=1lu
SOURCE=http://puzzle.dl.sourceforge.net/sourceforge/gpsbabel/$NAME-$VERSION.$EXT
CWD=`pwd`

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

if ["$TMP" = ""]; then
 TMP=/tmp
fi

PKG=$TMP/package-$NAME

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ "$EXT" = "tar.gz" ]; then
   TARCMD="tar xvzf"
elif [ "$EXT" = "tgz" ]; then
   TARCMD="tar xvzf"
elif [ "$EXT" = "tar.bz2" ]; then
   TARCMD="tar xvjf"
elif ["$EXT" = "gz" ]; then
   TARCMD="gunzip"
elif ["$EXT" = "zip" ]; then
   TARCMD="gzip"
fi

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

mkdir -p $PKG/usr

cd $TMP

$TARCMD $CWD/$NAME-$VERSION.$EXT

cd $NAME-$VERSION

CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr \
--program-prefix= \
--program-suffix= \
--build=i486-slackware-linux
make
mkdir -p $PKG/usr/bin
#make prefix=$PKG/usr install
install $NAME $PKG/usr/bin/$NAME
cp guibabel $PKG/usr/bin/
mkdir -p $PKG/usr/doc/$NAME-$VERSION

cp -a AUTHORS COPYING README* $PKG/usr/doc/$NAME-$VERSION

cp -a $CWD/slack-desc $CWD/*.SlackBuild $PKG/usr/doc/$NAME-$VERSION

chown -R root:root $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
)

chmod 755 $PKG/usr/bin/*

mkdir -p $PKG/install

cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
chown -R root:root .
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP/$NAME-$VERSION
 rm -rf $PKG
fi