#!/bin/bash
###################################################################################
# File.......: /usr/lib/setup/armedslack-postinstall-scripts/001-hwm-os-configure
# Called from: /usr/lib/setup/armedslack-run-postinstall-scripts
#              (sourced from SeTconfig)
# Purpose....: Configure Hardware Models from within the Slackware Installer
# Version....: 1.00
# Date.......: 01-Feb-2022
# Author.....: Stuart Winter <mozes@slackware.com>
###################################################################################
#
# Copyright 2022  Stuart Winter, Donostia, Spain.
# 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.

# This script is called from /usr/lib/setup/armedslack-run-postinstall-scripts
#
# Two arguments are provided:
# 1 -- the target prefix (normally /, but ${T_PX} from the bootdisk)
# 2 -- the name of the root device.
#( cd $T_PX
#  for armedslack_script in /usr/lib/setup/armedslack-postinstall-scripts/* ; do
#     $armedslack_script $T_PX $ROOT_DEVICE
#  done )

# Location of the root file system:
T_PX=$1

# Determine the Hardware Model name:
export HWM=$( slk-hwm-discover )
[ -z "${HWM}" ] && exit 0

# Collapse all 32bit ARM & x86 variants of 'i?86' into a single
# platform: 'x86' and 'arm' respectively.
export ARCH=$( uname -m | sed -e 's%i[0-9]86%x86%g' -e 's?arm.*?arm?g' )

# Location of Hardware Model os-configuration helpers and assets
# within the Slackware Installer:
hwm_osconfigpath=/usr/share/hwm-configure/platform/$ARCH/installer/

# Helper: test for a word on the kernel cmdline. Some helper scripts may use this
# to take an action conditionally, such as knowing which Boot Loader to configure.
function cmdline_has() {
   grep -wq -- "$1" /proc/cmdline 2>/dev/null
}
export -f cmdline_has

# Run any Hardware Model setup scripts found within the Installer for
# this architecture:
[ -d ${hwm_osconfigpath}/helper.scr ] && {
   for platformscr in ${hwm_osconfigpath}/helper.scr/* ; do
      unset hwm # this is used within the scripts for directory names
      # Some of the Hardware Model configuration scripts need to execute
      # under their own process rather than being sourced into this
      # one. This avoids needing to care about any of the variables
      # set by this script, and enables them to exit with any value.
      # Such scripts contain the token '#LAUNCH_FORK' in the first
      # 3 lines of the script.
      if ( head -n3 $platformscr | grep -qE '^#LAUNCH_FORK' ); then
        chmod 755 $platformscr
        $platformscr "$T_PX"
        else
        . $platformscr
      fi
   done ;}
