#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# Requirements: geos, postgresql, proj, unixodbc, jasper, ogdi, shapelib, libgeotiff
# http://pyserial.sourceforge.net/

CWD=`pwd`
NAME=pyserial
VERSION=2.2
EXT=zip
ARCH=${ARCH:-noarch}
BUILD=1lu
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-$NAME
SOURCE=http://mesh.dl.sourceforge.net/sourceforge/pyserial/$NAME-$VERSION.$EXT

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

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
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="gzip -e"
elif [ "$EXT" = "zip" ]; then
   TARCMD="unzip"
fi

cd $TMP
$TARCMD $CWD/$NAME-$VERSION.$EXT
cd $NAME-$VERSION
chown -R root:root .
mkdir -p $PKG/usr/doc/$NAME-$VERSION
python setup.py install --root=$PKG
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
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
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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