#/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 /usr/bin/notif https://ssh.surf/notif && chmod +x /usr/bin/notif
echo "Done"

notif "Thank for for using Discord-Linux x64 Beta! We are preparing your Wordpress installation..."

echo "======System Info========"
uptime
hostname
ifconfig
free -h
df -h
echo "======Updating System======="
apt update
DEBIAN_FRONTEND=noninteractive apt upgrade -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"  -

echo "=====Allowing Overrides========"
sed -i 's/AllowOverride None/AllowOverride All/g'  /etc/apache2/apache2.conf
echo "done."
echo "======Starting MariaDB======="
service mariadb start
echo "sleeping 3 seconds to allow mysql to start"
sleep 3
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'";

echo "Database and user created."
notif "We have created the following Database information for this installaton: Database: $DBNAME, Username: $USERNAME, Password: $PASSWORD"

#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

echo "====starting webserver====="
service apache2 start
echo "=====starting httpRelay===="
httpPort
sed -i "s/#wp/service mariadb start \&\& service apache2 start/g" /start.sh
echo "========================="
echo "Installation is complete."
echo "========================="
sed -i 's/wpinstaller//g' /start.sh
mv /wpInstaller_log /var/log
sleep 3
echo "$(httpURL)"
echo "$(httpURL)" > /wpInstallComplete

notif "Installation is complete. You can access your site at $(httpURL)"