#!/bin/bash
# Clean removal of BookStack

set -e
echo "=== Removing BookStack ==="
date

# Stop PHP-FPM
systemctl stop php8.3-fpm

# Remove BookStack directory
rm -rf /var/www/bookstack

# Remove Nginx config
rm -f /etc/nginx/sites-enabled/bookstack
rm -f /etc/nginx/sites-available/bookstack

# Drop database
mysql -e "DROP DATABASE IF EXISTS bookstack_db;"
mysql -e "DROP USER IF EXISTS 'bookstack_user'@'localhost';"

# Remove SSL certificate (we'll recreate for MkDocs)
certbot delete --cert-name docs.firefrostgaming.com --non-interactive

# Reload Nginx
nginx -t && systemctl reload nginx

# Remove sync script and cron
rm -f /opt/bookstack-sync.sh
crontab -l | grep -v bookstack-sync | crontab -

echo ""
echo "=== BookStack Removed ==="
echo "Ready for MkDocs deployment"
