#!/bin/sh
set -e
alias log='echo [post_install.sh]'


ROOT=/mnt/archinstall
export HOME=/home/user


log "clearing caches"
    rm -fr "$ROOT/var/cache/"


log "altering linux confs"
    cp content/sudoers       "$ROOT/etc/sudoers"
    cp content/pacman.conf   "$ROOT/etc/pacman.conf"
    cp content/lightdm.conf  "$ROOT/etc/lightdm/lightdm.conf"
    cp content/loader.conf   "$ROOT/boot/loader/loader.conf"


log "configuring network"
    mkdir -p                 "$ROOT/etc/systemd/network"
    cp content/wired.network "$ROOT/etc/systemd/network/20-wired.network"
    echo "
        sudo systemctl enable systemd-networkd.service
        sudo systemctl enable systemd-resolved.service
    " | arch-chroot -u user "$ROOT"


log "installing virtualbox guest additions"
    mkdir            "$ROOT/mnt/cdrom"
    mount "/dev/sr1" "$ROOT/mnt/cdrom"
    echo "
        cd /mnt/cdrom
        echo yes | sudo ./VBoxLinuxAdditions.run
    " | arch-chroot -u user "$ROOT" || true # will fail to reload modules since running in a chroot, but installation still successful


log "installing paru"
    echo "
        cd /tmp
        git clone https://aur.archlinux.org/paru-bin.git
        cd paru-bin
        makepkg -si
    " | arch-chroot -u user "$ROOT"
    cp content/paru.conf "$ROOT/etc/paru.conf"


log "installing extra packages"
    echo "
        paru -Sy --noconfirm tealdeer nnn-nerd ttf-ms-fonts discord visual-studio-code-bin opam ttf-firacode-nerd xviewer
    " | arch-chroot -u user "$ROOT"


log "clearing caches"
    echo '
        paru -cca --noconfirm
    ' | arch-chroot -u user "$ROOT"
    rm -fr "$ROOT/home/user/.cache"
    rm -fr "$ROOT/var/cache/"


log "configuring extra packages"
    echo '
        sudo systemctl enable lightdm.service
        sudo groupadd autologin
        sudo usermod --append --groups=autologin user
        opam init --yes
        opam install --yes ocaml-lsp-server dune
    ' | arch-chroot -u user "$ROOT"


log "configuring home"
    echo '
        cd
        sudo chsh --shell=/bin/zsh user
        true | sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
    ' | arch-chroot -u user "$ROOT"

    cp -r content/home/* content/home/.* "$ROOT/home/user/"
    echo '
        cd /home/user/
        sudo chown -R user:user .
    ' | arch-chroot -u user "$ROOT"

    echo '
        git clone https://github.com/zsh-users/zsh-autosuggestions         ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
        git clone https://github.com/zsh-users/zsh-completions             ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions
        git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
    ' | arch-chroot -u user "$ROOT" zsh

    echo '
        for ext in eamodio.gitlens Equinusocio.vsc-community-material-theme Equinusocio.vsc-material-theme equinusocio.vsc-material-theme-icons ms-dotnettools.csharp ms-python.isort ms-python.python ms-python.vscode-pylance ms-vscode.cpptools ocamllabs.ocaml-platform rust-lang.rust-analyzer usernamehw.errorlens VisualStudioExptTeam.intellicode-api-usage-examples VisualStudioExptTeam.vscodeintellicode
        do code --install-extension $ext
        done
    ' | arch-chroot -u user "$ROOT"


log "optimizing drive"
    umount "$ROOT/mnt/crdom"
    umount "$ROOT/boot"
    umount "$ROOT"
    zerofree -v /dev/sda2 # note : sda2 is the 20Gb drive, sda1 is the boot partition
