#!/bin/bash

# Slackware build script for game-devices-udev

# Copyright 2024-2025 Jeremy Hansen <jebrhansen+SBo@gmail.com>
# 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.
cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=game-devices-udev
VERSION=${VERSION:-0.25}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

ARCH=noarch

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}

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 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 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

RULESFOLDER=$PKG/lib/udev/rules.d
mkdir -p $RULESFOLDER

install_rules()
{
  install -m 644 $1 $RULESFOLDER
}

if [ "${_8BITDO:-YES}" == "YES" ]; then install_rules 71-8bitdo-controllers.rules; fi
if [ "${ALPHA:-YES}" == "YES" ]; then install_rules 71-alpha_imaging_technology_co-vr.rules; fi
if [ "${ASTRO:-YES}" == "YES" ]; then install_rules 71-astro_gaming-controllers.rules; fi
if [ "${BETOP:-YES}" == "YES" ]; then install_rules 71-betop-controllers.rules; fi
if [ "${CYPRUS:-YES}" == "YES" ]; then install_rules 71-cypress_semiconductor_corp.rules; fi
if [ "${GOOGLE:-YES}" == "YES" ]; then install_rules 71-google-controllers.rules; fi
if [ "${HORI:-YES}" == "YES" ]; then install_rules 71-hori-controllers.rules; fi
if [ "${HTCVR:-YES}" == "YES" ]; then install_rules 71-htc-vr.rules; fi
if [ "${LOGITECH:-YES}" == "YES" ]; then install_rules 71-logitech-controllers.rules; fi
if [ "${MADCATZ:-YES}" == "YES" ]; then install_rules 71-mad_catz-controllers.rules; fi
if [ "${MIRCOSOFT:-YES}" == "YES" ]; then install_rules 71-microsoft-controllers.rules; fi
if [ "${NACON:-YES}" == "YES" ]; then install_rules 71-nacon-controllers.rules; fi
if [ "${NINTENDO:-YES}" == "YES" ]; then install_rules 71-nintendo-controllers.rules; fi
if [ "${NVIDIA:-YES}" == "YES" ]; then install_rules 71-nvidia-controllers.rules; fi
if [ "${PDP:-YES}" == "YES" ]; then install_rules 71-pdp-controllers.rules; fi
if [ "${PERSONALCOMM:-YES}" == "YES" ]; then install_rules 71-personal_communication_systems_inc-controllers.rules; fi
if [ "${PIDCODES:-YES}" == "YES" ]; then install_rules 71-pid_codes-controllers.rules; fi
if [ "${POWERA:-YES}" == "YES" ]; then install_rules 71-powera-controllers.rules; fi
if [ "${RAZER:-YES}" == "YES" ]; then install_rules 71-razer-controllers.rules; fi
if [ "${RASPBERRYPI:-YES}" == "YES" ]; then install_rules 71-raspberry-pi-ltd.rules; fi
if [ "${SONY:-YES}" == "YES" ]; then install_rules 71-sony-controllers.rules; fi
if [ "${SONYVR:-YES}" == "YES" ]; then install_rules 71-sony-vr.rules; fi
if [ "${UNINPUT:-YES}" == "YES" ]; then install_rules 71-uinput-dev-early-creation.rules; fi
if [ "${VALVE:-YES}" == "YES" ]; then install_rules 71-valve-controllers.rules; fi
if [ "${VALVEVR:-YES}" == "YES" ]; then install_rules 71-valve-vr.rules; fi
if [ "${VKBSIM:-YES}" == "YES" ]; then install_rules 71-vkb_sim-controllers.rules; fi
if [ "${ZEROPLUS:-YES}" == "YES" ]; then install_rules 71-zeroplus_technology_corporation-controllers.rules; fi

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  LICENSE README.md \
  $PKG/usr/doc/$PRGNAM-$VERSION
# Only copy this README over if the rule is installed
if [ "${_8BITDO:-YES}" == "YES" ]; then cp 8BitDo.md $PKG/usr/doc/$PRGNAM-$VERSION; fi
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README.devices > $PKG/usr/doc/$PRGNAM-$VERSION/README.devices

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
