#!/bin/bash

# Written by Benjamin Trigona-Harany <email removed>

# Modified by B. Watson <urchlay@slackware.uk>

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

# 20230106 bkw: BUILD=4, add doinst.sh

# 20211121 bkw: BUILD=3
# - new-style icons.
# - replace old icon, too small for modern displays.

# 20140921 bkw: bump BUILD to 2
# - took over maintenance
# - add man page
# - add mp3valgui and GUI=no to disable it
# - make SLKCFLAGS actually get used
# - patch to fix -Wwrite-strings warnings, future-proofing since this may
#   become an error someday.
# - minor script cleanups

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

PRGNAM=mp3val
VERSION=${VERSION:-0.1.8}
BUILD=${BUILD:-4}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) export ARCH=i586 ;;
    arm*) export ARCH=arm ;;
       *) export 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

SRCNAM=$PRGNAM-$VERSION-src

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM
tar xvf $CWD/$SRCNAM.tar.gz
cd $SRCNAM

# upstream's permissions are horrid, please don't change this to boilerplate
chown -R root:root .
chmod 644 *

# this isn't strictly necessary for Slack 14.1, but at some point these
# warnings might possibly become errors in some future version of gcc.
zcat $CWD/fix_compile_warnings.diff.gz | patch -p1

make -f Makefile.linux CXXFLAGS="$SLKCFLAGS"
mkdir -p $PKG/usr/bin
install -s -m0755 $PRGNAM $PKG/usr/bin

# man page converted from manual.html with html2pod | pod2man
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# package the GUI unless disabled
if [ "${GUI:-yes}" = "yes" ]; then
  GUINAME=${PRGNAM}gui
  GUIVER=${GUIVER:-0.1.1}
  unzip $CWD/$GUINAME-$GUIVER.zip
  cd $GUINAME

  # damn thing expects to find its .glade file somewhere in $PATH or the
  # current dir. Let's use /usr/share/mp3valgui/ instead.
  sed -i \
    '/gladefile *= *locate/s,=.*,= "/usr/share/'$GUINAME/$GUINAME.glade'",' \
    $GUINAME.py

  mkdir -p $PKG/usr/share/$GUINAME
  install -m0755 $GUINAME.py $PKG/usr/bin/$GUINAME
  cat $GUINAME.glade > $PKG/usr/share/$GUINAME/$GUINAME.glade

  # .desktop taken from mandriva's mp3valgui-0.1.1-1mdv2007.1.noarch.rpm
  # and modified.
  mkdir -p $PKG/usr/share/applications
  cat $CWD/$GUINAME.desktop > $PKG/usr/share/applications/$GUINAME.desktop

  # icon made by SlackBuild author. Not pretty, sorry.
  for px in 16 32 48 64 96; do
    size=${px}x${px}
    dir=$PKG/usr/share/icons/hicolor/$size/apps
    mkdir -p $dir
    convert -resize $size $CWD/$GUINAME.png $dir/$GUINAME.png
  done

  mkdir -p $PKG/usr/share/pixmaps
  ln -s ../icons/hicolor/48x48/apps/$GUINAME.png $PKG/usr/share/pixmaps/$GUINAME.png

  mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/$GUINAME-$GUIVER
  cp -a AUTHORS COPYING NEWS $PKG/usr/doc/$PRGNAM-$VERSION/$GUINAME-$GUIVER
  cd -
fi

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a changelog.txt manual.html $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
cat $CWD/doinst.sh > $PKG/install/doinst.sh

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