#!/usr/bin/env bash

# Setups an Arch Linux system the way I like it
# (requires sudo)

if ! which pacman &> /dev/null; then
    echo "This is not an Arch Linux system (or pacman isn't installed)"
    exit 1
fi

# Configuration
function prompt { # text
    while true; do
        read -p "$1 [yn] " yn
        case $yn in
            [Yy]* ) return 1;;
            [Nn]* ) return 0;;
            * ) echo "Please answer yes or no.";;
        esac
    done
}

# Don't ask for things that are already there
if which pacaur &> /dev/null; then
    PACAUR=1
fi
if which bauerbill &> /dev/null; then
    BAUERBILL=1
fi

if [ -z $PACAUR ]; then
    prompt "Do you want pacaur on this machine?"
    PACAUR=$?
fi
if [ $PACAUR == 1 ]; then
    if [ -z $BAUERBILL ]; then
        prompt "Do you want bauerbill on this machine?"
        BAUERBILL=$?
    fi
else
    BAUERBILL=0
fi

# COMMON

# Install packages if they aren't installed
function inst {
    for pkg in $*; do
        pacman -Q $pkg &> /dev/null
        if [ $? == 1 ]; then
            sudo pacman -S $pkg --noconfirm
        fi
    done
}

# Install package from PKGBUILD file
function installPKGBUILD { # url
    # TODO Download snapshots
    TMP_DIR="$(mktemp -d /tmp/pkgbuild.XXXXXXXXXX)"
    cd "$TMP_DIR"
    wget "$1" -O PKGBUILD
    makepkg -si --noconfirm
    cd -
    rm -rf "$TMP_DIR"
}

# SYSTEM
inst wget

# Aur

pacman -Q pacaur &> /dev/null
if [[ $PACAUR == 1 && $? == 1 ]]; then
    installPKGBUILD "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=cower"
    installPKGBUILD "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=pacaur"
fi

# Git for /etc
sudo pacman -S etckeeper --noconfirm --needed
sudo etckeeper init
sudo etckeeper commit "~/.dotfiles/scripts/install-arch commit"

# Uninstall Manjaro's pamac
if pacman -Q pamac &> /dev/null ; then
    sudo pacman -Rsc pamac --noconfirm
fi

# Ccache
sudo pacman -S ccache --noconfirm --needed
sudo sed 's|BUILDENV=\(.\+\)!ccache\(.\+\)|BUILDENV=\1ccache\2|' /etc/makepkg.conf -i

# Bauerbill
pacman -Q bauerbill &> /dev/null
if [[ $BAUERBILL == 1 && $? == 1 ]]; then
    sudo pacman -Sy

    gpg --recv-keys 1D1F0DC78F173680
    installPKGBUILD http://xyne.archlinux.ca/projects/reflector/pkgbuild/PKGBUILD
    pacaur -S bauerbill --noconfirm --noedit

    bauerbill -Su --noconfirm
else
    sudo pacman -Syu
fi

# TLP
sudo pacman -S tlp --noconfirm --needed
sudo sed 's|SATA_LINKPWR_ON_BAT=min_power|SATA_LINKPWR_ON_BAT=max_performance|' /etc/default/tlp -i
sudo systemctl enable tlp.service tlp-sleep.service
sudo systemctl disable systemd-rfkill.service systemd-rfkill.socket
sudo tlp start

# Numlock on boot
echo -e "[Service]\nExecStartPre=/bin/sh -c 'setleds +num < /dev/%I'" | sudo systemctl edit getty\@.service

# Makeflags
sudo sed "s|#MAKEFLAGS=\"-j2\"|MAKEFLAGS=\"-j$(nproc)\"|" /etc/makepkg.conf -i

# Time synchronisation
sudo pacman -S ntp --noconfirm --needed
sudo systemctl start ntpd
sudo systemctl enable ntpd

# MANUAL

# Hibernation
# Add resume=UUID=<UUID-of-swap-partition> to GRUB_CMDLINE_LINUX_DEFAULT and run `sudo grub-mkconfig -o /boot/grub/grub.cfg`