#!/bin/bash

# Run as root check
if [ "$(id -u)" -ne 0 ]; then
    echo "This script must be run as root."
    exit 1
fi

# Update system
echo "Updating the system..."
apt update && apt upgrade -y

# Install base packages
echo "Installing required packages: sudo, expect, openssh-server, composer..."
apt install -y sudo expect openssh-server composer

# Enable and start SSH
echo "Enabling and starting SSH server..."
systemctl enable ssh
systemctl start ssh
echo "SSH server is running."

# Prompt for new Linux user
read -p "Enter the new username: " NEW_USER

# Check if user already exists
if id "$NEW_USER" &>/dev/null; then
    echo "User $NEW_USER already exists. Exiting."
    exit 1
fi

# Prompt for password (used for both Linux and MariaDB)
read -sp "Enter password for $NEW_USER (used for both Linux and MariaDB): " USER_PASS
echo

# Create Linux user and set password
adduser --gecos "" "$NEW_USER" --disabled-password
echo "$NEW_USER:$USER_PASS" | chpasswd
usermod -aG sudo "$NEW_USER"
echo "User $NEW_USER created and added to sudo group."

# Install Apache
echo "Installing Apache..."
apt install apache2 -y
systemctl start apache2
systemctl enable apache2
echo "Apache installed and running."

# Install MariaDB
echo "Installing MariaDB..."
apt install mariadb-server mariadb-client -y
systemctl start mariadb
systemctl enable mariadb
echo "MariaDB installed and running."

# Secure MariaDB using expect
echo "Securing MariaDB..."
SECURE_MYSQL=$(expect -c "
set timeout 10
spawn mysql_secure_installation
expect \"Enter current password for root (enter for none):\" { send \"\r\" }
expect \"Switch to unix_socket authentication\" { send \"n\r\" }
expect \"Change the root password?\" { send \"n\r\" }
expect \"Remove anonymous users?\" { send \"y\r\" }
expect \"Disallow root login remotely?\" { send \"y\r\" }
expect \"Remove test database and access to it?\" { send \"y\r\" }
expect \"Reload privilege tables now?\" { send \"y\r\" }
expect eof
")
echo "$SECURE_MYSQL"

# Create matching MariaDB user
echo "Creating MariaDB user '$NEW_USER'..."
mysql -e "CREATE USER '$NEW_USER'@'localhost' IDENTIFIED BY '$USER_PASS';"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO '$NEW_USER'@'localhost' WITH GRANT OPTION;"
mysql -e "FLUSH PRIVILEGES;"
echo "MariaDB user '$NEW_USER' created with full privileges."

# Install PHP
echo "Installing PHP and extensions..."
apt install php libapache2-mod-php php-mysql php-intl -y

# Configure Apache to prioritize PHP
echo "Configuring Apache for PHP..."
sed -i "s/index.html/index.php index.html/" /etc/apache2/mods-enabled/dir.conf
systemctl restart apache2

# Create test PHP file
echo "<?php phpinfo(); ?>" > /var/www/html/test.php
echo "PHP test file created at /var/www/html/test.php"

# Install phpMyAdmin silently
echo "Installing phpMyAdmin..."
export DEBIAN_FRONTEND=noninteractive
apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl -y
phpenmod mbstring
ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
a2enconf phpmyadmin
systemctl reload apache2
echo "phpMyAdmin installed and linked with Apache."

echo "Setup complete! System will reboot in 10 seconds..."
sleep 10
reboot