# Build script to compose the Live CD image.
#
# Copyright (c) 2017 MMPG.
# Copyright (c) 2017-2022 Matias Fonzo <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# ISO file name
ISONAME="${ISONAME:-dragora-3.0_$(date +%Y%m%d)-${package_arch}}"

# Perform sanity checks (requirements)

if ! type xorriso > /dev/null
then
    echo "    xorriso(1) was not found in your PATH." 1>&2
    echo "GNU Xorriso is required to produce the ISO image(s)." 1>&2
    exit 2;
fi

if ! test -e "${rootdir}/squashfs-tools_lzip/mksquashfs"
then
    echo "Local \`${rootdir}/squashfs-tools_lzip/mksquashfs' does not exist." 1>&2
    exit 4;
fi

# This depends on the existence of the stage 1

if test ! -d "${output}/stage1/usr/pkg"
then
    echo "The stage number 1 does not exist or is incomplete." 1>&2
    echo "Build (completely) the stage 1 before processing '$stage'." 1>&2
    exit 1;
fi
if test ! -f "${rootdir}/initrd/initrd.img"
then
    echo "${rootdir}/initrd/initrd.img: No such initramfs file" 1>&2
    exit 4;
fi

STAGE1="${output}/stage1"
CDROM="${rootdir}/cdrom"

# Re-create CD tree

echo "Re-creating CD tree (${CDROM}) ..."
echo ""
rm -rf -- "$CDROM"
mkdir -p -- "${CDROM}/isolinux"

#
# Copy kernel installation to isolinux/

cd -- "${STAGE1}/boot"

for file in System.map-* config-* vmlinuz-*
do
    cp -p "${STAGE1}/boot/$file" "${CDROM}/isolinux/${file%%-*}"
done
unset file

# Copy local isolinux files and initramfs for ISOLINUX

cp -p "${worktree}"/archive/isolinux/* "${CDROM}/isolinux/"
cp -p "${rootdir}/initrd/initrd.img" "${CDROM}/isolinux/"

# Adjust things on the live side.
#
# Set auto-login:
sed \
 -e 's|c1:123:respawn:/sbin/agetty --noclear tty1 38400 linux|c1:123:respawn:/sbin/agetty --noclear --autologin root --noissue tty1 38400 linux|' \
 -e 's|c2:123:respawn:/sbin/agetty tty2 38400 linux|c2:123:respawn:/sbin/agetty --autologin root --login-pause --noissue tty2 38400 linux|' \
 -e 's|c3:12345:respawn:/sbin/agetty tty3 38400 linux|c3:12345:respawn:/sbin/agetty --autologin root --login-pause --noissue tty3 38400 linux|' \
 -i "${STAGE1}/etc/inittab"

# Add instructions for running the installer from boot menu option

cat << 'EOF' > "${STAGE1}/root/.profile"
#
# .profile - Personal shell-initialization file.
#
# The instructions here could serve to complement
# /etc/profile or to override default (previous)
# values.
#

# Add personal bin directory to the end of path
# and make it known to child process

PATH=${PATH}:${HOME}/bin
export PATH

# Run Dragora setup if the "parameter" in the boot was set

#
# Execute setup only on tty1/serial console

case "$(fgconsole)" in
1* | serial* )
    if grep -q -m 1 DRAGORA_SETUP /proc/cmdline
    then
        /sbin/dragora-keymap
        /sbin/dragora-mouse
        /sbin/dragora-installer
    fi
    ;;
esac

EOF

# Produce compressed images using mksquashfs(1)

cd -- "$STAGE1"
rm -f ./dragora.sfs

EXCLUDED_PATHS_COMMON="
 ./tools \
 ./root/.ash_history \
 ./*-log* \
 ./usr/src/qi \
 ./usr/bin/build-for-release
"
EXCLUDED_PATHS_CD="
 $EXCLUDED_PATHS_COMMON \
 ./var/cache/qi/packages \
 ./usr/pkg/kernel-buildtree-* \
 ./usr/src/linux-*
"

# For a DVD live-image with binary packages and kernel sources
echo "Creating DVD ISO image ..."
echo ""

echo "Binding ${CWD}/packages with ${rootdir}/var/cache/qi/packages ..."
mount -o bind "${CWD}/packages" ./var/cache/qi/packages
echo ""

"${rootdir}"/squashfs-tools_lzip/mksquashfs ./* "${CDROM}/dragora.sfs" \
 -comp lzip -b 1024K -Xcompression-level 1 -Xdict-size 100% \
 -noappend -e $EXCLUDED_PATHS_COMMON

echo ""
echo "Unmounting bind-mount ${CWD}/packages ..."
umount ./var/cache/qi/packages

# Produce hybrid ISO-9660 image (DVD)
cd -- "$CDROM"

rm -f ./${ISONAME}-dvd*
xorriso -as mkisofs -verbose \
         -o ${ISONAME}-dvd.iso                                    \
         -J -rational-rock -hide-rr-moved -iso-level 3            \
         -c "isolinux/boot.cat"                                   \
         -b "isolinux/isolinux.bin"                               \
            -partition_offset 16                                  \
            -no-emul-boot -boot-load-size 4 -boot-info-table      \
         -isohybrid-mbr "isolinux/isohdpfx.bin"                   \
         -publisher "Dragora GNU/Linux-Libre, https://www.dragora.org" \
         -volid "Dragora_${package_arch}"                         \
         .

echo "Creating .sha256 sum ..."
echo "sha256sum ${ISONAME}-dvd.iso > ${ISONAME}-dvd.iso.sha256"
sha256sum ${ISONAME}-dvd.iso > ${ISONAME}-dvd.iso.sha256

# ^ End Of 'For a DVD live-image with binary packages and kernel sources'

echo "Finishing ..."
echo ""
echo "***"
echo "The DVD ISO image has been produced at"
echo "    ${CDROM}/${ISONAME}-dvd.iso"
echo "***"
echo ""

exit 0

