#!/usr/bin/env bash
source ./common.d/functions.sh
source ./common.d/build_functions.sh

require "debootstarp"

export DEBIAN_FRONTEND=noninteractive

fetch_base_system()
{
    local rootdir="$1"
    local branch="$2"
    local components="$3"
    local repo="$4"
    local debootstrap_flags="$5"

    [ ! -d "$rootdir" ] && {
        log "Rootdir [$rootdir] does not exists" ierror
        return 2
    }
    log "Fetching base system" internal
    debootstarp --arch arm64 --components "$components" "$branch" "$rootdir" "$repo" || {
        log "Failed to fetch base system" ierror
    }
}

prepare_system()
{
    local rootdir="$1"
    [ ! -d "$rootdir" ] && {
        log "Rootdir [$rootdir] does not exists" ierror
        return 2
    }
    
    log "Refrashing apt repos" internal
    chroot "$rootdir" apt update || {
        log "Failed to refresh apt repos" ierror
        return 1
    }

    log "Updating system" internal
    chroot "$rootdir" apt upgrade -y || {
        log "Failed to update system" ierror
        return 1
    }
}

setup_zram_generator()
{
    local rootdir="$1"
    [ ! -d "$rootdir" ] && {
        log "Rootdir [$rootdir] does not exists" ierror
        return 2
    }
    
    log "Installing systemd-zram-generator" install
    chroot "$rootdir" apt install systemd-zram-generator -y || {
        log "Failed to install systemd-zram-generator" ierror
        return 1
    }

    log "Configuring zram-generator" internal
    cp ./drop/zram-generator.conf "$rootdir/etc/systemd/zram-generator.conf" || {
        log "Failed to confugire zram-generator" ierror
        return 1
    }
    
    log "Enabling zram-generator" internal
    chroot "$rootdir" systemctl enable systemd-zram-setup@zram0.service || {
        log "Failed to enable zram-generator" ierror
        return 1
    }
}

install_nabu_packages()
{
    local rootdir="$1"
    [ ! -d "$rootdir" ] && {
        log "Rootdir [$rootdir] does not exists" ierror
        return 2
    }
    
    cp ./packages/*.deb "$rootdir/opt/"
    chroot "$rootdir" bash -c "dpkg -i /opt/*.deb" {
        log "Failed to install packages" ierror
        return 1
    }
    chroot "$rootdir" bash -c "rm /opt/*.deb"

    log "Enabling userspace services" internal
    chroot "$rootdir" systemctl enable qrtr-ns pd-mapper tqftpserv rmtfs || {
        log "Failed to enable services" ierror
        return 1
    }
}

install_packages()
{
    local rootdir="$1"
    local packages="${@:2}"
    [ ! -d "$rootdir" ] && {
        log "Rootdir [$rootdir] does not exists" ierror
        return 2
    }

    chroot "$rootdir" apt install ${packages[*]} -y || {
        log "Failed to install package(s)" ierror
        return 1
    }
}

finish_system()
{
    local rootdir="$1"
    [ ! -d "$rootdir" ] && {
        log "Rootdir [$rootdir] does not exists" ierror
        return 2
    }
    
    log "Cleaning apt cache" internal
    chroot "$rootdir" apt clean || {
        log "Failed to clean apt cache" ierror
        return 1
    }
    
    log "Updating runtime linked bindings" internal
    chroot "$rootdir" ldconfig || {
        log "Failed to update runtime linker bindings" ierror
        return 1
    }
}