#!/bin/sh

# Slackware build script for apache-ant
# Written by Konstantinos Mantzaris (kmanjaris@gmail.com)
# 
# Based on the Slackware 13.0 build script for apache-ant created by: 
#  - Dugan Chen (dugan[underscore]c[at]fastmail[dot]fm)
#  - Vincent Batts (vbatts@hashbangbash.com)
#
# Copyright 2011-2016 Konstantinos Mantzaris <kmanjaris@gmail.com>
# 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=apache-ant
PKGVER=1.9.7
VERSION=1.9.7
BUILD=${BUILD:-1}
TAG=${TAG:-xsb}
PROJECT=$(echo $PKGNAM | cut -d- -f2)
ANT_HOME=/usr/share/$PROJECT


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

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "arm" ]; then
  SLKCFLAGS="-O2 -pipe -march=armv6j -mfpu=vfp -mfloat-abi=softfp -Wno-psabi -lGLESv2"
  SLKINCFLAGS="/opt/vc/include:/opt/vc/include/interface/vcos:/usr/include/interface/vcos/pthreads"
  LIBDIRSUFFIX=""
fi

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

rm -rf $PKG
mkdir -p $TMP $PKG $PKG$ANT_HOME
mkdir -p $PKG/etc/profile.d
mkdir -p $PKG/usr/{bin,share/java}
mkdir -p

cd $TMP
rm -rf ${PKGNAM}-$PKGVER
tar xvf $CWD/${PKGNAM}-$VERSION-src.tar.?z* || exit 1
cd ${PKGNAM}-$PKGVER || exit 1

# Make sure ownerships and permissions are sane:
chown -R root:root .
find . \
  \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
  -exec chmod 755 {} \; -o \
  \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
  -exec chmod 644 {} \;

# Prepare for build
./build.sh

cp -a dist/bin $PKG$ANT_HOME/
mkdir -p $PKG$ANT_HOME/etc
cp -a dist/lib${LIBDIRSUFFIX} $PKG$ANT_HOME/
cp -a build/lib${LIBDIRSUFFIX}/ant-bootstrap.jar $PKG$ANT_HOME/etc
cp -a src/etc/*.xsl $PKG$ANT_HOME/etc
cp -a src/etc/checkstyle $PKG$ANT_HOME/etc/

cat << EOF > $PKG/etc/profile.d/$PKGNAM.csh
#!/bin/csh
setenv ANT_HOME ${ANT_HOME}
EOF
cat << EOF > $PKG/etc/profile.d/$PKGNAM.sh
#!/bin/sh
export ANT_HOME=${ANT_HOME}
EOF
chmod 0755 $PKG/etc/profile.d/*
export ANT_HOME=${ANT_HOME}

cd $PKG/usr/share/java
  for JARFILE in ../../../$ANT_HOME/lib${LIBDIRSUFFIX}/*.jar ; do
    ln -s $JARFILE
  done
cd -

cd $PKG/usr/bin
  ln -s $ANT_HOME/bin/ant
  ln -s $ANT_HOME/bin/antRun
  ln -s $ANT_HOME/bin/antRun.pl
  ln -s $ANT_HOME/bin/runant.pl
  ln -s $ANT_HOME/bin/runant.py
cd -

# Add a documentation directory:
mkdir -p $PKG/usr/doc/${PKGNAM}-$VERSION
cp -a \
  INSTALL KEYS LICENS* NOTICE README WHATSNEW \
  $PKG/usr/doc/${PKGNAM}-$VERSION

mkdir -p $PKG/install
if [ "$ARCH" = "arm" ]; then LIBDIRSUFFIX="RPi"; fi
sed "s/SlaXBMC-14./SlaXBMC${LIBDIRSUFFIX}-14./g" $CWD/slack-desc > $PKG/install/slack-desc
sed "s/SlaXBMC-14./SlaXBMC${LIBDIRSUFFIX}-14./g" $CWD/slack-desc > $PKG/usr/doc/$PKGNAM-$VERSION/slack-desc
cat $CWD/$PKGNAM.SlackBuild > $PKG/usr/doc/$PKGNAM-$VERSION/$PKGNAM.SlackBuild

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

