#!/bin/bash

# Slackware build script for syncterm

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

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

# Issues:
# 1. Figure out why shell: doesn't work on the command line (though
#    it does, with the ^D "quick connect" option, or by adding it to
#    the dialing list). Though, if I add it to the dialing list, I
#    still can't make it work directly from the command line, even
#    though it works from the menu. *Shrug*.

# 20251227 bkw: update for v1.7.
# 20250123 bkw: update for v1.4.

# 20241203 bkw: update for v1.3.
# - patch for -current no longer needed (and no longer works anyway), removed.
# - 'make install' now installs the icons, stopping shipping our own.
# - manpage.diff reworked for 1.3.
# - remove -j1 from make: parallel builds work fine now.

# 20240218 bkw: BUILD=2
# - add compile fix for -current.

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

PRGNAM=syncterm
VERSION=${VERSION:-1.7}
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

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

# Man page: Fix path to dialing list and a couple typos. The path
# baked into the binary really is /etc/syncterm.lst (not /usr/etc)...
# In 1.3, the system dialing list actually works. If both the system
# and user lists both exist, both are read, which is nice.
patch -p1 < $CWD/manpage.diff

# This is a big hassle: -O3 is hardcoded in a makefile... which is
# inside a zip file. To force our flags, we have to extract the zip
# file, mod the makefile, then re-zip it. Using -0 with zip means
# "store" (for speed).
cd 3rdp/dist
unzip -oa cryptlib.zip
rm -f cryptlib.zip
sed -i "s,-O3,$SLKCFLAGS," makefile
zip -0r cryptlib.zip *
cd -

# Rest of the flags:
sed -i "s,-O[23],$SLKCFLAGS," src/build/Common.gmake
sed -i "s,-O3,\"$SLKCFLAGS\"," 3rdp/build/GNUmakefile

# Top-level source directory:
cd src/$PRGNAM

# Lots of make options here:
# - RELEASE=1 means binary already stripped, man page already gzipped.
# - WITHOUT_PORTAUDIO=1 to avoid a hidden dependency on portaudio. I
#   see no advantage to using portaudio anyway (SDL audio works fine).
# - INSTALL_DATA: install man page with usual Slackware permissions.
# - SRC_ROOT has to be set because the makefile is dumb...
make \
  PREFIX=/usr \
  MANPREFIX=/usr \
  SYSTEM_LIST_DIR=/etc \
  SRC_ROOT="$( realpath .. )" \
  USE_SDL=1 \
  USE_SDL_AUDIO=1 \
  WITHOUT_PORTAUDIO=1 \
  VERBOSE=1 \
  RELEASE=1 \
  DESTDIR=$PKG \
  INSTALL_DATA="install -m 0644" \
  all install

# Compile the terminfo entries. Have to run the binary we just built,
# let's not do it as root.
su nobody -s /bin/sh -c "$PKG/usr/bin/$PRGNAM -T" | \
  tic -o $PKG/usr/share/terminfo -

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

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a Install-Mozilla.txt CHANGES LICENCE gpl.txt $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

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