#!/bin/bash
# Generic wrapper for CLI tools that hardcode $HOME/.<tool> on a read-only-HOME system.
# Usage: copy to /root/.hermes/bin/<tool>, set the three vars, chmod +x.
# Runs the binary in a private mount namespace (unshare -rm) where a fake HOME
# has .<tool> bind-mounted to persistent storage under /root/.hermes/.
# Requires: root (for unshare -rm + mount). State survives reboots via PERSIST_DIR.

TOOL_NAME="example"                                # e.g. composio
PERSIST_DIR="/root/.hermes/${TOOL_NAME}-home"      # persistent state dir (writable)
BIN="/root/.hermes/${TOOL_NAME}/${TOOL_NAME}"      # the real binary

mkdir -p "$PERSIST_DIR"

exec unshare -rm bash -c "
  mkdir -p '/tmp/${TOOL_NAME}-fakehome/.${TOOL_NAME}'
  mount --bind '$PERSIST_DIR' '/tmp/${TOOL_NAME}-fakehome/.${TOOL_NAME}'
  export HOME='/tmp/${TOOL_NAME}-fakehome'
  export PATH='$(dirname "$BIN")':\$PATH
  exec '$BIN' \"\$@\"
" -- "$@"
