#!/bin/sh

# SlackBuild script for vwm.
#
# Copyright 2009 Pierre Cazenave <email removed>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Previously maintained by Gerardo Zamudio.
# Now maintained by B. Watson <yalhcru@gmail.com>.

# 20170825 bkw:
# - Take over maintenance.
# - Update for v2.1.3.
# - slack-desc cosmetics.
# - Don't install useless INSTALL to doc dir.
# - Remove -j1 from the make commands, except the one for keycodes. seems
#   not to be needed any longer.
# - Simplify script a bit.
# - Build a private static 'libvterm', which is NOT the same libvterm as
#   libraries/libvterm on SBo. Unfortunately this adds a lot of complexity
#   back to the script :(
# - Rebase patches/*.diff against 2.1.3 and combine into one patch.

PRGNAM=vwm
VERSION=${VERSION:-2.1.3}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
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

fixperms() {
  chown -R root:root .
  find -L . \
     \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
     -exec chmod 755 {} \; -o \
     \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
     -exec chmod 644 {} \;
}

set -e

rm -rf $PKG
mkdir -p $TMP/$PRGNAM $PKG $OUTPUT

# First, the library. This is "libvterm", by the same author as vwm,
# but it's completely unrelated to the libvterm on SBo. Two projects
# happened to pick the same name. This one doesn't build a static lib
# by default (which we need) so it's done here with 'ar'. None of this
# stuff gets included in the package.
# libvterm-20170804.tar.xz was created thus:
# git clone https://github.com/TragicWarrior/libvterm.git libvterm-20170804
# tar cvfJ libvterm-20170804.tar.xz libvterm-20170804
LIBNAM=libvterm
LIBVER=${LIBVER:-20170804}

cd $TMP/$PRGNAM
rm -rf $LIBNAM-$LIBVER
tar xvf $CWD/$LIBNAM-$LIBVER.tar.xz
cd $LIBNAM-$LIBVER
fixperms

# Build a static library. We need -fPIC even on 32-bit.
make CFLAGS="$SLKCFLAGS -fPIC"
rm -f *.so
ar rcs $LIBNAM.a *.o

# Now, the main program, which will link with the static lib we just built.
cd $TMP/$PRGNAM
rm -rf $PRGNAM
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM
fixperms

# Patch the relevant Makefiles to not use hardcoded paths.
patch -p1 < $CWD/patches/paths.diff

# Allows us to pass a full path to libvterm.a, avoids 'undefined symbol'
# when starting vmterm3.
patch -p1 < $CWD/patches/vterm_static.diff

LIBDIR=$TMP/$PRGNAM/$LIBNAM-$LIBVER
make CFLAGS="$SLKCFLAGS -I$LIBDIR" VTERM_A=$LIBDIR/libvterm.a
make install prefix=$PKG/usr libdir=$PKG/usr/lib${LIBDIRSUFFIX}

# Build and install the sysmon module (the vmwterm3 one is done with
# the main installation).
cd modules/sysmon
   make CFLAGS="$SLKCFLAGS -I$PKG/usr/include"
   make install prefix=$PKG/usr libdir=$PKG/usr/lib${LIBDIRSUFFIX}
cd -

# Build and install the keycodes tool to determine codes for remapping
# keystrokes in '~/.vwm/vwmrc'.
make -j1 keycode_tool
install -s -m0755 keycodes/keycodes $PKG/usr/bin/

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a CHANGELOG LICENSE NOTES samples $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README > $PKG/usr/doc/$PRGNAM-$VERSION/README$TAG

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:-tgz}
