#!/bin/bash

################################################################################
# Title:        build_pepos.sh
# Description:  Script to build the PeppermintOS Server ISO image
# Author:       PeppermintOS Team <peppermintosteam@proton.me>
# Date:         May 10, 2023
# License:      GPL-3.0-or-later
################################################################################

set -euo pipefail

# Set the working folder variables
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
readonly WORK_DIR="$SCRIPT_DIR/fusato"
readonly CACHE_DIR="$WORK_DIR/cache"
readonly CHROOT_DIR="$WORK_DIR/chroot"

# Creates the working directory and cleans any old files
mkdir -p "$WORK_DIR"
rm -rf "$CHROOT_DIR" "$CACHE_DIR"

# Define the Live Build settings
lb config \
    --architectures amd64 \
    --linux-flavours amd64 \
    --distribution bookworm \
    --archive-areas "main contrib non-free non-free-firmware" \
    --apt-recommends true \
    --security true \
    --updates true \
    --backports true \
    --debian-installer live \
    --debian-installer-distribution bookworm \
    --debian-installer-gui true \
    --parent-mirror-debian-installer http://repo.peppermintos.com/packages \
    --win32-loader false \
    --image-name "PepOS-Server" \
    --iso-volume "PepOS-server" \
    --iso-publisher "Peppermint OS Team" \
    --iso-preparer "PeppermintOS-https://peppermintos.com/" \
    --debian-installer-preseedfile "$SCRIPT_DIR/preseed.cfg" \
    --binary-images iso-hybrid \
    --checksums sha512 \
    --no-auto \
    "${@}"

# Setup the chroot structure
mkdir -p "$CHROOT_DIR/etc/default"
mkdir -p "$CHROOT_DIR/etc/firewalld/zones"
mkdir -p "$CHROOT_DIR/boot/grub"
mkdir -p "$CHROOT_DIR/usr/share"
mkdir -p "$WORK_DIR/config/includes.binary"
mkdir -p "$WORK_DIR/config/includes.chroot"
mkdir -p "$WORK_DIR/config/includes.installer"
mkdir -p "$WORK_DIR/config/hooks/normal"
mkdir -p "$WORK_DIR/config/archives"
mkdir -p "$WORK_DIR/config/config/package-lists"

# Install software
cat << EOF > "$WORK_DIR/config/config/package-lists/package.list.chroot"
zonefstoolspep
dmzonedtoolspep
libzbdpep1
sudo
task-ssh-server
task-web-server
sshguard
htop
whois
rkhunter
debsecan
net-tools
cockpit
cockpit-podman
cockpit-machines
cockpit-pcp
podman
tuned
nfs-common
firewalld
#samba
cups
gvfs-backends
git
wget
EOF


# Copy single files and folder to the chroot
cp "$SCRIPT_DIR/pepgrub/grub" "$WORK_DIR/config/includes.chroot/etc/default"
cp "$SCRIPT_DIR/pephooks/normal/*" "$WORK_DIR/config/hooks/normal"
cp "$SCRIPT_DIR/peprepo/*" "$WORK_DIR/config/config/archives"
cp "$SCRIPT_DIR/pepfirewall/*" "$WORK_DIR/config/includes.chroot/etc/firewalld/zones"
cp "$SCRIPT_DIR/pepinstaller/preseed/preseed.cfg" "$WORK_DIR/config/includes.installer"

# Copy recursive files and sub-directories, containing symlinks.
cp -r "$SCRIPT_DIR/peploadersplash/boot" "$WORK_DIR/config/includes.binary"
cp -r "$SCRIPT_DIR/peploadersplash/isolinux" "$WORK_DIR/config/includes.binary"
cp -r "$SCRIPT_DIR/pepgrub/themes" "$WORK_DIR/config/includes.chroot/boot/grub"
cp -r "$SCRIPT_DIR/pepinstaller/graphics" "$WORK_DIRconfig/includes.installer/usr/share"
cp -r "$SCRIPT_DIR/pepinstaller/themes" "$WORK_DIR/config/includes.installer/usr/share"

# Build the iso
lb build