#!/bin/bash

# Slackware build script for xzgv

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

# Original author: Markus Reichelt.
# Now maintained by B. Watson <urchlay@slackware.uk>.

# 20230308 bkw: BUILD=2, add doinunst.sh to clean up GNU info index.

# 20230102 bkw: v0.9.2_2
# - take over maintenance.
# - relicense as WTFPL.
# - include Debian's improved .desktop file and PNG icons.
# - include GNU info doc in package.
# - fix documentation permissions.
# - get rid of useless INSTALL from docs.
# - use Debian patch to fix man page nroff formatting.

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

PRGNAM=xzgv
VERSION=${VERSION:-0.9.2_2}
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"
  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

SRCVER="$( echo $VERSION | cut -d_ -f1 )"
DEBVER="$( echo $VERSION | cut -d_ -f2 )"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$SRCVER
tar xvf $CWD/$PRGNAM-$SRCVER.tar.gz
cd $PRGNAM-$SRCVER
tar xvf $CWD/${PRGNAM}_$SRCVER-$DEBVER.debian.tar.xz
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 {} \+

# use Debian's .desktop file
cp debian/$PRGNAM.desktop src/

# use 48x48 old-style icon
sed -i 's,icon-64,icon-48,' src/Makefile

# apply Debian's patches (currently, fixes for the man page)
for i in $( cat debian/patches/series ); do
  patch -p1 < debian/patches/$i
done

cat > config.mk <<EOF
CC=gcc
AWK=awk
CFLAGS=$SLKCFLAGS
BINDIR=$PKG/usr/bin
INFODIR=$PKG/usr/info
MANDIR=$PKG/usr/man/man1
USE_A4_DEF=-t @afourpaper
LDFLAGS=-lX11 -lm
PIXMAPDIR=$PKG/usr/share/pixmaps
DESKTOPDIR1=$PKG/usr/share/applications
DESKTOPDIR2=$PKG/usr/share/applications
EOF

mkdir -p $PKG/usr/{bin,man/man1,info,share/{pixmaps,applications}}

make
strip src/$PRGNAM
make info
make install
rm -f $PKG/usr/info/dir
chmod 644 $PKG/usr/man/man*/*
gzip $PKG/usr/man/man*/*

# include Debian's PNG icons
for i in debian/$PRGNAM-*.png; do
  px="$( basename $i .png | cut -d- -f2 )"
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  install -m0644 -oroot -groot $i $dir/$PRGNAM.png
done

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a AUTHORS ChangeLog COPYING NEWS README TODO $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/douninst.sh > $PKG/install/douninst.sh

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