#!/bin/bash
###################################################################################
# File.......: /usr/lib/setup/armedslack-removeinstaller
# Called from: /usr/lib/setup/SeTconfig
#              This is post package installation, and during the configuration
#              routines.
# Purpose....: Delete the installer image from /boot.
# Version....: 1.00
# Date.......: 03-May-2021
# Author.....: Stuart Winter <mozes@slackware.com>
###################################################################################
#
# Copyright 2021  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 is the code within /usr/lib/setup/SeTconfig:
# Here, we call each script in /var/log/setup. 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
#  if [ -x var/log/setup/$SCRIPT ]; then
#  ./var/log/setup/$SCRIPT $T_PX $ROOT_DEVICE
#  fi

# The U-Boot configuration file for the 'sysboot' feature.
# This is contained within /boot.

# 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.
ARMEDSLACK_EXTLINUXCONF=extlinux/extlinux.conf
# This was for experimentation with the extlinux 'INCLUDE' directive
#ARMEDSLACKINS_EXTLINUXCONF=extlinux/slkinstaller
#
# 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.
dialog \
   --backtitle "Slackware ARM / AArch64 installer" \
   --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

if [ $? = 0 ]; then
   # Remove the installer image from the SD card:
   rm -f ${T_PX}/boot/initrd-*.img
   # Remove the Installer's extlinux configuration file:
   #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 from being included:
   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
