#!/usr/bin/env bash

# shellcheck disable=SC2312

set -e

usage() {
  cat <<EOF
$(basename "$0")
  a simple script to toggle LittleSnitch profile and update macOS

commands:
  [c]heck       - Switch LS to update mode and list macOS updates.
  [i]nstall     - Switch LS to update mode and install macOS updates.
  [t]oggle      - Toggle LS to disabled mode.
  [x]code       - Install xcode-commandline-tools.
  [h]elp        - Display this message.
EOF
}

checks_osx() {
  sudo littlesnitch profile -a "Apple Update"
  xprotect status
  xprotect version
  sudo xprotect check
  sudo xprotect update
  sudo softwareupdate --list
}

install_osx() {
  sudo littlesnitch profile -a "Apple Update"
  sudo softwareupdate --list
  sudo softwareupdate --install --req
}

install_xcodecmdline() {
  xcode-select --install
}

toggle_ls() {
  sudo littlesnitch profile -a "Apple Disabled"
}

case "$1" in
  c | check)
    checks_osx
    ;;
  i | install)
    install_osx
    ;;
  t | toggle)
    toggle_ls
    ;;
  x | xcode)
    install_xcodecmdline
    ;;
  h | help)
    usage
    exit 0
    ;;
  -h | --help)
    usage
    exit 0
    ;;
  *)
    usage
    exit 1
    ;;
esac
