#!/bin/sh

#
# Mr Hyde  revision date 2009/09/20
#
# Based on what you can read at
#
# http://slackwiki.org/Writing_A_SlackBuild_Script
#
# Thanks also to Carlos Coracho for his kaffeine.SlackBuild
# (and his support to SLAMD64 and his job on acer_acpi)
#
# 
# SOME EXAMPLE ABOUT HOW TO USE THIS SILLY SCRYPT:
# To build on a i686 32bit Slackware system type the following command:
#      ARCH=i686 DISTRO=slackware ./aMule-AdunanzA.Slackbuild
#
# To build on a x86_64 64bit Slamd64  system type the following command:
#      ARCH=x86_64 DISTRO=slamd ./aMule-AdunanzA.Slackbuild
#
#

# Set initial variables:	
DEFAULT_PKG_BUILD=1
PACKAGER_TAG=mrh


CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

# The application's name
APP=aMuleAdunanzA

# The version which appears in the application's filename
VERSION=2011.1
#
# Mr Hyde: Se vuoi installare anche aMule ufficiale
#          metti qualcosa in PRGPREFIX (e/o PRGSUFFIX)
#          per differenziare i file da quelli di aMule ufficiale
# PRGPREFIX=adu_
PRGPREFIX=
PRGSUFFIX=adunanza
# PRGSUFFIX=


# If the version conflicts with the Slackware package standard
# The dash character ("-") is not allowed in the VERSION string
# You can set the PKG_VERSION to something else than VERSION
PKG_VERSION=$VERSION # the version which appears in the package name. 

# The target distro
DISTRO=${DISTRO:-slamd64}

ARCH=${ARCH:-x86_64} # the architecture on which you want to build your package	

TAG=
if [ $DISTRO = slackware ]; then
	PKGARCH=$ARCH
	TAG=${PACKAGER_TAG}
	LIBSUFFIX=
	SLKLDFLAGS=
else
	PKGARCH=${ARCH}_${DISTRO}
	TAG=_${PACKAGER_TAG}
	LIBSUFFIX=64
	SLKLDFLAGS="-L/usr/lib64"
fi



# The installation directory of the package (where its actual directory
# structure will be created) 
PKG=$TMP/package-$APP

# to change for 64bit distro
TAG=
LIBSUFFIX=
if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mtune=i686"
 elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBSUFFIX=64
fi



# Delete the leftover directories if they exist (due to a previous build)
# and (re)create the packaging directory
rm -rf $PKG 
mkdir -p $TMP $PKG
rm -rf $TMP/$APP-$VERSION

# Change to the TMP directory
cd $TMP || exit 1
 
# Extract the application source in TMP
# Note: if your application comes as a tar.bz2, you need tar -jxvf
tar -jxvf $CWD/$APP-$VERSION.tar.bz2 || exit 1

# Change to the application source directory
cd $APP-$VERSION || exit 1

BUILD=${BUILD:-$DEFAULT_PKG_BUILD}


# Change ownership and permissions if necessary
# This may not be needed in some source tarballs, but it never hurts
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# Set configure options
# If your app is written in C++, you'll also need to add a line for CXXFLAGS
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
LDFLAGS="$SLKLDFLAGS" \
./autogen.sh ; \
./configure \
--prefix=/usr \
--libdir=/usr/lib$LIBSUFFIX \
--sysconfdir=/etc \
--localstatedir=/var \
--mandir=/usr/man \
--docdir=/usr/doc/$APP-$VERSION \
--program-prefix=$PRGPREFIX \
--program-suffix=$PRGSUFFIX \
--enable-amule-daemon \
--enable-amulecmd \
--enable-webserver \
--enable-amule-gui \
--enable-alc \
--enable-alcc \
--enable-geoip \
--build=$ARCH-$DISTRO-linux \
--host=$ARCH-$DISTRO-linux 

# compile the source, but exit if anything goes wrong
make || exit
 
# Install everything into the package directory, but exit if anything goes wrong
make install DESTDIR=$PKG || exit



# INSTALL DOCUMENTATION
# # remove usr/share/doc/amule
# rm -rf usr/share/doc/amule-adunanza

# Create a directory for package documentation
mkdir -p $PKG/usr/doc/$APP-$VERSION

# Copy documentation to the docs directory and fix permissions
cp -a \
	ABOUT-NLS LEGGIMI.AdunanzA Changelog.AdunanzA README* \
	$PKG/usr/doc/$APP-$VERSION

mkdir $PKG/usr/doc/$APP-$VERSION/docs
cp -a docs/A* docs/C* docs/ED2K* docs/INSTALL docs/README docs/Rele* docs/TODO docs/*.txt docs/socks4.protocol \
	$PKG/usr/doc/$APP-$VERSION/docs


find $PKG/usr/doc/$APP-$VERSION -type f -exec chmod 644 {} \;

# Create the ./install directory and copy the slack-desc into it
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Strip some libraries and binaries
( 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
)

# Compress man pages if they exist
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $(find . -type l) ; do ln -s $(readlink $i).gz $i.gz ; rm $i ; done
  ) 
fi

# Compress info pages if they exist (and remove the dir file)
if [ -d $PKG/usr/info ]; then
  gzip -9 $PKG/usr/info/*.info
  rm -f $PKG/usr/info/dir
fi

# Build the package
cd $PKG
/sbin/makepkg -l y -c n $TMP/$APP-$PKG_VERSION-$PKGARCH-$BUILD$TAG.txz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$PKGNAM-$VERSION
  rm -rf $PKG
fi
