#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild
# Requirements: wxwidgets (unicode)
# http://sourceforge.net/projects/amule-adunanza/
# Packagers: Vincenzi Loris - slacky@slacky.it
#            amule-cvs by Submax (Massimo Cavalleri)

NAME=amule-adunanza
VERSION=2010.1.2.2.6
SVERSION=2010.1-2.2.6
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1sl}
TMP=${TMP:-/tmp/txz}
PKG=$TMP/package-$NAME
CWD=$(pwd)

# CHOST arch should be set to i486 for any 32-bit x86, unless
# you want problems finding your compiler.
case $ARCH in
 "i486") SLKCFLAGS="-O2 -march=i486 -mtune=i686" ; SLKLIBDIR=lib ; CHOST=i486 ;;
 "i686") SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer" ; SLKLIBDIR=lib ; CHOST=i486 ;;
 "x86_64_slamd64") SLKCFLAGS="-O2" ; SLKLIBDIR=lib64 ; CHOST=x86_64 ;; # multilib /lib and /lib64
 "x86_64") SLKCFLAGS="-O2" ; SLKLIBDIR=lib ; CHOST=x86_64 ;; # pure /lib only es. sflack
 "s390") SLKCFLAGS="-O2" ; SLKLIBDIR=lib ; CHOST=s390 ;;
esac

# clean necessary before compile after old run
rm -rf $PKG
mkdir -p $PKG
rm -rf $TMP/aMule-AdunanzA-$SVERSION

# extract source
cd $TMP
mkdir -p $PKG/usr
tar xjvf $CWD/aMule-AdunanzA-$SVERSION.tar.bz2
cd aMule-AdunanzA-$SVERSION
./autogen.sh

# Make sure ownerships and permissions are sane
chown -R root:root .
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 {} \;

# compile
LDFLAGS="-L/usr/$SLKLIBDIR -L/$SLKLIBDIR" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
sed -i 's/Reference Manual/Crypto++ Library/' configure 
./configure --prefix=/usr \
--mandir=/usr/man \
--libdir=/usr/$SLKLIBDIR \
--disable-debug \
--enable-optimize \
--enable-amule-daemon \
--enable-amulecmd \
--enable-webserver \
--enable-amule-gui \
--enable-cas \
--enable-geoip \
--enable-wxcas \
--enable-alc \
--enable-alcc \
--program-prefix= \
--program-suffix= \
--build=$CHOST-slackware-linux
echo "press any key for compile"; read -n 1
make || exit 1
make install DESTDIR=$PKG
( 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
)

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

# documentation
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp ABOUT-NLS Changelog.AdunanzA LEGGIMI.AdunanzA $PKG/usr/doc/$NAME-$VERSION
rm -rf $PKG/usr/share/doc
mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/$NAME.SlackBuild

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

# build package
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

# checksum and desc
#(cd $CWD/..
# md5sum $NAME-$VERSION-$ARCH-$BUILD.tgz > $NAME-$VERSION-$ARCH-$BUILD.tgz.md5
#cat $PKG/install/slack-desc | grep "^$NAME" > $NAME-$VERSION-$ARCH-$BUILD.txt )

# optional clean
if [ "$1" = "--cleanup" ]; then
   if [ "$TMP" != "/" ]; then
      rm -rf $TMP
   fi
fi

