#!/bin/bash

## flyingscorpio
## Script to automate the setting up of my working environment.
## Uncomment the function calls in MAIN you wish to run.


# 1. Set up {{{

RED=$(tput setaf 1)
export RED
GREEN=$(tput setaf 2)
export GREEN
ORANGE=$(tput setaf 3)
export ORANGE
NC=$(tput sgr0)
export NC

BASE_DIR="$PWD"
export BASE_DIR

GITHUB_DOTFILES=~/github_dotfiles
export GITHUB_DOTFILES

make_symlink() {

  link=$1
  target=$2

  echo -n "Checking symlink $link..."
  if [ "$link" -ef "$target" ]; then
    echo "${GREEN} ==> OK${NC}"
  else
    echo "${RED} ==> wrong symlink or not a symlink.${NC}"
    if [ "$OVERWRITE" = 'true' ]; then
      rm "$link" 2> /dev/null
    fi
    if ln -rs "$target" "$link"; then
      echo "${ORANGE}$link symlink created${NC}"
    fi
  fi

}
export -f make_symlink

separator() {
  exit_status=$?
  if [ $exit_status -eq 0 ]; then
    echo -n "${GREEN}"
  else
    echo -n "${RED}"
  fi
  echo "========================================"
  echo "${NC}"
}

# }}}

# 2. Define OS {{{

OS='none'

os_release=$(cat /etc/os-release*)
case ${os_release,,} in
  *debian* )
    OS='debian'
    echo "You are using Debian"
    ;;
  *ubuntu* )
    OS='debian'
    echo "You are using Ubuntu"
    ;;
  *arch* )
    OS='arch'
    echo "You are using Arch-Linux"
    ;;
  *manjaro* )
    OS='arch'
    echo "You are using Manjaro"
    ;;
  * )
    echo "${ORANGE}Unable to determine OS${NC}"
    echo "Which OS are you using?"
    select OS in 'Debian / Ubuntu' 'Arch / Manjaro'; do
      case $OS in
        'Debian / Ubuntu' ) OS='debian'; break;;
        'Arch / Manjaro' ) OS='arch'; break;;
      esac
    done
    echo "Using $OS"
esac

export OS

separator

# }}}

# 3. Handle args {{{

OVERWRITE='false'
UPDATE='false'

if [ "$#" -eq 0 ]; then
  echo "No arguments provided."
  echo "Use -o to overwrite files."
  echo "Use -u to enable updating."
fi
if [ "$#" -ge 1 ]; then
  case "$1" in
    -o | --overwrite )
      echo "${ORANGE}You agree to overwrite preexisting files!${NC}"
      OVERWRITE='true'
      ;;
    -u | --update )
      echo "${ORANGE}Set to update plugins!${NC}"
      UPDATE='true'
      ;;
    -ou | -uo )
      echo "${ORANGE}Both options were set.${NC}"
      OVERWRITE='true'
      UPDATE='true'
      ;;
    * )
      echo "Din't understand arguments"
      exit 1
  esac
fi
if [ "$#" -eq 2 ]; then
  case "$2" in
    -o | --overwrite )
      echo "${ORANGE}You agree to overwrite preexisting files!${NC}"
      OVERWRITE='true'
      ;;
    -u | --update )
      echo "${ORANGE}Set to update plugins!${NC}"
      UPDATE='true'
      ;;
    * )
      echo "Din't understand arguments"
      exit 1
  esac
fi

export OVERWRITE
export UPDATE

separator

# }}}

# FUNCTIONS:

clone_projects() { # {{{
  ######
  # OK #
  ######

  # Clone projects

  echo "Cloning projects..."
  /bin/bash "$BASE_DIR"/installation_scripts/clone_projects.sh
  separator

} # }}}

configure_git() { # {{{
  ######
  # OK #
  ######

  # Configure Git

  echo "Configuring Git..."
  git config --global user.name "$USER@$(uname -n)"
  git config --global user.email "tfranken@protonmail.com"
  git config --global alias.ci "commit"
  git config --global alias.co "checkout"
  git config --global alias.st "status"
  git config --global alias.br "branch"
  separator

} # }}}

