#!/bin/sh
set -eu

cert=${1:-}
if [ -z "$cert" ] || [ ! -f "$cert" ]; then
  echo "Usage: sudo $0 /path/to/ghobooz-root.crt" >&2
  exit 1
fi

subject=$(openssl x509 -in "$cert" -noout -subject 2>/dev/null || true)
case "$subject" in
  *Ghobooz\ Indentification\ Radar*) ;;
  *)
    echo "This file is not the Ghobooz Indentification Radar ($subject)" >&2
    exit 1
    ;;
esac

if [ "$(id -u)" -ne 0 ]; then
  echo "Run with sudo." >&2
  exit 1
fi

security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "$cert"
echo "Ghobooz Indentification Radar is in the system keychain. Restart the browser."
echo "Until this step, the browser correctly shows the site as untrusted."
