#!/bin/sh -e

# 2014
# Sterling Pickens soul@linuxsociety.org
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PKGNAM=uget
VERSION=${VERSION:-$(echo $PKGNAM-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
BUILD=${BUILD:-1_sop}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) export ARCH=i486 ;;
    arm*) export ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) export ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PKGNAM

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
fi

rm -rf $PKG
mkdir -p $TMP $PKG

cd $TMP || exit 1
rm -rf $PKGNAM-$VERSION
tar xzvf $CWD/$PKGNAM-$VERSION.tar.gz || exit 1
cd $PKGNAM-$VERSION || exit 1
chown -R root:root .
# chmod -R u+w,go+r-w,a-s .

#echo -e '\n'
# Apply patch to fix loading from FF download manager ie: file:// 
#zcat $CWD/epdfview-Omit_File_Colon.diff.gz | patch -p1 || exit
#echo -e '\n'
# Update doxygen conf file
#doxygen -u doc/Doxyfile.in || exit

# Set configure options
CXXFLAGS="$SLKCFLAGS" \
  CFLAGS="$SLKCFLAGS" \
  ./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --docdir=/usr/doc/$PKGNAM-$VERSION \
  --build=$ARCH-slackware-linux \
  --host=$ARCH-slackware-linux 

# compile the source, but exit if anything goes wrong
make -j5 || make || exit

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

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

# Copy documentation to the docs directory and fix permissions
cp -a ABOUT-NLS AUTHORS COPYING INSTALL NEWS README THANKS doc/ $PKG/usr/doc/$PKGNAM-$VERSION
find $PKG/usr/doc/$PKGNAM-$VERSION -type f -exec chmod 644 {} \;

# no manual entries to compress for now
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

# no info pages to compress for now
if [ -d $PKG/usr/info ]; then
  gzip -9 $PKG/usr/info/*.info
  rm -f $PKG/usr/info/dir
fi

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

# Add doinst.sh to package (if it exists)
if [ -e $CWD/doinst.sh.gz ]; then
  zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
fi

# strip
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

chmod 755 $PKG/usr/bin/*

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/$PKGNAM-$VERSION-$ARCH-$BUILD.txz

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

