#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://packages.debian.org/testing/sound/libgsm-tools
# Packagers Andrea Sciucca Gohanz ( gohanz@infinito.it)
# http://www.slacky.it

CWD=`pwd`
if ["$TMP" = ""]; then
 TMP=/tmp
fi
PKG=$TMP/package-libgsm
NAME=libgsm
VERSION=1.0.10
PATCH=13
ARCH=${ARCH:-i686}
BUILD=1as

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

cd $TMP
tar xvzf $CWD/$NAME'_'$VERSION.orig.tar.gz
gzip -d -c $CWD/$NAME'_'$VERSION-$PATCH.diff.gz | patch -p0 

echo "+------------------------------------+"
echo "| Starting SlackBuild  $NAME-$VERSION |"
echo "+------------------------------------+"

cd $NAME-$VERSION.orig

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

make

mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/lib
mkdir -p $PKG/usr/include/gsm
mkdir -p $PKG/usr/man/man3
mkdir -p $PKG/usr/man/man1

cp -a bin/*   $PKG/usr/bin
cp -a lib/*   $PKG/usr/lib
cp -a inc/gsm.h   $PKG/usr/include
cp -a inc/gsm.h   $PKG/usr/include/gsm
cp -a man/*.1 $PKG/usr/man/man1
cp -a man/*.3 $PKG/usr/man/man3

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
 COPYRIGHT  ChangeLog   ChangeLog.orig  INSTALL MACHINES MANIFEST README \
 $PKG/usr/doc/$NAME-$VERSION
cd $CWD
cp -a *.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION

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

( 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
)

gzip -9 $PKG/usr/man/*/*

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

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