#!/bin/bash

# Slackware build script for SLiM

# Copyright 2006  Martin Lefebvre
# Copyright 2007-2009 Frank Caraballo
# Copyright 2010,2013 Niels Horn, Rio de Janeiro, Brazil
# Copyright 2014 Ryan P.C. McQuen | Everett, WA
# Formerly maintained by David Spencer
# Copyright 2024 B. Watson <urchlay@slackware.uk>
# 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.

# 20240924 bkw: BUILD=4
# - take over maintenance.
# - make /etc/slimlock.conf a .new config file.
# - get useless INSTALL out of doc dir.

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

PRGNAM=slim
VERSION=${VERSION:-1.3.6}
BUILD=${BUILD:-4}
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

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

# Correct the path used for suspend and a few things about sessions
patch -p1 < $CWD/patches/slim-1.3.6-config.diff
# Fix for freetype-2.5.5 headers
patch -p1 < $CWD/patches/slim-1.3.6_freetype-2.5.1.diff
# Fixes for slim libraries
patch -p1 < $CWD/patches/slim-1.3.6-libslim-cmake-fixes.patch
# Honour our FLAGS
patch -p1 < $CWD/patches/slim-1.3.6-honour-cflags.patch
# Fix for the latest gcc
patch -p1 < $CWD/patches/slim-1.3.6-gcc11.patch
# Fix a bug
patch -p1 < $CWD/patches/slim-1.3.6-envcpy-bad-pointer-arithmetic.patch

# Fix location of man-pages
sed -i "s,share/man,man," CMakeLists.txt

mkdir -p build
cd build
  cmake \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DLIB_SUFFIX=$LIBDIRSUFFIX \
    -DUSE_PAM=yes \
    -DUSE_CONSOLEKIT=no \
    -DCMAKE_BUILD_TYPE=Release ..
  make
  make install/strip DESTDIR=$PKG
cd ..

# Don't overwrite existing config file:
mv $PKG/etc/slim.conf $PKG/etc/slim.conf.new

# Add the slackware-black theme to the package:
tar xvf $CWD/slackware-black.tar.bz2 -C $PKG/usr/share/slim/themes

# Add a logrotate file
mkdir -p $PKG/etc/logrotate.d
cat $CWD/files/slim.logrotate > $PKG/etc/logrotate.d/slim.new

# Kill the incorrectly-placed (and useless anyway) systemd stuff
rm -rf $PKG/usr/usr

# Kill systemd stuff
rm -rf $PKG/lib

install -m 0644 -D $CWD/files/Xsession $PKG/usr/share/slim/Xsession
install -m 0644 -D $CWD/files/slim.pam $PKG/etc/pam.d/slim/slim.pam
install -m 0644 -D $CWD/files/slimlock.pam $PKG/etc/pam.d/slimlock/slimlock.pam
install -m 0644 -D slimlock.conf $PKG/etc/slimlock.conf.new

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

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a ChangeLog COPYING README THEMES TODO xinitrc.sample $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild
cat $CWD/README.SLACKWARE > $PKGDOC/README.SLACKWARE

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
