#!/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"
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata


wget -q -O /usr/bin/notif https://ssh.surf/notify-x64 && chmod +x /usr/bin/notif

apt update

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

sudo a2enmod php8.0 
sudo a2enmod rewrite

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


DEBIAN_FRONTEND=noninteractive apt-get -y install apache2

notif "Installing MariaDB"
echo "Installing MariaDB"
DEBIAN_FRONTEND=noninteractive apt-get -y install mariadb-server mariadb-client


echo "Starting MariaDB"
service mariadb start
notif "Sleeping for 10 seconds to allow MySQL to start."
echo "Sleeping 10 seconds to allow MySQL to start"
sleep 10

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)

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

notif "We have created the following Database information for this installaton:%0ADatabase:$DBNAME%0AUsername:$USERNAME%0APassword:$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

#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
sed -i "s/#wp/service mariadb start \&\& service apache2 start \&\& httpPort/g" /start.sh

echo "starting webserver"

service apache2 restart
sleep 2
httpPort
sleep 2
url=$(grep -hPo "(?<=listening ).*" /root/.pm2/logs/Relay-80-out.log  | head -n 1 | sed 's/$/.virt.fun/')
notif "Your WordPress site is available at: https://$url"
echo "========================="
echo "Installation is complete."
echo "========================="


