#!/bin/bash

set -euo pipefail

cleanup() {
    if [[ -n "${WORKDIR:-}" && -d "${WORKDIR:-}" ]]; then
        rm -rf "${WORKDIR}"
    fi
}

if [[ "$#" -ne 1 ]]; then
    echo "Usage: $0 <path-to-iso>"
    exit 2
fi

ISO=$1
if [[ ! -f "$ISO" ]]; then
    echo "E: ISO file not found: $ISO"
    exit 2
fi

# ensure clean working directory
cleanup

if ! command -v xorriso >/dev/null 2>&1; then
    echo "E: missing dependency: xorriso"
    echo "   Install xorriso (recommended) or run inside the vyos-build container."
    exit 2
fi

if ! command -v unsquashfs >/dev/null 2>&1; then
    echo "E: missing dependency: unsquashfs"
    echo "   Install squashfs-tools or run inside the vyos-build container."
    exit 2
fi

if ! command -v jq >/dev/null 2>&1; then
    echo "E: missing dependency: jq"
    echo "   Install jq or run inside the vyos-build container."
    exit 2
fi

WORKDIR="$(mktemp -d -t iso-to-oci.XXXXXXXXXX)"
trap cleanup EXIT

ROOTFS="${WORKDIR}/iso"
UNSQUASHFS="${WORKDIR}/unsquashfs"
mkdir -p "${ROOTFS}/live" "${UNSQUASHFS}"

echo "I: extracting ISO metadata"
xorriso -osirrox on -indev "${ISO}" -extract /version.json "${ROOTFS}/version.json" >/dev/null 2>&1

echo "I: extracting squashfs image"
xorriso -osirrox on -indev "${ISO}" -extract /live/filesystem.squashfs "${ROOTFS}/live/filesystem.squashfs" >/dev/null 2>&1

# create directory, unpack squashfs filesystem, get ISO version
echo "I: extracting squashfs content"
unsquashfs -follow -dest "${UNSQUASHFS}/" "${ROOTFS}/live/filesystem.squashfs" >/dev/null 2>&1
VERSION="$(jq --raw-output .version "${ROOTFS}/version.json")"

# fix locales for correct system configuration loading
sed -i 's/^LANG=.*$/LANG=C.UTF-8/' "${UNSQUASHFS}/etc/default/locale"

# optional step: Decrease docker image size by deleting not necessary files for container
rm -rf "${UNSQUASHFS}/boot"
rm -rf "${UNSQUASHFS}/lib/firmware/"
rm -rf "${UNSQUASHFS}/usr/lib/x86_64-linux-gnu/libwireshark.so*"
rm -rf "${UNSQUASHFS}/lib/modules/*-vyos"
rm -rf "${UNSQUASHFS}/root/.gnupg"

# delete features not supported in container - only remove the node.def files,
# this is sufficient to not make the feature pop up on the CLI
rm -rf "${UNSQUASHFS}/opt/vyatta/share/vyatta-cfg/templates/container"
rm -rf "${UNSQUASHFS}/opt/vyatta/share/vyatta-cfg/templates/system/console"
rm -rf "${UNSQUASHFS}/opt/vyatta/share/vyatta-cfg/templates/system/option/kernel"
rm -rf "${UNSQUASHFS}/opt/vyatta/share/vyatta-cfg/templates/system/option/startup-beep"
rm -rf "${UNSQUASHFS}/opt/vyatta/share/vyatta-cfg/templates/system/option/root-partition-auto-resize"
rm -rf "${UNSQUASHFS}/opt/vyatta/share/vyatta-cfg/templates/system/option/reboot-on-panic"
rm -rf "${UNSQUASHFS}/opt/vyatta/share/vyatta-cfg/templates/system/option/performance"

# create a symbolic link to the configuration
ln -s /opt/vyatta/etc/config "${UNSQUASHFS}/config"

# create docker image
echo "I: generate OCI container image vyos-$VERSION.tar"
tar -C "${UNSQUASHFS}" -c . -f "vyos-${VERSION}.tar"

echo "I: to import the previously generated OCI image to your local images run:"
echo ""
echo "   docker import vyos-$VERSION.tar vyos:$VERSION --change 'CMD ["/sbin/init"]'"
echo ""

cleanup
