#!/bin/bash
set -e

APP_NAME="runosaari"
APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

echo "Deploying $APP_NAME from $APP_DIR..."

cd "$APP_DIR"

echo "Pulling latest changes..."
sudo -E git pull

echo "Installing dependencies..."
sudo bun install

echo "Building application..."
sudo bun run build

echo "Starting/reloading with pm2..."
if pm2 describe "$APP_NAME" > /dev/null 2>&1; then
  pm2 reload "$APP_NAME" --update-env
else
  pm2 start bun --name "$APP_NAME" -- run start
fi

pm2 save

echo "Deployment complete."
