#!/bin/bash

# Slackware build script for cwiid

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

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

# 20220410 bkw: BUILD=2
# - *really* fix permissions on header.
# - re-add --disable-ldconfig. configure claims it's unsupported, but
#   it actually works.

# 20210910 bkw:
# - Upgrade to 0.6.91_2 (Debian's 0.6.91-2).
# - Actually apply SLKCFLAGS.
# - Use icon, .desktop, lswm.1 from Debian tarball instead of
#   including in SBo git.

# 20170306 bkw:
# - Add --disable-ldconfig

# 20130318 bkw:
# - Upgraded from 0.6.00 to 0.6.00+svn201
# - Added init script
# - Added lswm man page
# - Added .desktop and icon
# - Removed bluez4 API patch (no longer needed)
# - Fixed permissions of config files and header
# - Added ir_fps and nunchuk_kb plugins

# https://deb.debian.org/debian/pool/main/c/cwiid/cwiid_0.6.91-2.debian.tar.xz
# https://deb.debian.org/debian/pool/main/c/cwiid/cwiid_0.6.91.orig.tar.xz

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

PRGNAM=cwiid
VERSION=${VERSION:-0.6.91_2}
BUILD=${BUILD:-2}
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"
  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

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

LIBDIR=/usr/lib$LIBDIRSUFFIX
PKGLIB=$PKG/$LIBDIR

rm -rf $PKG
mkdir -p $TMP $PKG/usr $PKG/etc $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xvf $CWD/${PRGNAM}_$TARVER.orig.tar.xz
cd $PRGNAM
tar xvf $CWD/${PRGNAM}_$TARVER-$DEBVER.debian.tar.xz
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# this patch is debian-specific (calls dpkg):
rm -f debian/patches/python-setup.py.patch

for i in $( cat debian/patches/series ); do
  [ -e $i ] && patch -p1 < debian/patches/$i
done

# re-enable this when needed
#patch -p1 < $CWD/python310.patch

autoreconf -if

./configure \
  --prefix=/usr \
  --libdir=$LIBDIR \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --mandir=/usr/man \
  --disable-ldconfig \
  --build=$ARCH-slackware-linux

# configure script ignores CFLAGS in the env. Can't use CFLAGS
# nor DEBUGFLAGS with make... sneak them in this way.
make WARNFLAGS="-Wall $SLKCFLAGS"
make install DESTDIR=$PKG

# no install-strip...
strip $PKG/usr/bin/* \
      $PKGLIB/*.so.?.? \
      $PKGLIB/$PRGNAM/plugins/*.so \
      $PKGLIB/python*/site-packages/*.so

# Header and configs get installed +x, fix
chmod 644 $PKG/etc/cwiid/wminput/* $PKG/usr/include/cwiid.h

# Remove static library, configure doesn't accept --disable-static
rm -f $PKGLIB/libcwiid.a

gzip -9 $PKG/usr/man/man1/*

# Use Debian's extra man page, desktop file, icons.
gzip -9c debian/lswm.1 > $PKG/usr/man/man1/lswm.1.gz

mkdir -p $PKG/usr/share/pixmaps
cp -a debian/icons/*.xpm $PKG/usr/share/pixmaps

mkdir -p $PKG/usr/share/applications
sed -e '/^Icon/s,=.*,=/usr/share/pixmaps/wmgui.xpm,' \
    -e '/^Categories/s,$,;Settings;,' \
    debian/menus/wmgui.desktop > $PKG/usr/share/applications/wmgui.desktop

# init script written for this build
mkdir -p $PKG/etc/rc.d/
cat $CWD/rc.cwiid.new > $PKG/etc/rc.d/rc.cwiid.new
cat $CWD/rc.cwiid.conf.new > $PKG/etc/rc.d/rc.cwiid.conf.new

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS COPYING ChangeLog NEWS README $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
