#!/bin/sh
# ----------------------------------------------------------------------
#
# Copyright (c) 2019 Andrew Strong, Blue Mountains, Australia.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS 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.
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:        Andrew Strong <andrew.david.strong@gmail.com>
# For:       Jörg Schilling's star utility
# Descr:     An alternative for GNU tar, can coexist with this quite happily.
# URL:       https://sourceforge.net/projects/s-tar/
# Needs:     A full Slackware install can compile this.
# Slack:     Tested under slackware -current
# Latest:    March 2019: Script fine tuning, stealing some ideas from
#                        PV's cdrtools package.        
# -----------------------------------------------------------------------------

PRGNAM=star
VERSION=${VERSION:-1.5.3}
BUILD=${BUILD:-1}
TAG=${TAG:-_ads}

NUMJOBS=${NUMJOBS:-" -j$(expr $(nproc) + 1) "}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/ads}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
rm -rf $TMP/$PRGNAM-$VERSION
cd $TMP
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION

# Fix ownership and permissions before we start:
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# Run the makefile and install to $PKG:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
make -e $NUMJOBS GMAKE_NOWARN=true INS_BASE=/usr DEFMANBASE=.
make -e install GMAKE_NOWARN=true INS_BASE=/usr DEFMANBASE=. DESTDIR=$PKG 

# Eliminate a bunch of files extraneous to this package:
# 1. Executables and symlinks we don't want
rm $PKG/usr/bin/{smt,star_sym,tartest,gnutar,mt,scpio,spax,suntar,tar,ustar} \

# 2. Various directories we don't want
rm -rf $PKG/etc \
       $PKG/usr/include \
       $PKG/usr/lib \
       $PKG/usr/sbin \
       $PKG/usr/share

# 3. Remove all but one of the man pages
rm -rf $PKG/usr/man/{man3/,man5/} \
       $PKG/usr/man/man1/{gnutar.1,match.1,mt.1,rmt.1,scpio.1,smt.1,spax.1,suntar.1,tartest.1}

# Strip files in the package:
find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# Sort out the documentation:
# 1. gzip the remaining man page (for star only now)
find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

# 2. Make the docs directory and move required licences etc into it
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a *GPL* CDDL* AN-1.5.3 star/README $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# 3. Package up the slack-desc file
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Create the package:
cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
