#!/bin/bash
#
# Updates the system settings to my preferred state.
#
# This should be run under sudo so it can properly detect the Spotlight
# configuration store.

# Load JXL. See "Loading JXL" in dots/README.md.
source "$(cd "$(dirname "$0")" && pwd -P)/../dots/all-os/flat/dotlib/jxl-lib.sh" \
    || { echo >&2 "ERROR: failed to load jxl-lib.sh"; exit 1; }

# errexit on, per jxl::init_script's default: aborting a sudo with Ctrl-C should take
# the whole script with it.
jxl::init_script
jxl::use_short_names

OPT_TRACE=0

function usage() {
  cat <<EOF
$PROGRAM_NAME - Set up system settings according to apjanke's preferences

Usage:
  $PROGRAM_NAME [--trace]
  $PROGRAM_NAME --help

Options:
    -x, --trace     trace program execution
$(jxl::std_options_help)
EOF
}

function main() {

################################################
# Tools and editors
################################################

# Install Xcode CLT
# (needed for system git, at least)
# Disabled for now so user can manually choose whether to install Xcode or CLT
if false; then
  rslt=$(sudo xcode-select --install 2>&1)
  if [[ $? == 0 ]]; then
    echo " "
    echo "Now, complete the Xcode CLT install in that dialog!"
    echo -n "When done, press enter to continue: "
    # shellcheck disable=SC2034
    read -r DUMMY
    echo " "
  elif [[ $rslt == *"already installed"* ]]; then
    echo "Looks like Xcode CLT is already installed."
  else
    echo "ERROR: Failed installing Xcode CLT: $rslt"
    exit 1
  fi
fi

# Only /usr/local exists by default
# I might want to link in some binaries, so make sure bin/ exists, too

# TODO: These are outdated now that Homebrew has changed its installation location.

# These sudos aren't necessary or desirable if Homebrew is being installed.
# But I'm not going to assume that yet.
# Just run the Homebrew install script after this one and it will be fine.

if [[ ! -e /usr/local/bin ]]; then
  sudo mkdir -p -v /usr/local/bin
fi

# Link Sublime Text command line tool
if [[ -e /usr/local/bin/subl ]]; then
  echo "subl is already linked."
else
  if [[ -e "/Applications/Sublime Text 3.app" ]]; then
    sudo ln -s -v "/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
  elif [[ -e "/Applications/Sublime Text 2.app" ]]; then
    sudo ln -s -v "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
  fi
fi

################################################
# System-wide behaviors
################################################

# Set the time zone; see `sudo systemsetup -listtimezones` for other values
sudo systemsetup -settimezone "America/New_York" > /dev/null

# Allow remote login
sudo systemsetup -setremotelogin on

# Power management settings
sudo pmset -b displaysleep 15
sudo pmset -c displaysleep 15
sudo pmset -b sleep 30
sudo pmset -c sleep 0
sudo pmset -b lessbright 0

# Show system info on login screen
sudo defaults write /Library/Preferences/com.apple.loginwindow AdminHostInfo HostName

if [[ -e /.Spotlight-V100/VolumeConfiguration.plist ]]; then

  # Disable Spotlight indexing for any volume that gets mounted and has not yet
  # been indexed before.
  # Use `sudo mdutil -i off "/Volumes/foo"` to stop indexing any volume.
  sudo defaults write /.Spotlight-V100/VolumeConfiguration Exclusions -array "/Volumes"

  # Disable Spotlight indexing for any volume that gets mounted and has not yet
  # been indexed before.
  # Use `sudo mdutil -i off "/Volumes/foo"` to stop indexing any volume.
  sudo defaults write /.Spotlight-V100/VolumeConfiguration Exclusions -array "/Volumes"

fi

# Reveal IP address, hostname, OS version, etc. when clicking the clock
# in the login window
sudo defaults write /Library/Preferences/com.apple.loginwindow AdminHostInfo HostName

# Hide Spotlight tray-icon (and subsequent helper)
#sudo chmod 600 /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search

# Load new settings before rebuilding the index
killall mds > /dev/null 2>&1
# Make sure indexing is enabled for the main volume
sudo mdutil -i on / > /dev/null

################################################
# VM Guest specific stuff
################################################

# System settings for when running as a guest inside a VM

if pkgutil --pkg-info com.vmware.tools.macos.pkg.files &>/dev/null; then
  # Enable Retina/HiDPI display modes (requires restart)
  # This may not work well, on a per-program basis
  sudo defaults write /Library/Preferences/com.apple.windowserver DisplayResolutionEnabled -bool true
fi

echo "Done."

}

function parse_cli() {
  local arg

  while [[ $# -ge 1 ]]; do
    arg="$1"; shift
    case "$arg" in
      --trace | -x)                 OPT_TRACE=1 ;;
      *)
        jxl::std_opt "$arg" \
            || die "Unexpected argument: ${arg}. See '$PROGRAM_NAME --help' for usage"
        if [[ $_JXL_WANT_HELP == 1 ]]; then usage; exit 0; fi
        ;;
    esac
  done

  readonly OPT_TRACE
}

parse_cli "$@"

if [[ $OPT_TRACE == 1 ]]; then
  set -x
fi

main
exit 0
