#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://www.hylafax.org
# Packagers Andrea Sciucca  Gohanz ( gohanz@infinito.it)
# http://www.slacky.eu

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-hylafax
NAME=hylafax
VERSION=5.2.9
ARCH=${ARCH:-i686}
BUILD=1as

SOURCE=http://downloads.sourceforge.net/hylafax/$NAME-$VERSION.tar.gz

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

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

cd $TMP
tar xvzf $CWD/$NAME-$VERSION.tar.gz

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

cd $NAME-$VERSION

cp -a $CWD/config.cache .

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 {} \;

./configure
make || exit 1

mkdir -p $PKG/etc/rc.d
mkdir -p $PKG/etc/cron.daily
mkdir -p $PKG/etc/cron.hourly
mkdir -p $PKG/etc/logrotate.d
mkdir -p $PKG/usr/lib/fax
mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/sbin
mkdir -p $PKG/usr/man/man1
mkdir -p $PKG/usr/man/man5
mkdir -p $PKG/usr/man/man8
mkdir -p $PKG/var/spool/hylafax

make install || exit 1

install -m 755 $CWD/hylafax.cron.daily $PKG/etc/cron.daily/hylafax
install -m 755 $CWD/hylafax.cron.hourly $PKG/etc/cron.hourly/hylafax
install -m 644 $CWD/hylafax.logrotate $PKG/etc/logrotate.d/hylafax

mv /usr/bin/faxalter $PKG/usr/bin/
mv /usr/bin/faxcover $PKG/usr/bin
mv /usr/bin/faxmail $PKG/usr/bin
mv /usr/bin/faxrm $PKG/usr/bin
mv /usr/bin/faxstat $PKG/usr/bin
mv /usr/bin/sendfax $PKG/usr/bin
mv /usr/bin/sendpage $PKG/usr/bin

find . -perm -755 -exec cp {} $PKG/usr/sbin \;

( cd $PKG/usr/sbin
 rm config* lib* hylafax.in mkdepend.in mkhash mailfax* sendpage sendfax faxstat faxrm faxcover faxalter
)

mv /usr/sbin/{faxq,dialtest,hfaxd,faxqclean,pagesend,typetest,tiffcheck,faxinfo,faxquit,faxsend,tagtest,\
faxgetty,faxmodem,faxsetup,faxstate,faxwatch,textfmt,cqtest,faxmsg,choptest,faxadduser,tsitest,lockname,\
ondelay,faxdeluser,faxanswer,faxconfig,edit-faxcover,faxabort,faxaddmodem,faxcron,faxlock,probemodem,\
recvstats,xferfaxstats,faxsetup.*} $PKG/usr/sbin
mv /var/spool/hylafax/* $PKG/var/spool/hylafax

cp -a $TMP/$NAME-$VERSION/etc/hylafax $PKG/etc/rc.d/rc.hylafax

mv /usr/local/lib/libfax* $PKG/usr/lib
mv /usr/lib/fax $PKG/usr/lib/

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a CHANGES CONTRIBUTORS COPYRIGHT INSTALL README TODO VERSION /etc/rc.d/init.d/*hylafax* $PKG/usr/doc/$NAME-$VERSION
rm /etc/rc.d/init.d/*hylafax*
rm /etc/rc.d/rc*.d/*hylafax*
mv $PKG/var/spool/hylafax/COPYRIGHT $PKG/usr/doc/$NAME-$VERSION
rm -rf /var/spool/hylafax/
cp -a $CWD/README.Slackware $PKG/usr/doc/$NAME-$VERSION
cd $CWD
cp -a *.SlackBuild slack-desc $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
)

( cd $PKG
  find -iname 'Makefile*' -exec rm -f {} \;
)

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
gzip -9 $PKG/usr/man/*/*

cd $PKG
chown -R root:root .
chown -R uucp.uucp $PKG/var/spool/hylafax
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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