#!/bin/bash

# Slackware build script for pdfsandwich

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

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

# This thing is written in ocaml, but all it does is call a bunch of
# external binaries to do all the work. IMO, it would have been better
# as a shell script, but there's a thing about a hammer and nails...

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

PRGNAM=pdfsandwich
VERSION=${VERSION:-0.1.7}
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

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

# nothing needs LIBDIRSUFFIX or SLKCFLAGS (it's not even written in C).

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod -h 755 {} + -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod -h 644 {} +

# fix typo (debian does this with a patch):
sed -i 's,defaut,default,' $PRGNAM.ml

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION

# the -j1 is needed. parallelism wouldn't speed it up much anyway, it
# compiles 2 ocaml files, then generates the man page with a bundled
# shell script called txt2man. The configure script isn't an autoconf
# one, the only option it takes is --prefix.
./configure --prefix=/usr
make -j1 all install \
  DESTDIR=$PKG \
  INSTALLMANDIR=$PKG/usr/man/man1 \
  INSTALLDOCDIR=$PKGDOC

# binary already stripped, man page already gzipped, all is well.

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
