#!/usr/bin/env bash
#
# Deploys dust to server.inhive.ro (Aegis), with Whisp Edge as the public front door.
#
# Environment:
#   DUST_DEPLOY_HOST  the server, `server.inhive.ro` by default
#   DUST_DEPLOY_PORT  the SSH port, 2244 by default
#   DUST_DEPLOY_USER  the SSH user, root by default
#
# The script uses your SSH key. The Aegis host keeps nginx on :8080/:8081 and serves
# public traffic through `io-edge` on :80/:443, so this script installs edge vhosts,
# not nginx site files.

set -euo pipefail

HOST="${DUST_DEPLOY_HOST:-server.inhive.ro}"
PORT="${DUST_DEPLOY_PORT:-2244}"
USER="${DUST_DEPLOY_USER:-root}"

HERE="$(cd "$(dirname "$0")" && pwd)"
SOURCE="$(cd "$HERE/.." && pwd)"

REMOTE_HOME=/var/www/html/dust-framework
REMOTE_DEMO=/var/www/html/dust-framework-demo
EDGE_BIN=/aegis/apps/web/edge/0.1/bin/io-edge

SSH=(ssh -p "$PORT" -o StrictHostKeyChecking=accept-new "$USER@$HOST")
RSYNC=(rsync -az --delete --no-owner --no-group \
    --exclude target \
    --exclude .git \
    --exclude archive \
    --exclude .idea \
    --exclude node_modules \
    --exclude build \
    --exclude /sandbox \
    --exclude dust-framework-src.tar.gz \
    --exclude /index.html \
    --exclude /install.html \
    --exclude /install.sh \
    --exclude /install.ps1 \
    -e "ssh -p $PORT -o StrictHostKeyChecking=accept-new")
COPY=(rsync -az --no-owner --no-group -e "ssh -p $PORT -o StrictHostKeyChecking=accept-new")

echo "==> framework source to $REMOTE_HOME"
"${SSH[@]}" "mkdir -p $REMOTE_HOME $REMOTE_HOME/build/docs $REMOTE_DEMO"
"${RSYNC[@]}" "$SOURCE/" "$USER@$HOST:$REMOTE_HOME/"

echo "==> documentation site"
(cd "$HERE/docs" && npm install --no-audit --no-fund --silent && node build.mjs)
"${RSYNC[@]}" --delete "$SOURCE/build/docs/" "$USER@$HOST:$REMOTE_HOME/build/docs/"
"${COPY[@]}" "$SOURCE/sandbox/site/" "$USER@$HOST:$REMOTE_HOME/"

echo "==> toolchain and the dust command"
"${SSH[@]}" "if [ ! -x \"\$HOME/.cargo/bin/cargo\" ] && ! command -v cargo >/dev/null; then command -v rustup >/dev/null || curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal; fi"
"${SSH[@]}" "export PATH=\$HOME/.cargo/bin:\$PATH; cd $REMOTE_HOME && cargo build --release --quiet -p dust-cli && install -m 0755 target/release/dust /usr/local/bin/dust && dust --version"

echo "==> source archive"
"${SSH[@]}" "rm -f /tmp/dust-framework-src.tar.gz; tar --exclude=dust-framework/target --exclude=dust-framework/.git --exclude=dust-framework/archive --exclude=dust-framework/build --exclude=dust-framework/node_modules --exclude=dust-framework/sandbox --transform 's,^dust-framework,dust-framework-src,' -czf /tmp/dust-framework-src.tar.gz -C /var/www/html dust-framework && mv /tmp/dust-framework-src.tar.gz $REMOTE_HOME/dust-framework-src.tar.gz"

echo "==> demo application"
"${RSYNC[@]}" --exclude .env --exclude storage/app/database.sqlite --exclude storage/app/public --exclude storage/app/private --exclude storage/logs --exclude storage/framework/cron.pid --exclude /bin --exclude modules/.backup "$SOURCE/sandbox/examples/demo/" "$USER@$HOST:$REMOTE_DEMO/"
"${SSH[@]}" "cd $REMOTE_DEMO && sed -i 's|path = \"../../..\"|path = \"$REMOTE_HOME\"|' Cargo.toml && { [ -f .env ] || cp .env.example .env; }; grep -q '^APP_BASE_PATH=' .env || printf 'APP_BASE_PATH=$REMOTE_DEMO\n' >> .env; if ! grep -q '^APP_KEY=base64:' .env; then sed -i \"s|^APP_KEY=.*|APP_KEY=base64:\$(openssl rand -base64 32)|\" .env; fi"
"${SSH[@]}" "cd $REMOTE_DEMO && for setting in SESSION_DRIVER=database CACHE_STORE=database QUEUE_CONNECTION=database PULSE_ACCESS=public; do key=\${setting%%=*}; if grep -q \"^\$key=\" .env; then sed -i \"s|^\$key=.*|\$setting|\" .env; else printf '%s\n' \"\$setting\" >> .env; fi; done"
# Mail settings are defaults only: an operator who configured SMTP keeps their values across deploys.
"${SSH[@]}" "cd $REMOTE_DEMO && for setting in SMTP_HOST=127.0.0.1 SMTP_PORT=25 SMTP_ENCRYPTION=none MAIL_FROM_ADDRESS=smtp@dustapp.dev; do key=\${setting%%=*}; grep -q \"^\$key=\" .env || printf '%s\n' \"\$setting\" >> .env; done"
"${SSH[@]}" "cd $REMOTE_DEMO && export PATH=\$HOME/.cargo/bin:\$PATH && /usr/local/bin/dust build --release"
"${SSH[@]}" "chown -R aegis-www:aegis-www $REMOTE_DEMO $REMOTE_HOME/build/docs"
"${SSH[@]}" "cd $REMOTE_DEMO && runuser -u aegis-www -- ./bin/dust-demo install --force"
echo "==> application guides"
"${SSH[@]}" "cd $REMOTE_DEMO && runuser -u aegis-www -- /usr/local/bin/dust docs:install"

echo "==> services and edge vhosts"
"${SSH[@]}" "mkdir -p /etc/systemd/system /aegis/config/edge/vhosts"
"${COPY[@]}" "$SOURCE/deploy/systemd/" "$USER@$HOST:/etc/systemd/system/"
"${COPY[@]}" "$SOURCE/deploy/edge/" "$USER@$HOST:/aegis/config/edge/vhosts/"
"${SSH[@]}" "systemctl daemon-reload; systemctl disable --now dust-worker 2>/dev/null || true; systemctl enable --now dust-demo dust-relay && systemctl restart dust-demo dust-relay"
"${SSH[@]}" "$EDGE_BIN reload -c /aegis/config/edge/edge.toml"

echo "==> verifying"
"${SSH[@]}" "sleep 2; curl -fsS -o /dev/null -w 'site %{http_code}\\n' https://dustapp.dev/; curl -fsS -o /dev/null -w 'docs %{http_code}\\n' https://dustapp.dev/docs/commands/; curl -fsS -o /dev/null -w 'demo %{http_code}\\n' https://demo.dustapp.dev/; for path in /pulse /pulse/requests /pulse/sessions /pulse/jobs /pulse/cron /demos/session /demos/queue /demos/db /demos/validation /demos/mail /demos/media /demos/pipeline /demos/relay /welcome; do printf '%-24s ' \"\$path\"; curl -sS -o /dev/null -w '%{http_code}\\n' \"https://demo.dustapp.dev\$path\"; done"

echo "Deployed: https://dustapp.dev and https://demo.dustapp.dev"
