#!/bin/sh set -eu umask 022 REPOSITORY="keynope/keynope" CASK_URL="https://raw.githubusercontent.com/keynope/homebrew-keynope/main/Casks/keynope.rb" install_root="$HOME/.keynope" bin_dir="$install_root/bin" app_dir="$HOME/Applications" say() { printf '%s\n' "$*" } fail() { printf 'keynope installer: %s\n' "$*" >&2 exit 1 } for command_name in curl tar shasum awk sed grep uname sw_vers mktemp install ditto codesign spctl basename; do command -v "$command_name" >/dev/null 2>&1 || fail "required command not found: $command_name" done [ "$(uname -s)" = "Darwin" ] || fail "Keynope currently supports macOS only" macos_major="$(sw_vers -productVersion | awk -F. '{print $1}')" [ "$macos_major" -ge 14 ] 2>/dev/null || fail "macOS 14 Sonoma or later is required" case "$(uname -m)" in arm64) release_arch="arm64" ;; x86_64) release_arch="amd64" ;; *) fail "unsupported Mac architecture: $(uname -m)" ;; esac workdir="$(mktemp -d "${TMPDIR:-/tmp}/keynope-install.XXXXXX")" cleanup() { rm -rf "$workdir" } trap cleanup 0 trap 'exit 1' HUP INT TERM curl_args="--proto =https --tlsv1.2 --fail --silent --show-error --location" # shellcheck disable=SC2086 curl $curl_args "$CASK_URL" --output "$workdir/keynope.rb" version="$(sed -n 's/^[[:space:]]*version "\([^"]*\)".*/\1/p' "$workdir/keynope.rb" | awk 'NR == 1 { print; exit }')" [ -n "$version" ] || fail "could not determine the current Keynope version" asset="keynope-mac-${release_arch}.tar.gz" release_base="https://github.com/${REPOSITORY}/releases/download/${version}" archive="$workdir/$asset" checksums="$workdir/SHA256SUMS" expected_sha="" # shellcheck disable=SC2086 if curl $curl_args "$release_base/SHA256SUMS" --output "$checksums" 2>/dev/null; then expected_sha="$(awk -v asset="$asset" '$2 == asset || $2 == "*" asset { print $1; exit }' "$checksums")" fi [ -n "$expected_sha" ] || fail "could not determine the SHA-256 checksum for $asset" say "Installing Keynope $version for $release_arch..." # shellcheck disable=SC2086 curl $curl_args "$release_base/$asset" --output "$archive" actual_sha="$(shasum -a 256 "$archive" | awk '{print $1}')" [ "$actual_sha" = "$expected_sha" ] || fail "SHA-256 verification failed for $asset" tar -xzf "$archive" -C "$workdir" package="$workdir/keynope-mac-${release_arch}" cli="$package/keynope" helper="$package/KeynopePresenter.app" app="$package/Keynope.app" [ -x "$cli" ] || fail "release archive does not contain the Keynope CLI" [ -x "$helper/Contents/MacOS/KeynopePresenter" ] || fail "release archive does not contain KeynopePresenter.app" [ -x "$app/Contents/MacOS/Keynope" ] || fail "release archive does not contain Keynope.app" codesign --verify --strict "$cli" codesign --verify --deep --strict "$helper" codesign --verify --deep --strict "$app" spctl --assess --type execute "$app" staging_root="$install_root/.install-$$" mkdir -p "$install_root" rm -rf "$staging_root" mkdir -p "$staging_root" install -m 0755 "$cli" "$staging_root/keynope" ditto "$helper" "$staging_root/KeynopePresenter.app" ditto "$app" "$staging_root/Keynope.app" mkdir -p "$bin_dir" rm -f "$bin_dir/keynope" rm -rf "$bin_dir/KeynopePresenter.app" mv "$staging_root/keynope" "$bin_dir/keynope" mv "$staging_root/KeynopePresenter.app" "$bin_dir/KeynopePresenter.app" mkdir -p "$app_dir" rm -rf "$app_dir/Keynope.app" mv "$staging_root/Keynope.app" "$app_dir/Keynope.app" rmdir "$staging_root" shell_name="$(basename "${SHELL:-zsh}")" case "$shell_name" in bash) shell_config="$HOME/.bash_profile" ;; zsh) shell_config="$HOME/.zshrc" ;; *) shell_config="$HOME/.zshrc" ;; esac path_line='export PATH="$HOME/.keynope/bin:$PATH"' if [ ! -f "$shell_config" ] || ! grep -Fqx "$path_line" "$shell_config"; then printf '\n# Keynope\n%s\n' "$path_line" >> "$shell_config" path_updated=true else path_updated=false fi say "Keynope $version installed successfully." say "App: $app_dir/Keynope.app" say "CLI: $bin_dir/keynope" say "Presenter helper: $bin_dir/KeynopePresenter.app" if [ "$path_updated" = true ]; then say "Added $bin_dir to PATH in $shell_config." fi say "To use Keynope in this terminal now, run:" say 'export PATH="$HOME/.keynope/bin:$PATH"'