#!/bin/bash

# Slackware build script for mobiperl

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

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

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

PRGNAM=mobiperl
VERSION=${VERSION:-0.0.43}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

ARCH=noarch

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}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar
cd $PRGNAM-$VERSION
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 {} \+

# Get rid of some perl module dependencies, replacing with stuff that's
# shipped with Slackware's perl package. Specifically:
# The obsolete Getopt::Mixed module is replaced by Getopt::Long.
# Date::Format is only used for ctime(), which is replaced by POSIX::ctime().
# Date::Parse is supposedly required, but is never used (removed it).
# For anyone reading this, no, this didn't break opf2mobi (it was
# already broken).
patch -p1 < $CWD/reduce_dependency_hell.diff

# lit2mobi calls convertlit by its old name, which might trigger
# stupid content filters. Replace with modern inoffensive name, in
# silly obfuscated way so this script doesn't contain the old name.
# (In case you're wondering, I'm not offended by this, I just don't want
# to get complaints from easily-offended people).
sed -i 's,\<cl[^\000-\150\152-]t\>,convertlit,g' MobiPerl/Util.pm lit2mobi

# It's pure Perl code (no shared libs), but not packaged as a perl module.
# We'll install the modules in the standard place for 3rd-party perl modules.
mkdir -p $PKG/usr/share/perl5/vendor_perl
cp -a Palm MobiPerl $PKG/usr/share/perl5/vendor_perl

mkdir -p $PKG/usr/man/man1 $PKG/usr/bin
EXES="html2mobi lit2mobi mobi2html mobi2mobi opf2mobi mobils"
for exe in $EXES; do
  pod2man -c 'SlackBuilds.org' -r $VERSION $exe | gzip -9c > $PKG/usr/man/man1/$exe.1.gz
  install -m0755 -oroot -groot $exe $PKG/usr/bin/$exe
done

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README *.txt $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$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
