#!/bin/sh
set -eu

if [ "$(uname -s)" != "Darwin" ]; then
  printf '%s\n' 'Lighthouse Homebrew installation is currently for macOS.' >&2
  exit 1
fi

if ! command -v brew >/dev/null 2>&1; then
  printf '%s\n' 'Homebrew is required. Install it from https://brew.sh/ and run this command again.' >&2
  exit 1
fi

# This script is often piped into sh. Keep Homebrew from reading the remaining script as stdin.
brew install paulwritescode/lighthouse/lighthouse </dev/null

lighthouse_binary="$(brew --prefix </dev/null)/bin/lighthouse"
if [ ! -x "$lighthouse_binary" ]; then
  printf '%s\n' 'Homebrew finished, but the Lighthouse executable was not found.' >&2
  exit 1
fi

"$lighthouse_binary" version
printf '%s\n' 'Run lighthouse to open the first-time setup.'
