#!/bin/bash
# Deploy RealCV from local git repo to website
set -e

cd /git/RealCV

echo "Building application..."
dotnet publish src/RealCV.Web -c Release -o ./publish --nologo

echo "Stopping service..."
sudo systemctl stop realcv

echo "Backing up config..."
cp /var/www/realcv/appsettings.Production.json /tmp/appsettings.Production.json 2>/dev/null || true

echo "Deploying files..."
sudo rm -rf /var/www/realcv/*
sudo cp -r ./publish/* /var/www/realcv/

echo "Restoring config..."
sudo cp /tmp/appsettings.Production.json /var/www/realcv/ 2>/dev/null || true

echo "Setting permissions..."
sudo chown -R www-data:www-data /var/www/realcv

echo "Starting service..."
sudo systemctl start realcv

echo "Done! Checking status..."
sleep 2
sudo systemctl is-active realcv && echo "Service is running."
