#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://sk1project.org

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-uniconvertor
NAME=uniconvertor
VERSION=1.1.2
ARCH=${ARCH:-i486}
BUILD=1ga

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

cd $TMP
tar xzvf $CWD/$NAME-$VERSION.tar.gz
cd UniConvertor-$VERSION

chown -R root:root .
python setup.py bdist --formats=gztar
( cd $PKG
  tar xzvf $TMP/UniConvertor-$VERSION/dist/*.tar.gz
)

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a src/COPYRIGHTS src/GNU_GPL_v2 src/GNU_LGPL_v2 $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
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

find $PKG/usr/man -type f -exec gzip -9 {} \;

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

cd $PKG
requiredbuilder -v -y -s $CWD $PKG
if [ -x /sbin/makepkg7 ]; then
 makepkg7 -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz
else
 makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz
fi

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