#!/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 base-devel htop ranger git screen ncdu neovim pipewire sway swaybg swaylock xorg-server xorg-xwayland chezmoi foot polkit openssh firefox-developer-edition gnome-keyring xdg-utils qpwgraph qt6-wayland man-db udisks2 ttf-cascadia-code ponysay lm_sensors wget fish mosh jq pkgfile reflector blender

# 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 paru-bin bashmount ulauncher localepurge gomuks-bin

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 -

## 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