#!/bin/bash

###############################################################################
# Setup a VM with Watch My Domains SED
#
# Copyright (C) 2025 CodePunch Solutions - All Rights Reserved
# https://codepunch.com/
#
# Author: Syam Kumar
# Last revised on 29 May 2025
#
# This will check the OS version, download the installation script for that OS version,
# make it an executable, and run it.
#
# The downloaded script will install Apache, PHP, LetsEncrypt, fail2ban, MariaDB etc.
# A user 'wmdsed' will be created (password saved to /root/required.txt)
# MariaDB root password is also saved to /root/required.txt. Please
# delete this file after saving the passwords.
#
###############################################################################

# shellcheck source=/dev/null # https://www.shellcheck.net/wiki/SC1091
source /etc/os-release
INSTALL_URL="https://learn.domainpunch.com/static/downloads/"

if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

if [[ "$ID" = "ubuntu" ]]; then
	case "$VERSION_ID" in
		"22.04") SCRIPT_NAME="wmdsed6-ubuntu-22.04-php-8.1.sh";;
		"24.04") SCRIPT_NAME="wmdsed6-ubuntu-24.04-php-8.3.sh";;
		*) echo "Unsupported Ubuntu version: $VERSION_ID"; exit 1;;
	esac
elif [[ "$ID" = "debian" && "$VERSION_ID" == "12" ]]; then
	SCRIPT_NAME="wmdsed6-debian-12-php-8.2.sh"
elif [[ "$ID" = "debian" && "$VERSION_ID" == "13" ]]; then
	SCRIPT_NAME="wmdsed6-debian-13-php-8.4.sh"
else
	echo "Unsupported OS: $ID $VERSION_ID"
	exit 1
fi

echo "Detected OS: $ID $VERSION_ID"
echo "Downloading installation script: $SCRIPT_NAME"
wget "${INSTALL_URL}${SCRIPT_NAME}" -O "$SCRIPT_NAME"

# Ensure the script has execution permissions
chmod u+x "$SCRIPT_NAME"

echo "Running installation script..."
./"$SCRIPT_NAME" $1 $2
