#!/usr/bin/env fish

function main
    log 'Enter your configuration'
    read -P 'Timezone (America/New_York): ' -l timezone
    read -P 'Hostname (archlinux): ' -l new_host_name
    read -P 'Username (m): ' -l username
    read -P 'Password: ' -s -l pass
    read -P 'Confirm: ' -s -l confpass

    while test "$pass" != "$confpass"
        echo 'Passwords did not match!'
        read -P 'Password: ' -s -f pass
        read -P 'Confirm: ' -s -f confpass
    end

    read -P 'Network client (NM/networkd): ' -l network
    read -P 'Boot mode (BIOS/uefi): ' -l boot_mode
    read -P 'Boot target: ' -l device
    #read -P 'Is your disk encrypted? (y/N) ' -l is_encrypt

    read -P 'Continue? (y/N): ' -l ready

    if test "$ready" != y; and test "$ready" != Y
        return
    end

    set -l encrypted_uuid ''
    if test "$is_encrypt" = y; or test "$is_encrypt" = Y
        read -P 'Encrypted device UUID: ' encrypted_uuid
    end

    set_timezone $timezone

    set_locale

    set_hostname $new_host_name

    set_pacman_color

    if test "$network" = networkd
        configure_systemd_networkd
    else
        configure_network_manager
    end

    if test "$is_encrypt" = y; or test "$is_encrypt" = Y
        add_lvm2_mkinitcpio_hook true
    end

    mkinitcpio -P

    configure_sudo

    create_admin_user $username $pass

    install_openssh

    install_bootloader $boot_mode $device $encrypted_uuid

    move_dotfiles $username
end

function install
    pacman --sync --noconfirm $argv
end

function log -a message level
    set message "---------------- $message ----------------"
    echo \n$message\n
end

function set_pacman_color
    log "Enable color for pacman"
    sed -i 's/#Color/Color/' /etc/pacman.conf
end

function set_timezone -a timezone
    if test -z "$timezone"
        set -f timezone America/New_York
    end
    log "Setting timezone to $timezone"
    ln -sf /usr/share/zoneinfo/$timezone /etc/localtime

    log "Syncronising to hardware clock"
    hwclock --systohc
end

function set_locale
    log 'Setting locale to en_US.UTF-8 and generating it'
    sed -i 's/#en_US.UTF-8/en_US.UTF-8/' /etc/locale.gen
    locale-gen
    echo 'LANG=en_US.UTF-8' >/etc/locale.conf
    touch /etc/vconsole.conf
end

function set_hostname -a new_hostname
    if test -z "$new_hostname"
        set -f new_hostname archlinux
    end
    log "Setting hostname to $new_hostname"
    echo $new_hostname >/etc/hostname
end

function configure_systemd_networkd
    log "Configuring systemd-resolved wired interface"
    echo "
[Match]
Name=en*

[Network]
DHCP=yes
IPv6PrivacyExtensions=yes" >/etc/systemd/network/20-wired.network

    log 'Enabling systemd networkd and resolved services'
    systemctl enable systemd-networkd.service
    systemctl enable systemd-resolved.service
    log 'REMINDER: Run "# ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf" after booting.'
end

function configure_network_manager
    log "Configuring NetworkManager wired interface"
    install networkmanager

    log 'Enabling systemd networkd and resolved services'
    systemctl enable NetworkManager.service
    systemctl enable systemd-resolved.service
end

function add_lvm2_mkinitcpio_hook -a is_encrypt
    log 'Adding lvm2 mkinitcpio hook'

    set -l hooks lvm2
    if test -n "$is_encrypt"
        set hooks encrypt $hooks
    end

    sed -i "s/modconf block filesystems/modconf block $hooks filesystems/" /etc/mkinitcpio.conf

    log 'Installing lvm2'
    install lvm2
end

function configure_sudo
    log 'Installing and configuring sudo'
    install sudo sudo-rs

    ln -s /usr/bin/su-rs /usr/local/bin/su
    ln -s /usr/bin/sudo-rs /usr/local/bin/sudo
    ln -s /usr/bin/sudoedit-rs /usr/local/bin/sudoedit
    ln -s /usr/bin/visudo-rs /usr/local/bin/visudo
    sed -i 's/# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /etc/sudoers
    sed -i 's/Defaults!\/usr\/bin\/visudo/Defaults!\/usr\/local\/bin\/visudo/' /etc/sudoers
end

function create_admin_user -a username pass
    if test -z "$username"
        set -f username m
    end
    log "Creating user $username and adding to wheel group"
    useradd -m -G wheel -s /usr/bin/zsh $username
    echo $pass | passwd -s $username
    passwd -l root
end

function install_bootloader -a boot_mode target encrypted_uuid
    if test "$boot_mode" = uefi
        log "Installing systemd-boot with root $target"

        bootctl install
        set -lx partuuid (blkid --match-tag PARTUUID --output value $target)
        echo "title   Arch Linux
linux   /vmlinuz-linux
initrd  /initramfs-linux.img
options root=PARTUUID=$partuuid rootflags=subvol=root rootfstype=btrfs rw quiet" >/boot/loader/entries/arch.conf
    else
        log "Installing grub to target $target"
        install grub
        grub-install --target=i386-pc $target

        if test -n "$encrypted_uuid"
            sed -i "s/loglevel=3 quiet/loglevel=3 quiet cryptdevice=UUID=$encrypted_uuid:cryptlvm/" /etc/default/grub
        end

        grub-mkconfig -o /boot/grub/grub.cfg
    end
end

function install_zram
    log "Installing and configuring zram-generator"
    install zram-generator
    echo '[zram0]' >/etc/systemd/zram-generator.conf
end

function install_openssh
    log 'Installing openssh'
    install openssh

    log 'Enabling sshd service'
    systemctl enable sshd.service
end

function move_dotfiles -a username
    log 'Moving dotfiles'
    if test -z "$username"
        set -f username m
    end
    mkdir /home/$username/code
    mv /dotfiles /home/$username/code/
    chown -R $username: /home/$username
    cd /
end

main
