#!/bin/sh
# Heavily based on the Slackware 13.1 SlackBuild
# http://www.quut.com/gsm/
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.eu


CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=libgsm
PKG=$TMP/package/$NAME
SRCNAME=gsm
VERSION=1.0.13
ARCH=${ARCH:-i686} # i686 or x86_64
BUILD=3slacky

SOURCE=http://www.quut.com/gsm/$SRCNAME-$VERSION.tar.gz

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

if [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
fi

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

cd $TMP
tar xvzf $CWD/$SRCNAME-$VERSION.tar.gz
zcat $CWD/$NAME.diff.gz | patch -p0 -E --backup --verbose

echo -e "\E[0;32m+--------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+--------------------------------+\E[0;0m"

cd $SRCNAME-1.0-pl13

find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

make

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

install -m 755 bin/* $PKG/usr/bin
install -m 755 lib/* $PKG/usr/lib${LIBDIRSUFFIX}
install -m 444 inc/gsm.h $PKG/usr/include
install -m 444 inc/gsm.h $PKG/usr/include/gsm
install -m 444 man/*.1 $PKG/usr/man/man1
install -m 444 man/*.3 $PKG/usr/man/man3

mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
cp -a COPYRIGHT ChangeLog INSTALL MACHINES README $PKG/usr/doc/$NAME-$VERSION

(cd $PKG/usr/lib
 rm libgsm.so libgsm.so.1
 ln -sf libgsm.so.1.0.13 libgsm.so
 ln -sf libgsm.so.1.0.13 libgsm.so.1
)


# Replace hardlinks with symlinks
(  cd $PKG/usr/bin
  rm untoast tcat
  ln -s toast untoast
  ln -s toast tcat )


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


mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/$NAME.SlackBuild



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



cd $PKG
chown -R root:root $PKG
if [ -x "$(which requiredbuilder)" ]; then
requiredbuilder -y -v -s $CWD $PKG
fi
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

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