#!/bin/sh
# Image build: put the two plan sign-in add-ons into Hermes' own provider
# folder, unchanged and pinned by git commit. They do nothing until staff sign
# a machine in (sellable-model-signin); the programs themselves are installed
# then, on that machine only.
# With a folder as the first argument (a customer image), they are checked in
# place and then moved there, where Hermes does not look: the boot gives Hermes
# only the ones the machine is signed in to (placeModelProviderAddons).
set -eux
shelf="${1:-}"
nous_commit=6620f7ca295d86cc622e9dc4d5817fd5a695297f
agy_commit=6f80aefff442c1b2eb578480fb444243548d7e07
root=/opt/hermes/plugins/model-providers
work="$(mktemp -d)"
fetch() { # <repo> <commit> <dir>
  git init -q "$3"
  git -C "$3" fetch -q --depth 1 "$1" "$2"
  git -C "$3" checkout -q FETCH_HEAD
  test "$(git -C "$3" rev-parse HEAD)" = "$2"
}
place() { # <source dir> <provider name> <commit>
  install -d -o root -g root -m 0555 "${root}/$2"
  for file in "$1"/*.py "$1/plugin.yaml" "$1/LICENSE"; do
    case "${file##*/}" in conftest.py) continue ;; esac
    install -o root -g root -m 0444 "${file}" "${root}/$2/${file##*/}"
  done
  printf '%s\n' "$3" > "${root}/$2/SOURCE_COMMIT"
  chmod 0444 "${root}/$2/SOURCE_COMMIT"
  PYTHONPATH=/opt/hermes HERMES_HOME="${work}/home" /opt/hermes/.venv/bin/python -c \
    "import sys; from providers import get_provider_profile; p = get_provider_profile(sys.argv[1]); assert p is not None, sys.argv[1]" "$2"
}
fetch https://github.com/NousResearch/hermes-official-plugins "${nous_commit}" "${work}/nous"
place "${work}/nous/claude-subscription-directsdk" claude-subscription-directsdk-experimental "${nous_commit}"
fetch https://github.com/soyelmismo/hermes-antigravity-subscription "${agy_commit}" "${work}/agy"
place "${work}/agy" antigravity-subscription-directsdk "${agy_commit}"
rm -rf "${work}"
if [ -n "${shelf}" ]; then
  install -d -o root -g root -m 0755 "${shelf}"
  for name in claude-subscription-directsdk-experimental antigravity-subscription-directsdk; do
    mv "${root}/${name}" "${shelf}/${name}"
  done
fi
