#!/bin/bash

# RTSDA Church Website Deployment Script
set -e

echo "Setting up RTSDA Church Website..."

# Create directories
sudo mkdir -p /var/www/rtsda-website
sudo mkdir -p /opt/rtsda-website

# Copy static assets to web directory
sudo cp -r css/ js/ images/ /var/www/rtsda-website/
sudo chown -R www-data:www-data /var/www/rtsda-website

# Copy source code to opt directory
sudo cp -r src/ Cargo.toml Cargo.lock /opt/rtsda-website/
sudo chown -R rockvilleav:rockvilleav /opt/rtsda-website

# Build the application
cd /opt/rtsda-website
cargo build --release

# Create systemd service file
sudo tee /etc/systemd/system/rtsda-website.service > /dev/null <<EOF
[Unit]
Description=RTSDA Church Website
After=network.target

[Service]
Type=simple
User=rockvilleav
Group=rockvilleav
WorkingDirectory=/opt/rtsda-website
ExecStart=/opt/rtsda-website/target/release/axum-church-website
Restart=always
RestartSec=10
Environment=RUST_LOG=info

[Install]
WantedBy=multi-user.target
EOF

# Enable and start the service
sudo systemctl daemon-reload
sudo systemctl enable rtsda-website
sudo systemctl start rtsda-website

echo "Deployment complete!"
echo "Service status:"
sudo systemctl status rtsda-website --no-pager