#!/bin/bash

# Slackware build script for ssvnc

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

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

# Note: This was formerly on SBo and got removed for 15.0. I'm
# re-adding it because, so far as I know, it's the only Linux VNC
# client that supports client-side scaling, and I have a need for
# that. I didn't base this on the old ssvnc.SlackBuild (didn't even
# look at it, though I did attempt to run it and watched it fail).
# There are some differences:
# - This build makes ultraftp optional, so it doesn't require any
#   JDK. ultraftp supports the file transfer extension of the
#   (Windows-only) UltraVNC server. I don't need this, do you?
# - The old build was for a development version (1.0.30), this one
#   is for the last stable (1.0.29).
# - This build includes patches from Debian, which fix some bugs
#   and security holes.

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

PRGNAM=ssvnc
VERSION=${VERSION:-1.0.29_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" -o "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=$ARCH -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi
LIB=lib$LIBDIRSUFFIX

set -e

TARVER=${VERSION/_*}
DEBVER=${VERSION/*_}

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

# Don't apply this patch, since we want stripped binaries:
sed -i '/^nostrip/d' debian/patches/series

# Don't apply this; we hardcode the ultraftp.jar path, below.

# This got removed from SBo because it wouldn't build on
# 15.0. Debian's patches include openssl1.1.patch, which fixes
# it. They also patch a few CVEs.
for i in $( cat debian/patches/series ); do
  echo "===> applying $i"
  patch -p1 < debian/patches/$i
done

# Use correct path for ultraftp.
sed -i "/SSVNC_ULTRA_FTP_JAR/s,lib,lib$LIBDIRSUFFIX," \
       vnc_unixsrc/vncviewer/argsresources.c

# Use our icon, not a generic one.
sed -i "/^Icon/s,computer,$PRGNAM," $PRGNAM.desktop

# The JSRC="" turns off Java support.
JSRC="JSRC=\"\""
WITH="WITHOUT"

if [ "${ULTRAFTP:-no}" = "yes" ]; then
  JSRC=""
  WITH="WITH"
fi

# ROOT does what DESTDIR normally does.
# Override EPATH because it normally includes /usr/local/bin.
# Force gcc for the whole thing, since 3 of the sub-makes force it.
runmake() {
  make \
    CC=gcc \
    CFLAGS="$SLKCFLAGS" \
    EPATH=/usr/bin \
    PREFIX=/usr \
    LIB=$LIB/ssvnc \
    ROOT=$PKG \
    $JSRC \
    $1
}

runmake all
runmake install
strip $PKG/usr/lib*/ssvnc/*.so
gzip -9 $PKG/usr/man/man*/*

# Lame icon created by SlackBuild author. It's the gnome 'computer.png'
# with overlaid "SSVNC" text from the Atari 800.
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps $PKG/usr/share/pixmaps
cat $CWD/$PRGNAM.png > $PKG/usr/share/icons/hicolor/48x48/apps/$PRGNAM.png
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 COPYING README ReleaseNotes $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed "s,@WITH@,$WITH," < $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
