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

# pcsc-lite.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2007-2015  LukenShiro, Italy
# Copyright 2020-2023  Matteo Bernardini <ponce@slackbuilds.org>, Pisa, Italy
# Copyright 2025  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

# Detect whether we're building for /patches:
# This function sets the variable 'SLACKPATCHING'
slack_findpkgstore_is_stablerelease

# Temporary build locations:
shm_tmp # Use /dev/shm if >8GB RAM is available & not mounted 'noexec'
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || failextract
slackhousekeeping

# Debug is off by default
DEBUGATR=${DEBUGATR:-0}
# Inserted card's automatic poweroff (on-demand power on)
AUTOPOFF=${AUTOPOFF:-0}         # disabled by default

# Bail out if user or group isn't valid on your system
# For slackbuilds.org, assigned pcscd uid/gid are 257/257
# See http://slackbuilds.org/uid_gid.txt
# 20230801 bkw: grepping /etc/passwd and /etc/group may fail if e.g. NIS
# or LDAP is in use; always use getent. Also, print both messages (for user
# and group) if either doesn't exist.
if ! getent group pcscd &>/dev/null || ! getent passwd pcscd &>/dev/null; then
    echo "  You must have a \"pcscd\" user and group to run this script."
    echo "    # groupadd -g 257 pcscd"
    echo "    # useradd -u 257 -g pcscd -d /var/run/pcscd -s /bin/false pcscd"
    exit 1
fi

# ATR (answer to reset) debug parsing output
case "$DEBUGATR" in
    0)  DEBUGATR_FLAG="dis" ;;
    *)  DEBUGATR_FLAG="en" ;;
esac

# disabling/enabling card's automatic power off
case "$AUTOPOFF" in
    0)  sed -i "s|^\/\* #define DISABLE_ON_DEMAND_POWER_ON \*\/|#define DISABLE_ON_DEMAND_POWER_ON|" \
       src/pcscd.h.in
       ;;
    *) ;;
esac

# Configure:
slack_autotoolsprep
# --enable-runpid=/var/run/pcscd.pid, and
# --enable-muscledropdir="<libdir>/pcsc/services" are not supported anymore;
# pid directory changed to /var/run/pcscd for consistency
# libhal support is gone
# API docs not included anymore
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --mandir=/usr/man \
   --enable-ipcdir=/var/run/pcscd \
   --enable-usbdropdir="/usr/lib${LIBDIRSUFFIX}/pcsc/drivers" \
   --enable-confdir=/etc/reader.conf.d \
   --${DEBUGATR_FLAG}able-debugatr \
   --disable-libsystemd \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package framework:
make install DESTDIR=$PKG || failinstall

# Create the directory for drivers and such (not included with this package):
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/pcsc/{drivers,services}

# Install init script:
mkdir -p $PKG/etc/rc.d
install -vpm644 $CWD/rc.pcscd $PKG/etc/rc.d/rc.pcscd.new

# Default options file:
mkdir -p $PKG/etc/default
install -vpm644 $CWD/pcscd.default $PKG/etc/default/pcscd.new

# formaticc manpage is not automatically installed:
if [ ! -r $PKG/usr/man/man1/formaticc.1 ]; then
   mkdir -p $PKG/usr/man/man1
   install -vpm644 doc/formaticc.1 $PKG/usr/man/man1/
fi

# Correct manpages in reader.conf.5 and pcscd.8:
sed -i "s|@confdir@|/etc/reader.conf.d|g" $PKG/usr/man/man5/reader.conf.5 || exit 1
sed -i "s|@sysconfdir_exp@/init.d/pcscd|/etc/rc.d/rc.pcscd|g" $PKG/usr/man/man8/pcscd.8 || exit 1
sed -i "s|@sysconfdir_exp@|/etc/reader.conf.d|g" $PKG/usr/man/man8/pcscd.8 || exit 1

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  AUTHORS* COPYING* ChangeLog GPL-3.0.txt* HELP* NEWS* README* SECURITY* \
  $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

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