#!/bin/bash

# Slackware build script for transcribe

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

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

# Notes:

# Closed-source software, precompiled binaries for x86_64 (and
# formerly x86), deps are all included in Slackware.

# Originally ported from Windows, so it expects to be run from its
# own directory. We install to /usr/lib(64)?/transcribe, and a wrapper
# script /usr/bin/transcribe that cd's to the install dir and runs
# the app from there (this allows the app's help system to work).

# Upgrading from the 30-day eval to the registered version is just
# a matter of entering the license key (no change to the package
# needed).

# The version number doesn't appear in the tarball filename or the URL,
# so be careful that $VERSION matches the actual version number (found
# in Help/About)... though starting with v8.80, I host the tarballs
# and put $VERSION in the filenames.

# 20260622 bkw: updated for v9.51.1 and 15.0. Since upstream dropped
# 32-bit support, this build is now x86_64 only, and I've added a
# new transcribe-legacy for 32-bit users. Added README section about
# gstreamer sinks, too.

# 20211127 bkw: updated for v8.80 and -current. This is the last
# version that supports 32-bit. I tried the latest verion (9.10.0)
# on 64-bit, but it fails to init the audio device, then crashes
# with a "malloc(): unaligned tcache chunk detected", which means
# either an actual bug (that I can't fix due to lack of source) or an
# incompatible library (the 9.10.0 binary is built on XUbuntu 20.4).

# 20191211 bkw: updated for v8.73. This is the "older version" upstream
# provides for OSes where the libraries (including glibc) are too
# old for the latest (8.74.1) version. v8.74.1 requires glibc-2.27,
# while Slackware 14.2 only has 2.23. Please don't ask me to update to
# versions later than 8.73, I won't be able to do that until Slackware
# 15.0 is released. I'm hosting the download files on my server because
# at some point, upstream will release a version 8.75. At that point
# it's likely that 8.73 will disappear from his site.

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

PRGNAM=transcribe
VERSION=${VERSION:-9.51.1}
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" != "x86_64" ]; then
  echo "*** Unsupported ARCH '$ARCH', only x86_64 is supported."
  exit 1
fi

set -e

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

mkdir -p $PKG/usr/lib64/$PRGNAM \
         $PKG/usr/doc/$PRGNAM-$VERSION \
         $PKG/usr/bin \
         $PKG/install \
         $PKG/usr/share/applications \
         $PKG/usr/share/pixmaps

install -oroot -groot -m0755 $PRGNAM $PKG/usr/lib64/$PRGNAM/
install -oroot -groot -m0644 xschelp.htb $PKG/usr/lib64/$PRGNAM/
install -oroot -groot -m0644 readme_gtk.html $PKG/usr/doc/$PRGNAM-$VERSION/
install -oroot -groot -m0755 $CWD/$PRGNAM.sh $PKG/usr/bin/$PRGNAM

for i in gtkicons/*.png; do
  px="$( basename $i | cut -dx -f3 | cut -d. -f1 )"
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  cat $i > $dir/$PRGNAM.png
done

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

# Starting with v8.70, a gstreamer plugin is included.
if [ -e libgstvideosection.so ]; then
  mkdir -p $PKG/usr/lib64/gstreamer-1.0
  install -m0755 libgstvideosection.so $PKG/usr/lib64/gstreamer-1.0
fi

cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop
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
