#!/bin/env fish
begin
    set -l dir (status dirname)
    set -l autologinConfig /etc/systemd/system/getty@tty1.service.d/autologin.conf
    source "$dir/../lib/dependencies.fish"
    source "$dir/../../Common/OS/setup.fish"
    source "$dir/../../lib/settings.fish"

    function runChroot -S
        arch-chroot $argv
    end

    function bootstrapSetup -S
        pacstrap -K (getOSConfig fileSystems.rootDir) fish
    end

    function installDrivers -S
        if isOSEnabled hardware.surfaceBook
            pacstrap -K (getOSConfig fileSystems.rootDir) linux-firmware-marvell
        end
    end

    function getInstallerScript -V dir -S
        echo "$dir/install.fish"
    end

    function initOS -V dir
        source "$dir/../lib/dependencies.fish"
        source "$dir/../../lib/wait-network.fish"
        set -l mountDir (getOSConfig fileSystems.rootDir)

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

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

        installValhallaDeps "$mountDir"
    end

    function setupOS -S -V dir -S
        source "$dir/../../lib/hooks.fish"
        source "$dir/../../lib/wait-network.fish"
        set -l mountDir (getOSConfig fileSystems.rootDir)

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

            if set -l keyMap (getOSConfig keyMap)
                loadkeys "$keyMap"
            end

            and if set -l timezone (getOSConfig timeZone)
                timedatectl set-timezone "$timezone"
            end

            and genfstab -U "$mountDir" >>"$mountDir/etc/fstab"

            and if isProgramEnabled "networkmanager"
                and pacstrap -K "$mountDir" networkmanager
                arch-chroot "$mountDir" systemctl enable NetworkManager
            end

            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
                getOSConfig 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 arch-chroot "$mountDir" locale-gen
            end

            and begin
                getOSConfig 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 (getOSConfig hostname) | arch-chroot "$mountDir" tee /etc/hostname >/dev/null

            and runInOS fish "$tempDir/../Software/systemd-networkd/main.fish"
            and runInOS fish "$tempDir/../Software/btrfs/main.fish"
            and arch-chroot "$mountDir" mkinitcpio -P
            and runInOS fish "$tempDir/../Software/grub/main.fish"

            and if set -l keyLayout (getOSConfig 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 -V autologinConfig
        set -l mountDir (getOSConfig fileSystems.rootDir)
        arch-chroot "$mountDir" mkdir -p (dirname "$autologinConfig")

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

    function getAutologinDisableCommand -V autologinConfig
        echo "rm -rf $(string escape (dirname "$autologinConfig"))"
    end

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

    runSetup
end