#!/bin/bash
###################################################################################
# File.......: /usr/lib/setup/armedslack-postinstall-scripts/removeinstaller
# Called from: /usr/lib/setup/armedslack-run-postinstall-scripts
#              (sourced via SeTconfig)
# Purpose....: Delete the installer image from /boot.
# Version....: 1.00
# Date.......: 03-May-2021
# Author.....: Stuart Winter <mozes@slackware.com>
###################################################################################
#
# Copyright 2021-2026  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 )

# Ask the user if they would like to delete the Slackware installer initrd from /boot.
# This saves space, and unless you plan on reinstalling immediately afterwards
# (which does happen if the installation gets into an unstable state), it makes sense
# to remove it.
#
# Some users may think it's wise to keep the installer as a rescue
# environment, but that won't work for long if you apply the updates to
# your system.  This is because the installer contains the Kernel modules
# for the kernel within /boot, so the versions need to be aligned.
# It's generally only useful to retain the installer image if you know that
# the installation didn't work properly.

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

# The U-Boot configuration file for the 'sysboot' feature.
# This is contained within /boot.
ARMEDSLACK_EXTLINUXCONF=extlinux/extlinux.conf
# This was for experimentation with the extlinux 'INCLUDE' directive
#ARMEDSLACKINS_EXTLINUXCONF=extlinux/slkinstaller

# If the extlinux config isn't present, exit silently.  On ARM/AArch64 I'd consider
# this a failure, but some users create their own path which causes the removal
# operation to fail without any error throw back.
# In this case though it's not a critical error if we cannot remove the Installer
# image or boot stanza, so we'll exit silently.
# Note: we used to have this here as an initial sanity test, but since the
# integration of the RPi Native BL, it'd become more complex.
# Left here as an example for sanity testing.
#[ ! -f ${T_PX}/boot/${ARMEDSLACK_EXTLINUXCONF} ] && exit 0

# Note: it becomes more complex when we have to also handle removal of the RPi
#       Installer from its VFAT hardware bootware partition, so we'll no longer
#       offer an option not to remove the Installer.
#       If for some reason you want to not do this, figure out the logic and
#       mail me a patch.
#
# Only provide the option to retain the Installer image if a flag is present
# within the Installer, otherwise delete it.  By default this flag is absent, so
# the Installer will be removed:
#if [ -f /.offer-installer-remove ]; then
#   dialog \
#      --backtitle "/boot partition management" \
#      --title "REMOVE SLACKWARE INSTALLER IMAGE FROM SD CARD" --yesno \
#"\nThe SD card houses the (newly installed) Operating System's /boot directory.\n\n
#By default it includes the Slackware installer that you're using presently, and is
#surplus to requirements and of limited utility once the installation is complete, and consumes storage space within /boot. \
#\n\n \
#\nWould you like to remove the Slackware installer image from the SD card? (recommendation is 'Yes')" 17 69
# Silently exit if the user doesn't want to remove the Installer.
#[ $? = 1 ] && exit 0
#fi

# Either the user selected 'Yes' in the dialog above, or there was no flag to offer
# the option to retain the installer, so we will begin with the removal process.
#

# Remove the Installer image from the SD card:
rm -f ${T_PX}/boot/initrd-*.img

# Remove the Installer from the Raspberry Pi's native Boot Loader vfat
# partition, if present:
rm -f ${T_PX}/boot/platform/hwm_bw/slk_initrd-armv8.img

# Remove the Installer's U-Boot extlinux configuration file, if present:
if [ -f ${T_PX}/boot/${ARMEDSLACK_EXTLINUXCONF} ]; then
   # This was when using the 'INCLUDE' config directive where we had the Installer
   # configuration self-contained within its own config.
   #rm -f ${T_PX}/boot/$ARMEDSLACKINS_EXTLINUXCONF
   # The default boot option has already been changed to the OS by
   # /usr/lib/setup/armedslack-SeTpartitions.
   # Remove the installer boot configuration stanza from the main config file:
   sed -i '/^##SLKINS/,/^##SLKINS/d' ${T_PX}/boot/${ARMEDSLACK_EXTLINUXCONF}
   #sed -i '/^INCLUDE slkinstaller/d' ${T_PX}/boot/${ARMEDSLACK_EXTLINUXCONF}
   # Remove the starter/sample file that's used to boot the OS.
   # This is removed from this script, as this file is required to generate
   # the OS boot stanza.  If it's removed earlier and the user reinstalls,
   # the OS boot stanza will not be created.
   rm -f ${T_PX}/boot/extlinux/slkos.sample
fi
