#!/bin/bash

# Slackware build script for dwdiff
# Written by slakmagik.
# Released under the WTFPL.
# Modified and now maintained by B. Watson <urchlay@slackware.uk>.

# 20260707 bkw, BUILD=2:
# - take over maintenance.
# - get rid of option to disable Unicode (it's 2026, Unicode has been
#   around for longer than we had ASCII before Unicode). this is the
#   upstream default, and lets me get rid of a couple of patches.
# - likewise, get rid of NLS option (install all supported languages).
#   this is the upstream default, too.

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

PRGNAM=dwdiff
VERSION=${VERSION:-2.1.4}
BUILD=${BUILD:-2}
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}

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 $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM-$VERSION
chown -R root:root .
find . ! -type l    -a \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \)

# Patch for configure --mandir
patch -p1 < $CWD/config.pkg.patch

# This is one messed up makefile - and its configure doesn't accept '--build'
CC="gcc -fgnu89-inline" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --with-unicode \
  --prefix=/usr \
  --mandir='$(prefix)/man'

# 20260707 bkw: --docdir accepted and ignored...
sed -i "/^docdir=/s,share/doc/.*,doc/$PRGNAM-$VERSION," Makefile

make prefix=/usr
strip dwdiff dwfilter
make prefix=$PKG/usr install
gzip -9 $PKG/usr/man/{man1,*/man1}/*

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
