#!/bin/bash

# 1. Update System and Install Networking/Utility Tools
sudo apt update
sudo apt install -y net-tools avahi-daemon openssh-server

# 2. Install phpMyAdmin and PHP extensions
# Note: Use 'DEBIAN_FRONTEND=noninteractive' if you want to skip prompts, 
# but manual install is safer to configure the phpMyAdmin password.
sudo apt install -y phpmyadmin php-mbstring php-zip php-gd php-json php-curl

# 3. Configure phpMyAdmin with Apache
sudo ln -sf /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
sudo a2enconf phpmyadmin

# 4. Set Hostname and Network Discovery (mDNS)
sudo hostnamectl set-hostname isedbms
sudo systemctl enable --now avahi-daemon
sudo systemctl restart avahi-daemon

# 5. Enable and Configure SSH
sudo systemctl enable --now ssh
sudo ufw allow ssh

# 6. Fix MariaDB Root Authentication (CRITICAL for your Dashboard code)
# This sets the root password to 'mysql@123' so your PHP mysqli connection works.
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('mysql@123'); FLUSH PRIVILEGES;"

# 7. Setup Web Project Directory and Permissions
sudo mkdir -p /var/www/html/users
# Giving 'bmsit' ownership so you can manually copy/edit files
sudo chown -R bmsit:bmsit /var/www/html/users
sudo chmod -R 755 /var/www/html/users

# 8. Create a placeholder PHP file
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/users/index.php

# 9. Restart Apache to apply all changes
sudo systemctl restart apache2

echo "------------------------------------------"
echo "Setup complete! Hostname: isedbms"
echo "MariaDB Root Password: mysql@123"
echo "Project Folder: /var/www/html/users/"
echo "Access Dashboard: http://isedbms.local"
echo "------------------------------------------"
