#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# x11-skel.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# Copyright 2008, 2009, 2011, 2012, 2018  Patrick J. Volkerding, Sebeka, Minnesota, USA
# 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.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Create package framework:
cd $PKG
mkdir -p etc/xdg
mkdir -p etc/X11
mkdir -p usr/lib${LIBDIRSUFFIX}/X11
mkdir -p usr/bin
mkdir -p var/log/setup
mkdir -p usr/man/man1
mkdir -p usr/share/xsessions

# On Slackware ARM, we may install an Xorg config file for the particular
# Hardware Model upon package installation, so we need this directory:
mkdir -p etc/X11/xorg.conf.d

#cp -a $CWD/scripts/xorg.conf-fbdev etc/X11
cp -a $CWD/scripts/xorg.conf-vesa etc/X11
chown -R root:root etc/X11
find etc/X11 -type f -exec chmod 644 {} \;
cp -a $CWD/scripts/xorgsetup usr/bin
cp -a $CWD/scripts/xwmconfig usr/bin
chmod 755 usr/bin/*
sed -i -e "s#lib/#lib${LIBDIRSUFFIX}/#g" usr/bin/*

cp -a $CWD/scripts/setup.xwmconfig var/log/setup
chown root:root var/log/setup/setup.xwmconfig
chmod 755 var/log/setup/setup.xwmconfig
install -vpm644 $CWD/manpages/xwmconfig.1 $PKG/usr/man/man1/
install -vpm644 $CWD/scripts/xwmconfig.desktop usr/share/xsessions

# This is an x86 config file.
rm -f $PKG/etc/X11/xorg.conf-vesa

# Add ARM default xorg config that uses fbdev.
# I think this works universally!
# Commented - see note below.
#mkdir -vpm755 $PKG/etc/X11/xorg.conf.d/
#install -vpm644 $PORTCWD/xorg.conf.new $PKG/ $PKG/etc/X11/xorg.conf.d/
#
# Old code for Hardware Model-specific config files.
# We may need to restore this if particular on particular Hardware Models,
# Xorg cannot auto-detect settings at run time.
#install -vpm644 $PORTCWD/arm/xorg.conf-{versatile,openrd}* etc/X11/
#install -vpm644 $PORTCWD/arm/xorg.conf* etc/X11/

# Add any platform and Hardware Model-specific Xorg configuration
# files.  Normally Slackware ARM/AArch64 doesn't provide an xorg.conf
# since Xorg can auto-configure; but some Hardware Models require either
# help or a specific config to avoid Xorg using a problematic
# config or driver/both!
if [ -d $PORTCWD/sources/x11-skel-share/platform/$SLKPORTARCH ]; then
   mkdir -vpm755 $PKG/usr/share/x11-skel/platform
   cp -fa $PORTCWD/sources/x11-skel-share/platform/$SLKPORTARCH $PKG/usr/share/x11-skel/platform/
   find $PKG/usr/share/x11-skel/platform/ -type f -print0 | xargs -0 chmod 644
   find $PKG/usr/share/x11-skel/platform/ -type d -print0 | xargs -0 chmod 755
fi

# Patch the setup script to exit if it finds itself
# running on a headless platform:
# Don't do this anymore because it's useful to configure the default
# window manager for use with tightvnc
# in the 'extra' packages.
#( cd $PKG/var/log/setup
#  patch -p0 --verbose < $PORTCWD/arm/setup.xwmconfig.diff || exit 1)

# Apply generic Slackware packaging policies:
cd $PKG
slackdesc       # install slack-desc and doinst.sh

# Add in the post install script which modifies the configuration based on
# the Hardware Model upon which this package is being installed.
cat $PORTCWD/doinst.sh >> install/doinst.sh
#
# Support 64bit locations:
sed -i "s#lib/#lib${LIBDIRSUFFIX}/#g" install/doinst.sh
sed -i "s#lib #lib${LIBDIRSUFFIX} #g" install/doinst.sh

slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
#slackstriprpaths     # strip rpaths
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
