#!/usr/bin/env bash

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
    }

    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"
}

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 --size "$size" "$name" || {
        log "Failed to cretae image [$name]" ierror
        return 1
    }
    mkfs.ext4 "$name"
}

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

# shellcheck disable=SC2155
mount_image()
{
    local rootdir="$1"
    [ ! -d "$rootdir" ] && {
        log "Rootdir [$rootdir] does not exists" ierror
        return 2
    }
    local mountdir="$(mktemp --tmpdir=./tmp/ -d)"
    local mountdir="$(realpath "$mountdir")"
    
    mount -o loop "$(get_image_path "$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"
}

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
}

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" > /dev/null 2>&1
    umount "$rootdir/boot/simpleinit" > /dev/null 2>&1
    umount "$rootdir" > /dev/null 2>&1
    rm -d "$rootdir" > /dev/null 2>&1
}

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 --bind /proc "$rootdir/proc" > /dev/null 2>&1
    mount --bind /sys "$rootdir/sys" > /dev/null 2>&1
    mount --bind /dev "$rootdir/dev" > /dev/null 2>&1
    mount --bind /dev/pts "$rootdir/dev/pts" > /dev/null 2>&1

    if uname -m | grep -q aarch64 || [ -f "/proc/sys/fs/binfmt_misc/qemu-aarch64" ]; then
        log "Cancel qemu install for arm64" internal
    else
        wget -N https://github.com/multiarch/qemu-user-static/releases/download/v7.2.0-1/qemu-aarch64-static -O ./cache/qemu-aarch64-static
        install -m755 ./cache/qemu-aarch64-static "$rootdir/"

        # shellcheck disable=SC2028
        echo ':aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/qemu-aarch64-static:' > /proc/sys/fs/binfmt_misc/register
    
        # shellcheck disable=SC2028
        echo ':aarch64ld:M::\x7fELF\x02\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\xb7:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/qemu-aarch64-static:' > /proc/sys/fs/binfmt_misc/register
    
    fi
}

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

    rootdir=$(realpath "$1")
    blocking=$(lsof -t "$rootdir")
    if [ -n "$blocking" ]; then
        kill -9 "$blocking"
    fi
    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" > /dev/null 2>&1

    if uname -m | grep -q aarch64; then
        log "Cancel qemu uninstall for arm64" internal
    else
        if [ -f "/proc/sys/fs/binfmt_misc/aarch64" ]; then
            echo -1 > /proc/sys/fs/binfmt_misc/aarch64
        fi
        if [ -f "/proc/sys/fs/binfmt_misc/aarch64ld" ]; then
            echo -1 > /proc/sys/fs/binfmt_misc/aarch64ld
        fi
        if [ -f "$rootdir/qemu-aarch64-static" ]; then
            rm "$rootdir/qemu-aarch64-static"
        fi
    fi
}