#!/bin/bash

source /usr/share/slackdev/buildkit.sh

#####################################################################################
# Script : installers-bare.build
# Purpose: Launch the various Slackware Installer build scripts to create the "bare"
#          Installers.  These do not contain the Slackware package sets, but are used
#          primarily for testing, since the AiO Installer images are updated
#          infrequently.
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 30-Mar-2025
#####################################################################################
#
# Copyright 2025-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.
#####################################################################################
# Note:
# This script should be run from within ~/platform/aarch64/bootware/src
# on the master build machine for your configured version of Slackware ARM
# (as per the Slackware ARM devkit config file)

# Slackware tree is chosen from the environment configured within the
# 'slackkit' package.

CWD=$PWD

# Note: If this is being called from source/k/build_test_kernels.sh, it works
# because that script copies this entire directory (the one this 'installers-bare.build'
# resides within) to a temporary location, and builds it from there.
# The output of both the 'inst_dskimg.build' and 'inst_iso.build' scripts are
# relative, so it works.

#####################################################################################
######## Build Generic ISO image ####################################################
#####################################################################################

# If '$PKGSTORE_OVERRIDE' exists as a directory, it means we have
# test packages outside of the main tree.
# In 'inst_dskimg.build', we use this, but for the ISO we don't need it -we just
# use 'ROOTDIR_OVERRIDE'.
[ -d "${ROOTDIR_OVERRIDE}" ] && slktreeoverlay="--overlay-slackware-tree ${ROOTDIR_OVERRIDE}"

# The Slackware version is appended to this directory when the ISO build tool is
# called:
OUTDIR_BARE=$CWD/../installer

# This isn't used by this script - to be moved into the AIO builder.
# Build 'AiO' Installer image:
#   --output-dir ${OUTDIR_AIO}
# aio uses pub distributed tree..
#   --slackware-tree ${SLACKPUBTREEDIR}/${SLACKWAREVERSION} \
#OUTDIR_AIO=${SLACKPUBTREEDIR}/platform/$SLKPORTARCH/bootware/installer-aio/

# Build the 'bare' ISO Installer:
# SLACKPUBTREEDIR=/mnt/prisroot/devel/armedslack-distrib/slackwarearm/
# '$SLACKWAREVERSION' is (e.g.) "slackwareaarch64-current", and is set within the
# Slackware ARM devkit configuration file (sourced by 'buildkit.sh').
# Calling example to test override:
# ROOTDIR_OVERRIDE=/tmp/SLKtestbuilds/publish/experimental/ ./installers.build
./inst_iso.build \
   ${slktreeoverlay} \
   --installer-bare \
   --iso-architecture ${SLKPORTARCH} \
   --slackware-release-name ${SLACKWAREVERSION} \
   --slackware-tree ${PORTROOTDIR} \
   --output-dir ${OUTDIR_BARE}/$SLACKWAREVERSION \
   --linux-kernel-image kernels/armv8/Image-armv8 \
   --slackware-installer-image installer/initrd-armv8.img \
   --grub-config-file $CWD/platform/aarch64/generic-efi-grub/assets/grub.cfg \
   --iso-file-name efi_generic.iso \
   --md5sum-iso \
   --image-app-id "Slackware Installer" || { echo "ERROR: Generic ISO build failed" ; exit 3 ;}
#   --no-cleanup \

#####################################################################################
######## Build Installer disk images ################################################
#####################################################################################
# The Installers disk image build script pulls in the settings itself.
# This may be changed to work in the same way as the iso build script. time permitting.
# It already has some command line operators, but the naming convention is different.
# and it doesn't support all of the same options.
./inst_dskimg.build
#####################################################################################
