#!/bin/sh
# uploy launcher (portable build) — execs the unified seamain entry.
# No args → foreground daemon; args → CLI subcommand tree (e.g.
# `uploy autostart install`, `uploy deploy <id>`). Honors UPLOY_HOME
# for the data dir; refuses to double-start a daemon invocation.
DIR=$(cd "$(dirname "$0")/.." && pwd)
if [ -n "$UPLOY_HOME" ]; then DATA_DIR="$UPLOY_HOME"; else DATA_DIR="$HOME/.uploy"; fi
mkdir -p "$DATA_DIR"
PID_FILE="$DATA_DIR/server.pid"
# Only the no-arg (daemon) invocation is guarded against double-start; a
# CLI subcommand is a one-shot and may run alongside the daemon.
if [ $# -eq 0 ] && [ -f "$PID_FILE" ]; then
  PID=$(cat "$PID_FILE" 2>/dev/null || true)
  if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then
    echo "uploy already running (pid $PID). Dashboard: http://localhost:8765"; exit 0
  fi
fi
export NODE_ENV=production
exec node "$DIR/server/dist/seamain.js" "$@"
