#!/bin/bash
#
# piNail2 Deploy Script
#
# Deploys piNail2 to the Raspberry Pi. Steps:
#   1. Copy piNail2 files to ~/piNail2/ on the Pi
#   2. Copy the MAX6675 library from the old project
#   3. Install pip dependencies (system-wide for Python 3.5)
#   4. Optionally install and enable the systemd service
#
# Usage: ./deploy.sh [--service]
#   --service   Also install and enable the systemd service
#

set -euo pipefail

PI_HOST="pinail"  # SSH alias from ~/.ssh/config
PI_USER="pi"
REMOTE_DIR="/home/${PI_USER}/piNail2"
LOCAL_DIR="$(cd "$(dirname "$0")" && pwd)"
INSTALL_SERVICE=false

if [[ "${1:-}" == "--service" ]]; then
    INSTALL_SERVICE=true
fi

echo "=== piNail2 Deployment ==="
echo "Target: ${PI_HOST} -> ${REMOTE_DIR}"
echo ""

# -------------------------------------------------------------------
# Step 1: Copy project files
# -------------------------------------------------------------------
echo "--- Step 1: Copying piNail2 to Pi ---"

ssh ${PI_HOST} "mkdir -p ${REMOTE_DIR}/static ${REMOTE_DIR}/templates ${REMOTE_DIR}/logs"

# Copy Python source files
scp -q "${LOCAL_DIR}/app.py" \
       "${LOCAL_DIR}/config.py" \
       "${LOCAL_DIR}/thermocouple.py" \
       "${LOCAL_DIR}/pid_controller.py" \
       "${LOCAL_DIR}/config.json" \
       "${LOCAL_DIR}/requirements.txt" \
       "${LOCAL_DIR}/pinail.service" \
       "${PI_HOST}:${REMOTE_DIR}/"

# Copy static + templates
scp -q "${LOCAL_DIR}/static/app.js" \
       "${LOCAL_DIR}/static/style.css" \
       "${PI_HOST}:${REMOTE_DIR}/static/"

scp -q "${LOCAL_DIR}/templates/index.html" \
       "${PI_HOST}:${REMOTE_DIR}/templates/"

echo "Files copied."
echo ""

# -------------------------------------------------------------------
# Step 2: Copy MAX6675 library from old project
# -------------------------------------------------------------------
echo "--- Step 2: Copying MAX6675 library ---"

ssh ${PI_HOST} bash -s <<'REMOTE_SCRIPT'
# Copy the MAX6675 Python package from the old piNail project
if [ -d /home/pi/piNail/MAX6675-master/MAX6675 ]; then
    cp -r /home/pi/piNail/MAX6675-master/MAX6675 /home/pi/piNail2/MAX6675
    echo "MAX6675 library copied from old project"
else
    echo "WARNING: MAX6675 library not found in old project!"
fi
REMOTE_SCRIPT

echo ""

# -------------------------------------------------------------------
# Step 3: Install pip dependencies
# -------------------------------------------------------------------
echo "--- Step 3: Installing dependencies ---"

ssh ${PI_HOST} bash -s <<'REMOTE_SCRIPT'
cd /home/pi/piNail2

echo "Installing dependencies with pip3..."
pip3 install --user -r requirements.txt 2>&1 | tail -10

echo ""
echo "Key packages:"
pip3 show Flask simple-pid RPi.GPIO Adafruit-GPIO 2>/dev/null | grep -E '^(Name|Version):'
REMOTE_SCRIPT

echo ""

# -------------------------------------------------------------------
# Step 4: Systemd service (optional)
# -------------------------------------------------------------------
if $INSTALL_SERVICE; then
    echo "--- Step 4: Installing systemd service ---"

    ssh ${PI_HOST} bash -s <<'REMOTE_SCRIPT'
sudo cp /home/pi/piNail2/pinail.service /etc/systemd/system/pinail2.service
sudo systemctl daemon-reload
sudo systemctl enable pinail2.service
echo "Service installed and enabled (pinail2.service)"
echo "Start with: sudo systemctl start pinail2"
echo "Logs with:  sudo journalctl -u pinail2 -f"
REMOTE_SCRIPT
else
    echo "--- Step 4: Skipping systemd service (use --service flag to install) ---"
fi

echo ""
echo "=== Deployment complete! ==="
echo ""
echo "To run manually:"
echo "  ssh ${PI_HOST}"
echo "  cd ~/piNail2"
echo "  python3 app.py"
echo ""
echo "Web UI will be at: http://192.168.0.159:5000"
