#!/bin/sh
# Copyright © 2026, PhotoStructure Inc.

# BY RUNNING THIS SOFTWARE YOU AGREE TO ALL THE TERMS OF THIS LICENSE:
# https://photostructure.com/eula

trap 'exit 130' INT

DIR="$(dirname "$(realpath "$0")")"/

OS="$(uname -o 2>/dev/null || uname -s)"

if [ "$OS" = "Msys" ] || [ "$OS" = "Cygwin" ]; then
  NODE="${NODE:-node.exe}"
else
  NODE="${NODE:-node}"
  # If NODE is an absolute path, add its directory to PATH so co-located
  # tools (npm, npx) are also found. This is needed for nvm/fnm/volta
  # installations where tools aren't in standard system directories.
  NODE_DIR="$(dirname "$NODE")"
  if [ "$NODE_DIR" != "." ]; then
    PATH="$NODE_DIR:$PATH"
  fi
  if [ "$OS" = "Darwin" ]; then
    PATH="$PATH:/opt/homebrew/bin:/opt/homebrew/sbin"
  fi
  export PATH="$PATH:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/lib/libraw:/usr/sbin:/usr/bin:/sbin:/bin"
fi

export NODE_ENV=production

"$NODE" "$DIR/bin/bootstrap.js" "$@" || exit 1

exec "$NODE" "$DIR/bin/photostructure.js" "$@"
