#!/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=lcov
VERSION=${VERSION:-$(echo $PKGNAM-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
BUILD=${BUILD:-1}

# 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 xvf $CWD/$PKGNAM-$VERSION.tar.?z* || exit 1
cd $PKGNAM-$VERSION || exit 1
chown -R root:root .
# chmod -R u+w,go+r-w,a-s .

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

mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/man
mkdir -p $PKG/etc
cp -a bin/* $PKG/usr/bin
cp -a man/* $PKG/usr/man
cp -a lcovrc $PKG/etc

# Create a directory for documentation
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
# Copy documentation to the docs directory and fix permissions
cp -a README CHANGES CONTRIBUTING COPYING $PKG/usr/doc/$PKGNAM-$VERSION
find $PKG/usr/doc/$PKGNAM-$VERSION -type f -exec chmod 644 {} \;

# manual entries to compress
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/*
chmod 644 $PKG/usr/man/*
chmod 644 $PKG/etc/lcovrc

# 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

