#!/bin/sh
set -eu

install -d -o hermes -g hermes -m 0700 \
  /opt/data \
  /opt/data/.sellable-agent \
  /opt/data/profiles \
  /run/sellable-agent \
  /run/sellable-agent/admin-runtime
chown -R hermes:hermes /opt/data
chmod 0700 /opt/data

if [ -f /run/secrets/sellable-admin-recovery-private-key.pem ]; then
  chown hermes:hermes \
    /run/secrets \
    /run/secrets/sellable-admin-recovery-private-key.pem
  chmod 0700 /run/secrets
  chmod 0400 /run/secrets/sellable-admin-recovery-private-key.pem
fi

if [ -S /.fly/api ]; then
  chown root:hermes /.fly/api
  chmod 0660 /.fly/api
fi

exec /usr/local/bin/node /usr/local/lib/sellable-agent/admin-runtime/admin-runtime.mjs
