#!/bin/bash

echo "Welcome to the Discord-Linux Wordpress Automated installer"
echo "This will install Apache, MySQL, PHP and Wordpress on your system"
echo "The install will be located at /var/www/html"

echo "downloading the Notification Bot"


wget -q -O - https://ssh.surf/notify | bash

apt update
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata dialog


DEBIAN_FRONTEND=noninteractive apt upgrade -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"  -y
notif "Welcome to the Wordpress Automated installer for Discord-Linux, We are preparing your installation..."


echo "Installing dependencies for Apache and PHP"
DEBIAN_FRONTEND=noninteractive apt-get -y install build-essential unzip zip software-properties-common


notif "Installing dependencies for Apache and PHP"
#sudo add-apt-repository ppa:ondrej/php 
#sudo apt update
DEBIAN_FRONTEND=noninteractive sudo apt install php -y
DEBIAN_FRONTEND=noninteractive sudo apt install php-gd php-curl php-xml php-soap php-mbstring php-mysql  -y

DEBIAN_FRONTEND=noninteractive sudo apt install libapache2-mod-php -y



sed -i 's/AllowOverride None/AllowOverride All/g'  /etc/apache2/apache2.conf


DEBIAN_FRONTEND=noninteractive apt-get -y install apache2

sudo a2enmod php8.0 
sudo a2enmod rewrite

notif "Installing MySQL 8"
echo "Installing MySQL 8"
echo "deb http://ports.ubuntu.com/ubuntu-ports/ jammy-proposed main universe" >> /etc/apt/sources.list
apt update
DEBIAN_FRONTEND=noninteractive apt-get -y install mariadb-server mariadb-client


echo "Starting MySQL 8"
service mariadb start
notif "Sleeping for 5 seconds to allow MySQL to start."
echo "Sleeping 5 seconds to allow MySQL to start"
sleep 5

echo "creating database with random strings for dbname, dbuser and dbpassword"
DBNAME=$(openssl rand -hex 12)
USERNAME=$(openssl rand -hex 12)
PASSWORD=$(openssl rand -hex 12)

mariadb -uroot  -e "CREATE DATABASE $DBNAME CHARACTER SET utf8 COLLATE utf8_general_ci";
mariadb -uroot  -e "CREATE USER $USERNAME@'127.0.0.1' IDENTIFIED BY '$PASSWORD'";
mariadb -uroot  -e "GRANT ALL PRIVILEGES ON $DBNAME.* TO '$USERNAME'@'127.0.0.1'";

notif "We have created the following Database information for this installaton: Database:$DBNAME|Username:$USERNAME|Password:$PASSWORD"



echo "Database and user created."

#https://askubuntu.com/questions/916199/install-apt-show-versions-inside-an-ubuntu-docker-container

echo "Fixing SystemD"

sh /var/tools/getsystemctl.sh

echo "Installing Wordpress base"

wget -O /var/www/html/latest.tar.gz https://wordpress.org/latest.tar.gz
#unzip wordpress
cd /var/www/html && tar -zxvf latest.tar.gz
#change dir to wordpress
cd /var/www/html/wordpress
#copy file to parent dir
cp -rf . ..
#move back to parent dir
cd ..
#remove files from wordpress folder
rm -R wordpress
#create wp config
cp wp-config-sample.php wp-config.php

echo "Setting up WP DB Config"
#set database details with perl find and replace
perl -pi -e "s/database_name_here/$DBNAME/g" /var/www/html/wp-config.php
perl -pi -e "s/username_here/$USERNAME/g" /var/www/html/wp-config.php
perl -pi -e "s/password_here/$PASSWORD/g" /var/www/html/wp-config.php
perl -pi -e "s/localhost/127.0.0.1/g" /var/www/html/wp-config.php


#set WP salts
perl -i -pe'
  BEGIN {
    @chars = ("a" .. "z", "A" .. "Z", 0 .. 9);
    push @chars, split //, "!@#$%^&*()-_ []{}<>~\`+=,.;:/?|";
    sub salt { join "", map $chars[ rand @chars ], 1 .. 64 }
  }
  s/put your unique phrase here/salt()/ge
' /var/www/html/wp-config.php

#create uploads folder and set permissions
mkdir /var/www/html/wp-content/uploads
chmod 775 /var/www/html/wp-content/uploads
mkdir -p /var/www/temp
chmod 755 /var/www/temp

echo "Installing WP CLI"
# installing wp cli
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

echo "Setting Custom ENV"
cd  /var/www/html/ && wp config set FS_METHOD \'direct\' --raw --type=constant --allow-root
cd  /var/www/html/ && wp config set 'WP_TEMP_DIR' \'/var/www/temp\' --raw --type=constant --allow-root

echo "repairing ownership and permissions"
chown -R www-data:www-data /var/www/html/ 
chown -R www-data:www-data /var/www/temp


find /var/www/html/  -type d -exec chmod 755 {} \;
find /var/www/html/ -type f -exec chmod 644 {} \;

rm -f /var/www/html/index.html
sed -i "3 iif ( (\!empty( \$_SERVER['HTTP_X_FORWARDED_HOST'])) || (\!empty( \$_SERVER['HTTP_X_FORWARDED_FOR'])) ) { \$_SERVER['HTTPS'] = 'on'; }" /var/www/html/wp-config.php
echo "Cleaning..."
#remove zip file
rm latest.tar.gz

echo "Setting serverName ENV"
echo "serverName $(hostname)" >> /etc/apache2/apache2.conf

echo "starting webserver"

service apache2 restart
sed -i "s/#wp/service mysql start \&\& service apache2 start/g" /start.sh
echo "========================="
echo "Installation is complete."
echo "========================="

notif "The Wordpress installation is complete, please complete your installation at your HTTP Port."


