#! /usr/bin/env bash

# Shows the output of every command
set +x

pre_home() {
  echo "Creating config / cache directories..."

  # Polybar logs
  mkdir -p $HOME/.config/polybar/logs
  touch $HOME/.config/polybar/logs/bottom.log
  touch $HOME/.config/polybar/logs/top.log

  # FZF cache
  mkdir -p $HOME/.cache/fzf-hoogle
  touch $HOME/.cache/fzf-hoogle/cache.json

  # Desktop pic
  mkdir -p $HOME/Pictures/
  cp imgs/nixos.png $HOME/Pictures/
}

post_home() {
  # Set user's profile picture for Gnome3
  echo "Setting profile picture for the accounts service"
  sudo cp imgs/gvolpe.png /var/lib/AccountsService/icons/gvolpe
  sudo echo "Icon=/var/lib/AccountsService/icons/gvolpe" >> /var/lib/AccountsService/users/gvolpe

  # Set screenlock wallpaper
  echo "Setting screen-lock wallpaper"
  multilockscreen -u imgs/nixos.png
}

build_ci_home() {
  nix build .#homeConfigurations.gvolpe-edp.activationPackage
}

build_ci_system() {
  nix build .#nixosConfigurations.dell-xps.config.system.build.toplevel
  nix build .#nixosConfigurations.tongfang-amd.config.system.build.toplevel
}

fresh_install() {
  nix-shell -p cachix --command '
    cachix use gvolpe-nixos
    ./switch system
    ./build pre-home
    ./switch home
    ./build post-home
  '
}

case $1 in
  "ci-home")
    build_ci_home;;
  "ci-system")
    build_ci_system;;
  "fresh-install")
    fresh_install;;
  "pre-home")
    pre_home;;
  "post-home")
    post_home;;
  *)
    echo "Invalid option. Expected 'ci-home', 'ci-system', 'pre_home', 'post_home' or 'fresh-install'";;
esac
