#! /usr/bin/env bash
set -e

# Settings
PACKAGE_LIST_CMD="brew cask list"
PACKAGE_INSTALL_CMD="brew cask install"

#
# Package list
#

CORE_PKGS=(
  1password
  aerial
  alfred
  appcleaner
  bartender
  betterzip
  betterzipql
  emacs
  firefox
  fluid
  flux
  google-chrome
  gpg-suite
  hammerspoon
  iina
  istat-menus
  iterm2
  karabiner-elements
  logitech-gaming-software
  logitech-options
  mplayerx
  osxfuse
  qlcolorcode
  qlmarkdown
  qlstephen
  resolutionator
  stay
  ubersicht
  vlc
)

WORK_PKGS=(
  atom
  bbedit
  chromedriver
  cyberduck
  dash
  docker-edge
  fork
  github-desktop
  google-cloud-sdk
  insomnia
  java
  kaleidoscope
  licecap
  medis
  minikube
  minishift
  muzzle
  paw
  postico
  postman
  robo-3t
  rowanj-gitx
  sequel-pro
  slack
  stride
  transmit
  vagrant
  virtualbox
  visual-studio-code
)

PERSONAL_PKGS=(
  4k-video-downloader
  adium-beta
  android-file-transfer
  android-platform-tools
  audio-hijack
  autodmg
  bowtie
  calibre
  chicken
  daisydisk
  deluge
  discord
  dropbox
  ethereum-wallet
  filebot
  gog-galaxy
  goofy
  google-photos-backup-and-sync
  hackety-hack
  handbrake
  irccloud
  istumbler
  keybase
  little-snitch
  makemkv
  messenger
  micro-snitch
  mist
  omnigraffle
  openemu
  parallels-desktop
  peakhour
  plex-media-player
  qlimagesize
  qlprettypatch
  qlvideo
  quicklook-csv
  quicklook-json
  quicklookapk
  rclone-browser
  ring
  screenhero
  sixtyforce
  skyfonts
  skype
  spotify
  suspicious-package
  teamviewer
  transmission
  unetbootin
  virtualc64
  viscosity
  vmware-fusion
  vnc-viewer
  wavebox
  webpquicklook
  witgui
  xld
  yakyak
)

#
# Main
#

main() {
  cache_installed_packages

  install_packages "core" "${CORE_PKGS[@]}"

  if [[ " $* " == *" work "* ]]; then
    install_packages "work" "${WORK_PKGS[@]}"
  fi

  if [[ " $* " == *" personal "* ]]; then
    install_packages "personal" "${PERSONAL_PKGS[@]}"
  fi
}

#
# The rest...
#

INSTALLED_PACKAGES=()

package_name() {
  local install_args="$1"
  local pkg

  pkg="$(echo "$install_args" | awk '{ print $1 }')"
  [[ "$pkg" == *'/'* ]] && pkg="$(basename "$pkg")"

  echo "$pkg"
}

cache_installed_packages() {
  if [ ${#INSTALLED_PACKAGES[@]} -eq 0 ]; then
    echo "--> Getting list of installed packages"
    mapfile -t INSTALLED_PACKAGES < <($PACKAGE_LIST_CMD)
  fi
}

is_package_installed() {
  local pkg="$1"
  cache_installed_packages

  for i in "${INSTALLED_PACKAGES[@]}"; do
    [[ "$pkg" == "$i" ]] && return 0
  done
  return 1
}

install_package() {
  $PACKAGE_INSTALL_CMD "$@"
}

install_packages() {
  local group="$1"
  local pkg
  shift 1

  echo "--> Group: ${group}"
  for install_args in "$@"; do
    pkg="$(package_name "$install_args")"

    if is_package_installed "$pkg"; then
      echo "    Exists: ${pkg}"
    else
      echo "    Installing: ${pkg}"
      install_package "$install_args"
    fi
  done
}

main "$@"
