#!/bin/env fish
begin
    set -l dir (status dirname)
    set -q CONFIG_MODULE || set -l CONFIG_MODULE "$dir/config.nix"
    source "$dir/../../Common/OS/setup.fish"
    source "$dir/../../Common/Scripts/config.fish"

    set -l mountDir (getConfig valhalla.partition.rootDir)

    function runChroot -S
        arch-chroot $argv
    end

    function installValhallaDeps -V mountDir
        pacstrap -K "$mountDir" fish jq nix tmux
    end

    function setupOS -S -V dir -V CONFIG_MODULE -V mountDir
        source "$dir/../../Common/Scripts/hooks.fish"
        source "$dir/../../Common/Scripts/wait-network.fish"

        set -q USER_NAME || set -l USER_NAME manuel
        set -q USER_DISPLAYNAME
        set -q USER_GROUPS || set -l USER_GROUPS ""

        waitNetwork
        and begin
            set -l relativeDir (realpath --relative-to "$dir/../../.." "$dir")
            set -l tempDir "$PROJECT_CLONE_ROOT/$relativeDir"

            if set -l keyMap (getConfig valhalla.keyMap)
                loadkeys "$keyMap"
            end

            and if set -l timezone (getConfig valhalla.timeZone)
                timedatectl set-timezone "$timezone"
            end

            and pacman-key --init
            and pacman-key --populate

            and pacstrap -K "$mountDir" \
                base \
                linux \
                linux-firmware \
                networkmanager \
                man-db \
                man-pages \
                texinfo

            and genfstab -U "$mountDir" >> "$mountDir/etc/fstab"
            and arch-chroot "$mountDir" systemctl enable NetworkManager

            and if set -q timezone
                arch-chroot "$mountDir" ln -sf "/usr/share/zoneinfo/$timezone" /etc/localtime
            end

            and arch-chroot "$mountDir" hwclock --systohc

            and begin
                getConfig valhalla.i18n.localeSettings --json | \
                    jq --raw-output '[.[] | split(".") | .[0]] | unique | join("\\\\|")'
            end | begin
                read LOCALES
                and arch-chroot "$mountDir" sed -i "s/^#\?\(\($LOCALES\).*\)\$/\1/" /etc/locale.gen
                and locale-gen
            end

            and begin
                getConfig valhalla.i18n.localeSettings --json | \
                    jq --raw-output '[keys[] as $key | "\($key)=\(.[$key])"] | join("\n")'
            end | arch-chroot "$mountDir" tee /etc/locale.conf > /dev/null

            and if set -q keyMap
                echo "KEYMAP=$keyMap" | arch-chroot "$mountDir" tee /etc/vconsole.conf > /dev/null
            end

            and echo "$ARCH_HOSTNAME" | arch-chroot "$mountDir" tee /etc/hostname > /dev/null

            and arch-chroot "$mountDir" mkinitcpio -P
            and runHook installDrivers "Installing drivers..." || true
            and runInOS fish "$tempDir/../../Common/Software/nix/main.fish" configure user
            and arch-chroot "$mountDir" bash "$tempDir/../Software/sudo/install.sh"
            and runInOS fish "$tempDir/../Software/GRUB/main.fish"

            and if set -l keyLayout (getConfig valhalla.keyboardLayout)
                set -l serviceName set-keymap.service
                and set -l serviceFile "$mountDir/etc/systemd/system/$serviceName"
                and cp "$dir/$serviceName" "$serviceFile"
                and systemd-nspawn -D "$mountDir" systemctl enable "$serviceName"
                and systemd-nspawn -bD "$mountDir" -E "ARCH_X11_KEYMAP=$keyLayout"
                and systemd-nspawn -D "$mountDir" systemctl disable "$serviceName"
                and rm "$serviceFile"
            end
        end
    end

    function autologin -S
        set -l file "/etc/systemd/system/getty@tty1.service.d/autologin.conf"
        arch-chroot "$mountDir" mkdir -p (dirname "$file")

        begin
            printf %s\n \
                "[Service]" \
                "ExecStart=" \
                "ExecStart=-/sbin/agetty -o '-p -f -- \\u' --noclear --autologin root %I \$TERM"
        end | arch-chroot "$mountDir" tee "$file" > /dev/null
    end

    if not type -q getInstallerScript
        function getInstallerScript -S
            echo "$dir/install.fish"
        end
    end

    runSetup
end