#!/bin/sh
# Never ever curl some remote shit straight to shell, like:
# curl --proto '=https' --tlsv1.2 -sSf https://inex.dev/v/dotfiles/raw/branch/main/dot_scripts/executable_install_base.sh | sh
# seriously, don't do this, it's insecure!

## Pacman
echo "* enable multilib"
sudo sed -z -i -e 's/#\[multilib\]\n#Include = \/etc\/pacman.d\/mirrorlist/\[multilib\]\nInclude = \/etc\/pacman.d\/mirrorlist/' /etc/pacman.conf

# Workaround for keyring nonsense, see https://bugs.archlinux.org/task/75574
echo "* refresh pkg db, update keyring"
sudo pacman -Sy --needed --noconfirm archlinux-keyring
echo "* upgrade installed pkgs"
sudo pacman -Su --needed --noconfirm

echo "* install pkgs"
sudo pacman --needed --noconfirm -S - < .scripts/pacman_packages.txt 

# Fire reflector (should probably call it there directly with custom config)
echo "* run reflector"
sudo systemctl start reflector

# Sync pkgfile db
echo "* sync pkgfile"
sudo pkgfile -u

## Paru (AUR helper)
# Install Paru from AUR if not installed
if pacman -Qs paru > /dev/null ; then
  echo "* paru already installed."
else
  echo "* install paru"
  git clone https://aur.archlinux.org/paru-bin.git
  cd paru-bin/
  makepkg --needed --noconfirm -si
  cd ..
  rm -rf paru-bin/
fi

## AUR Packages
echo "* install AUR pkgs"
paru --needed --noconfirm -Syua - < .scripts/aur_packages.txt

if ! pacman -Qi nvim-packer-git > /dev/null; then
  paru -S --needed --noconfirm nvim-packer-git
fi

echo "* remove orphans"
paru -Qtdq | paru --noconfirm -Rns -

echo "* unlisted packages:"
echo $(comm -23 <(paru -Qqe | sort) <(sort .scripts/pacman_packages.txt .scripts/aur_packages.txt))

## Chezmoi
echo "* init chezmoi"
chezmoi init --apply https://inex.dev/v/dotfiles.git
cd ~/.local/share/chezmoi/
git remote set-url origin git@inex.dev:v/dotfiles.git
cd ~

## SSH
# Generate key if not exist
if [ -f "$HOME/.ssh/id_ed25519.pub" ]; then
    echo "* ssh key already exist"
else 
    echo "* create ssh key"
    ssh-keygen -t ed25519 -f "$HOME/.ssh/id_ed25519" -N ""
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
fi

## Neovim
echo "* install/upgrade nvim pkgs"
nvim -c 'autocmd User PackerComplete quitall' -c 'PackerSync'

## Add user to useful groups
echo "* add user to groups"
sudo usermod -aG wheel,input,audio,video,lp v

## Apply Firefox policies, see https://github.com/mozilla/policy-templates/blob/master/README.md
echo "* set firefox policies"
sudo mkdir -p /etc/firefox/policies
sudo cp ~/.scripts/firefox_policies.json /etc/firefox/policies/policies.json

## Fix NTFS Mount, see https://wiki.archlinux.org/title/NTFS#udisks_support
echo "* set mount options"
sudo cp ~/.scripts/mount_options.conf /etc/udisks2/mount_options.conf


