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

NAME=gpsman
VERSION=6.4
EXT=tgz
ARCH=${ARCH:-i686}
BUILD=1lu
SOURCE=http://www.ncc.up.pt/gpsman/gpsmanhtml/$NAME-$VERSION.$EXT
CWD=`pwd`

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

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

PKG=$TMP/package-$NAME

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

cd $TMP

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

cd $NAME-$VERSION
mkdir -p $PKG/usr/share/$NAME
cp gpsman.tcl $PKG/usr/share/$NAME
sed -e 's:set SRCDIR gmsrc:set SRCDIR /usr/share/gpsman:' <gpsman.tcl >$PKG/usr/share/gpsman/gpsman.tcl
mkdir -p $PKG/usr/bin
(cd $PKG/usr/bin; ln -s ../share/gpsman/gpsman.tcl gpsman)

cd gmsrc
install -c *.tcl $PKG/usr/share/gpsman
mkdir -p $PKG/usr/share/gpsman/gmicons
cd gmicons
install -c * $PKG/usr/share/gpsman/gmicons

cd $TMP/$NAME-$VERSION

cp -r man $PKG/usr
#cp -r gmsrc util $PKG/usr/share/$NAME

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -r manual/* $PKG/usr/doc/$NAME-$VERSION

cp -a $CWD/slack-desc $CWD/*.SlackBuild $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 -R 755 $PKG/usr/man
gzip -9 $PKG/usr/man/*/*

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