#!/bin/bash

# Slackware build script for pngcheck

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

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

# I've been using this for ages, off and on, and was kinda shocked I'd
# never done a SBo build for it. So here it is.

# 20251225 bkw: Upstream dropped pngsplit and png-fix-IDAT-windowsize from
# their repo for 4.0.1, but (so far as I can tell) haven't released them
# anywhere else. So we'll include the 4.0.0 versions of the dropped tools,
# for now.

# 20251225 bkw: update for 4.0.1.
# 20250617 bkw: update for 4.0.0, new homepage.

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

PRGNAM=pngcheck
VERSION=${VERSION:-4.0.1}
OLDVER=${OLDVER:-4.0.0}
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}

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

TOPDIR=$TMP/$PRGNAM-build

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

# 20251225 bkw: first, pngcheck itself.
cd $PRGNAM-$VERSION
make CPPFLAGS=" " LDFLAGS=" " STDC=" " CFLAGS="$SLKCFLAGS"

# no 'make install', but dead simple.
PKGBIN=$PKG/usr/bin
PKGMAN1=$PKG/usr/man/man1
mkdir -p $PKGBIN $PKGMAN1

install -m0755 -s $PRGNAM $PKGBIN
install -m0644 *.1 $PKGMAN1

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC/gpl
cp -a CHANGELOG* LICENSE* README* $PKGDOC

# 20251225 bkw: now, just the other tools from the old version.
cd $TOPDIR/$PRGNAM-$OLDVER
sed -i "/^CFLAGS/s,-O,$SLKCFLAGS," Makefile.unx

# 20250617 bkw: the CERRORS="-Wall" turns off the default -Werror.
# Why does anyone ship release code with -Werror enabled? *Especially*
# when the code actually *has* warnings?!
make ZINC=" " ZLIB=-lz CERRORS="-Wall" -f Makefile.unx

install -m0755 -s pngsplit png-fix-IDAT-windowsize $PKGBIN
install -m0644 gpl/*.1 $PKGMAN1
gzip -9 $PKGMAN1/*

cp -a gpl/COPYING* $PKGDOC/gpl
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
