#!/usr/bin/env bash

require "wget"

get_image_path()
{
    printf "%s" "$(realpath "./raw/${1}.img")"
}

setup_inet()
{
    local rootdir="$1"
    [ ! -d "$rootdir" ] && {
        log "Rootdir [$rootdir] does not exists" ierror
        return 2
    }
    [ -L "$rootdir/etc/resolv.conf" ] && {
        mv "$rootdir/etc/resolv.conf" "$rootdir/etc/resolv.conf.1"
    }
    echo "nameserver 1.1.1.1" > "$rootdir/etc/resolv.conf"
    echo "xiaomi-nabu" > "$rootdir/etc/hostname"
    echo "127.0.0.1 localhost
    127.0.1.1 xiaomi-nabu" > "$rootdir/etc/hosts"
    return 0
}

create_image()
{
    name="$(get_image_path "$1")"
    if [ -z "$2" ]; then
        size="$DEFAULT_IMAGE_SIZE"
    else
        size="${2}GB"
    fi
    
    if [ -f "$name" ]; then
        rm "$name"
    fi

    truncate -s "$size" "$name" || {
        log "Failed to cretae image [$name]" ierror
        return 1
    }
    mkfs.ext4 "$name"
}

trim_image()
{
    {
        name="$1"
        e2fsck -f "$name"
        resize2fs -M "$name"
    } || {
        log "Failed to trim image" ierror
        return 1
    }
    return 0
}

# shellcheck disable=SC2155
mount_image()
{
    local mountdir="$(mktemp --tmpdir=./tmp/ -d)"
    local mountdir="$(realpath "$mountdir")"
    
    mount -o loop "$1" "$mountdir" || {
        log "Failed to mount image" ierror
        return 1
    }
    
    mkdir -p "$mountdir/boot/efi"
    mkdir -p "$mountdir/boot/simpleinit"
    
    mount -o size=512M,mode=0755 -t tmpfs nabu_esp "$mountdir/boot/efi"
    mount -o size=512M,mode=0755 -t tmpfs nabu_simpleinit "$mountdir/boot/simpleinit"
    
    sed "s|{cmdline}|$SIMPLEINIT_CMDLINE|g" < ./drop/simpleinit.uefi.cfg > "$mountdir/boot/simpleinit/simpleinit.uefi.cfg"
    printf "%s" "$mountdir"
    return 0
}

gen_fstab()
{
    local rootdir="$1"
    [ ! -d "$rootdir" ] && {
        log "Rootdir [$rootdir] does not exists" ierror
        return 2
    }
    
    if [ -d "$rootdir/etc/" ]; then
        [ -f "$rootdir/etc/fstab" ] && rm "$rootdir/etc/fstab"
        cp ./drop/fstab "$rootdir/etc/fstab"
    fi
    return 0
}

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

    rootdir="$(realpath "${1}")"
    mkdir "$rootdir/opt/nabu/" -p
    tar -cf "$rootdir/opt/nabu/efi.tar" -C "$rootdir/" boot/
    cp ./drop/postinstall "$rootdir/opt/nabu/postinstall"
    chmod +x "$rootdir/opt/nabu/postinstall"

    umount "$rootdir/boot/efi"
    umount "$rootdir/boot/simpleinit"
    umount "$rootdir"
    rm -d "$rootdir"
    return 0
}

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

    export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:\$PATH
    rootdir="$(realpath "$1")"

    mount -t proc proc "$rootdir/proc"
    mount -t sysfs sysfs "$rootdir/sys"
    mount -t devtmpfs devtmpfs "$rootdir/dev"
    mount -t devpts devpts "$rootdir/dev/pts"
    mount -t tmpfs devshm "$rootdir/dev/shm"
    return 0
}

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

    rootdir=$(realpath "$1")
    killall gpg-agent > /dev/null 2>&1
    umount "$rootdir/proc" > /dev/null 2>&1 
    umount "$rootdir/sys" > /dev/null 2>&1
    umount "$rootdir/dev/pts" > /dev/null 2>&1
    umount "$rootdir/dev/shm" > /dev/null 2>&1
    umount "$rootdir/dev" > /dev/null 2>&1
    return 0
}