configure_protonvpn() { # {{{
  ######
  # OK #
  ######

  # Configure ProtonVPN

  echo "Configuring ProtonVPN..."
  sudo protonvpn init && sudo protonvpn c --cc CH
  separator

} # }}}

install_rust() { # {{{
  ######
  # OK #
  ######

  # Install Rust

  echo "Installing Rust..."
  if rustc --version; then
    echo "${GREEN}Rust is already intalled${NC}"
    if [ "$UPDATE" = 'true' ]; then
      echo "Updading..."
      rustup update
    fi
  else
    if curl https://sh.rustup.rs -sSf | sh; then
      echo "${GREEN}Successfully installed Rust${NC}"
    else
      echo "${ORANGE}There was a problem while installing Rust${NC}"
    fi
  fi
  separator

} # }}}

install_scripts() { # {{{
  ######
  # OK #
  ######

  echo "Installing scripts..."

  if [ "$OS" = 'debian' ]; then
    /bin/bash "$BASE_DIR"/installation_scripts/debian_based_installs.sh
  elif [ "$OS" = 'arch' ]; then
    /bin/bash "$BASE_DIR"/installation_scripts/arch_based_installs.sh
  else
    echo "${RED} Didn't receive correct OS information, quitting...${NC}"
    exit 1
  fi

  separator

} # }}}

markdown_mimetype() { # {{{
  ######
  # OK #
  ######

  # Add MIME type for reading .md files in Firefox (need to add extension too)

  echo "Adding MIME type for .md files in Firefox..."
  echo "${ORANGE}Remember to install the Firefox extension for it to work${NC}"
  mkdir -p ~/.local/share/mime/packages
  echo "<?xml version=\"1.0\"?>
  <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
    <mime-type type=\"text/plain\">
      <glob pattern=\"*.md\"/>
      <glob pattern=\"*.mkd\"/>
      <glob pattern=\"*.markdown\"/>
    </mime-type>
  </mime-info>" > ~/.local/share/mime/packages/text-markdown.xml
  update-mime-database ~/.local/share/mime
  separator

} # }}}

setup_bash_files() { # {{{
  ######
  # OK #
  ######

  # Set up .bashrc

  echo "Updating .bashrc..."
  make_symlink ~/.bashrc "$BASE_DIR"/dotfiles/bash/bashrc

  separator

  # Set up .bash_aliases

  echo "Updating .bash_aliases..."
  make_symlink ~/.bash_aliases "$BASE_DIR"/dotfiles/bash/bash_aliases

  separator

} # }}}

setup_bash_git_prompt() { # {{{
  ######
  # OK #
  ######

  # Clone bash-git-prompt

  if [ -e ~/.bash-git-prompt ]; then
    echo "${GREEN}Found existing bash-git-prompt.${NC}"
    if [ "$UPDATE" = 'true' ]; then
      echo "Updating..."
      cd ~/.bash-git-prompt && git pull
    fi
  else
    echo "${ORANGE}Cloning bash-git-prompt...${NC}"
    git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt --depth=1
  fi
  separator

} # }}}

setup_i3() { # {{{
  ######
  # OK #
  ######

  # Update i3 configuration

  echo "Setting up i3..."
  /bin/bash "$BASE_DIR"/installation_scripts/i3.sh
  separator

} # }}}

setup_sddm() { # {{{
  ######
  # OK #
  ######

  # Update sddm

  echo "Setting up SDDM..."
  /bin/bash "$BASE_DIR"/installation_scripts/sddm.sh
  separator

} # }}}

setup_vim() { # {{{
  ######
  # OK #
  ######

  # Update vim configuration

  echo "Setting up configuration for Vim..."
  /bin/bash "$BASE_DIR"/installation_scripts/vim.sh
  separator

} # }}}

# MAIN:

# install_scripts
# configure_git
# configure_protonvpn
# setup_bash_files
# setup_bash_git_prompt
# install_rust
# setup_vim
# setup_sddm
# setup_i3
# markdown_mimetype
# clone_projects
