#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash
# shellcheck shell=bash
set -Eeuo pipefail

end="\033[0m"

green() {
  green_color="\033[0;32m"
  echo -e "${green_color}${1}${end}"
}

yellow() {
  yellow_color="\033[0;33m"
  echo -e "${yellow_color}${1}${end}"
}

blue() {
  blue_color="\033[0;34m"
  echo -e "${blue_color}${1}${end}"
}

red() {
  red_color="\033[0;31m"
  echo -e "${red_color}${1}${end}"
}

usage() {
  red "Missing input argument $1.\n"
  cat <<EOF
Usage:
    setup.sh <TAR_PATH>

      Arguments
        TAR_PATH   Path to the encrypted tar file with key pair and ownertrust information.

Examples:
    Download setup.sh and run it local './EuAndreh.tar.gpg' key pair:
        curl https://euandre.org/dotfiles/nixos/setup.sh | bash -s ./EuAndreh.tar.gpg
EOF
}

TAR_PATH="${1:-}"

[[ -z "${TAR_PATH}" ]] && {
  usage 'TAR_PATH'
  exit 2
}

import_gpg() {
  yellow "Import GPG data..."
  gpg -o EuAndreh.tar -d "${TAR_PATH}"
  tar -xvf EuAndreh.tar
  gpg --import keys.gpg
  gpg --import-ownertrust trust.txt

  rm "${TAR_PATH}"
  shred trust.txt
  rm trust.txt
  shred keys.gpg
  rm keys.gpg
  shred EuAndreh.tar
  rm EuAndreh.tar
  green "Done."
}

start_nextcloud() {
  yellow "Starting Nextcloud process and leaving it on the background..."
  nextcloud &> /dev/null &
  disown
  green "Done."

  yellow "Waiting for Nextcloud Git Annex repo to become available..."
  while true; do
    read -r -p "Has Nextcloud finished syncing? (Type uppercase sync): " sync
    [ "$sync" = 'SYNC' ] && break
    red "Please try again."
  done
  green "Done."
}

configure_git_annex() {
  yellow "Setting up Git Annex repository..."
  git clone "gcrypt::$HOME/Nextcloud/annex.git" "$HOME/annex/"
  pushd ~/annex/
  read -r -p "What's the description of the local Git Annex repository? " DESCRIPTION
  git config --global user.email "eu@euandre.org"
  git config --global user.name "EuAndreh"
  git annex describe here "${DESCRIPTION}"

  git annex enableremote nextcloud
  git annex enableremote s3
  git annex enableremote hd
  # gitlab
  # rsyncnet

  git annex upgrade
  git annex sync
  popd
  green "Done."
}

configure_dotfiles() {
  yellow "Setting up temporary SSH configuration and dotfiles repository..."
  mkdir -p ~/.ssh
  cp ~/annex/secret/SSH/id_rsa.pub.txt ~/.ssh/id_rsa.pub
  cp ~/annex/secret/SSH/id_rsa.txt ~/.ssh/id_rsa
  chmod 600 ~/.ssh/id_rsa
  blue "Open up the password manager to decrypt the SSH private key"
  read -p "A prompt for the SSH password will appear! (Press any key to continue)" -n 1 -r

  export SSH_ENV="$HOME/.ssh/environment"
  echo "Initialising new SSH agent..."
  ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
  echo succeeded
  chmod 600 "${SSH_ENV}"
  # shellcheck disable=1090
  . "${SSH_ENV}" > /dev/null
  ssh-add

  mkdir -p ~/dev/libre/
  git clone git.euandreh.xyz/dotfiles ~/dev/libre/dotfiles/
  pushd ~/dev/libre/dotfiles/
  git crypt unlock
  popd
  pushd ~/
  # shellcheck disable=1090
  source ~/dev/libre/dotfiles/bash/bashrc.sh
  popd

  read -p "A prompt for the sudo password password will appear for the NixOS symlinks! (Press any key to continue)" -n 1 -r
  sudo ln -fs ~/dev/libre/dotfiles/nixos/configuration.nix /etc/nixos/configuration.nix
  # sudo create symlink for /etc/nixos/configuration.nix and /etc/nixos/local-configuration.nix
  green "Done."
}

restore_backups() {
  echo rsync
  echo borg serve

  # restore mbsync from backup
  # restore notmuch from backup
  # restore newsboat from backup
  # restore archive from backup
}

import_gpg
start_nextcloud
setup_git_annex
setup_dotfiles
restore_backups

green "All done!"
