#!/bin/bash
###################################################################################
# File.......: /usr/lib/setup/armedslack-postinstall-scripts/os-initrd-mgr
# Called from: /usr/lib/setup/armedslack-run-postinstall-scripts
#              (sourced via SeTconfig)
# Purpose....: Run os-initrd-mgr to re-incorporate any firmware from within the
#              newly installed Slackware OS.
# Version....: 1.00
# Date.......: 20-Mar-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

# Sanity checks to ensure /proc is mounted:
[ ! -f /proc/device-tree/model ] && exit 0
# We should have this installed from the a/kernel package, but if not we'll exit silently
# since it's not essential that we rebuild the OS InitRD on the supported Hardware Models.
[ ! -x $T_PX/usr/sbin/os-initrd-mgr ] && exit 0
# There should be some firmware present that was installed from the a/kernel-firmware package:
[ ! -d $T_PX/lib/firmware ] && exit 0
# User-controllable flag to prevent this script from running:
# Also, /usr/share/hwm-configure/platform/aarch64/installer/helper.scr/999-rpi-vendor-kernel-transition
# sets this to prevent the RPi Kernel fork's OSInitRD from being synced.
[ -f /.os-initrd-no-fw-sync ] && exit 0

#dialog \
#   --backtitle "Operating System Initial RAM Disk (OS InitRD)" \
#   --title "SYNCHRONIZE OPERATING SYSTEM INITIAL RAM DISK FIRMWARE" --yesno \
#"The generic Operating System Initial RAM Disk contains a minimal set of firmware \
#required to boot the supported Hardware Models.  However, there may be newer or additional firmware \
#available for your hardware, which may be beneficial to include within the Operating System \
#Initial RAM Disk ('OS InitRD').\n\n
#Note: You can use 'os-initrd-mgr(8)' from within the Operating System to syncronize at any time.\n
#\nDo you want to synchronize the firmware?\n
#\nRecommendation is 'Yes'" 16 80
# User chose 'no'? quit
#[ $? = 1 ] && exit 0

dialog --infobox "\nSynchronizing Operating System Initial RAM Disk..." 5 55

# Call the OS InitrRD manager to reincorporate any locally
# held user modifications, to load modules and so on and to syncronise the firmware.
chroot $T_PX /usr/sbin/os-initrd-mgr -q
