#!/bin/bash

# Slackware build script for ciderpress-tools

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# TODO:
# - Write man pages. Or at least, one man page, describing all the
#   tools. Not doing this now because I have absolutely no idea
#   what some of these tools do (packddd, sstasm?) and because
#   there's almost no documentation for the others.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=ciderpress-tools
VERSION=${VERSION:-4.1.1_d1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

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

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

# Slackware VERSION can't have hyphens, so:
SRCNAM=ciderpress
SRCVER="$( echo $VERSION | sed 's,_,-,g' )"

# Undocumented (broken) option.
# Someday, BZIP2=yes may work. Currently, you can compile with
# bzip2 support, but it doesn't actually work (not a recognized
# disk format).
# In case you're wondering: libnufx supports bzip2, but the diskimg
# library doesn't (and it's non-trivial to add support).
BZIP2=${BZIP2:-no}
if [ "$BZIP2" = "yes" ]; then
  CONFIG_ARG="--enable-bzip2"
  cat <<EOF
***
*** BZIP2=yes doesn't work yet. Remove this message if it's been fixed.
***
EOF
  sleep 5
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$SRCVER
tar xvf $CWD/$SRCNAM-$SRCVER.tar.gz
cd $SRCNAM-$SRCVER
TOPDIR=$( pwd )
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# Since we override OPT, and it contains this, we gotta add it here:
SLKCFLAGS+=" -DHAVE_CONFIG_H"

# DRY...
runmake() {
  make "$@" OPT="$SLKCFLAGS"
}

### nufxlib
cd nufxlib

# We don't need the full panoply of configure args here, they don't
# do anything (and we're not installing this static library).
./configure $CONFIG_ARG

# Plain "make" fails with parallelism (tries to build the samples
# before the library's been built), but this works:
runmake libnufx.a

# If we needed the samples:
#runmake samples  

### diskimg, then libhfs
cd $TOPDIR/diskimg
runmake
cd libhfs
runmake

### Finally, the binaries that use the above libraries.
cd $TOPDIR/linux

# LIBS and LDFLAGS don't work, surgically implant the bzip2 library
# if requested.
[ "$BZIP2" = "yes" ] && sed -i 's,-lz,-lz -lbz2,' Makefile

runmake

### Compile done, no 'make install' so install manually.
# Only going to install the "Sample" and "Bonus" programs that are
# documented in README-linux.md. They all get a cider- prefix because
# they have generic names like "getfile" and "makedisk" and even
# worse, "iconv" (which would conflict with /usr/bin/iconv).
for i in getfile iconv makedisk mdc packddd sstasm; do
  install -s -D $i $PKG/usr/bin/cider-$i
done

cd $TOPDIR

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
# Very sparse documentation. The sed stuff omits the build directions.
sed -n '/^Sample Programs/,$p' README-linux.md > $PKGDOC/README-linux.md
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
