#!/usr/bin/env bash
# start.sh — used as the systemd service ExecStart.
# Pulls latest code, installs dependencies, then replaces this shell with
# the Node process so systemd tracks the correct PID and restart works.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

echo "Clearing old database lock if needed"
rm -rf filemanager.db.lock

echo "[start.sh] git reset --hard"
git reset --hard

echo "[start.sh] git pull"
git pull

echo "[start.sh] npm install --omit=dev"
npm install --omit=dev

echo "[start.sh] starting server"
exec node server/index.js
