#!/bin/sh
#####################################################################
# Script per la creazione del pacchetto gammu
# OS Version : Slackware 12.0
# http://www.gammu.net/projects/gammu.php
# Changelog  :
# Alessandro Celli: Creazione script
# Dipendenze: postgresql
#####################################################################
set -x
#---- MODIFICABILI --------------------------------------------------
SOFTWARE="gammu"
VERSION="1.19.0"
ARCH="i486"
BUILD="1ac"
TMP=""
CWD=""

#---- COSTANTI ------------------------------------------------------
ERR_NO_VERSION=1

#---- PROCEDURE -----------------------------------------------------
print_error_and_exit() {
	case $1 in
		$ERR_NO_VERSION) 
			echo "Manca la versione del package"
			;;
		*)
			echo "Errore non specificato"
			;;
	esac
	exit $1
}


#--------------------------------------------------------------------
# Controllo dei parametri specificati settando i valori di default, dove possibile, per quelli non assegnati.

# Verifica e completamento variabili 
[ "$VERSION" == "" ] && print_error_and_exit $ERR_NO_VERSION
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

CWD=${CWD:-`pwd`}
TMP=${TMP:-/tmp}
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
PKG=$TMP/package-tar
rm -rf $PKG
mkdir -p $PKG

case $ARCH in
	"i386")
		SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
	;;
	"i486")
		SLKCFLAGS="-O2 -march=i486 -mtune=i686"
	;;
	"s390")
  		SLKCFLAGS="-O2"
	;;
	"x86_64")
  		SLKCFLAGS="-O2"
	;;
	*)
  		SLKCFLAGS="-O2"
	;;
esac

read
# Configurazione e compliazione
cd $TMP
rm -rf ${SOFTWARE}-${VERSION}
tar xjvf $CWD/${SOFTWARE}-${VERSION}.tar.bz2
cd ${SOFTWARE}-${VERSION}
chown -R root:root .
read

mkdir -p build
cd build
cmake -DCMAKE_INSTALL_PREFIX="/usr" ..
make || exit 1
make install DESTDIR=$PKG || exit 1
cd ..
mkdir -p $PKG/usr/doc/$SOFTWARE-$VERSION
cp -a INSTALL VERSION $PKG/usr/doc/$SOFTWARE-$VERSION
mv $PKG/usr/share/doc/gammu $PKG/usr/doc/$SOFTWARE-$VERSION
rm -rf $PKG/usr/share/doc
mkdir -p $PKG/usr/man/man1
cp $PKG/usr/share/man/man1/gammu.1 $PKG/usr/man/man1
rm -rf $PKG/usr/share/man

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

read
gzip -9 $PKG/usr/man/*/*
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $CWD
cp -a *.SlackBuild $PKG/usr/doc/$SOFTWARE-$VERSION
cp -a slack-desc $PKG/usr/doc/$SOFTWARE-$VERSION
chown -R root:root $PKG/usr/doc/$SOFTWARE-$VERSION

read
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg --linkadd y --chown n $CWD/$SOFTWARE-$VERSION-$ARCH-$BUILD.tgz

read
if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP/$SOFTWARE-$VERSION
 rm -rf $PKG
fi
set +x