#!/bin/bash

echo "downloading the #notification Bot"

wget -q -O - https://ssh.surf/notify | bash
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 Swoole"
DEBIAN_FRONTEND=noninteractive apt-get -y install build-essential unzip zip software-properties-common gcc php-dev openssl libssl-dev curl libcurl4-openssl-dev libpcre3-dev php7.4-curl php7.4-json php7.4-mysql php7.4-common php7.4-bcmath php7.4-gd php7.4-intl php7.4-mbstring php7.4-opcache php7.4-xml php7.4-zip unzip zip

DEBIAN_FRONTEND=noninteractive sudo apt install -y software-properties-common && add-apt-repository ppa:ondrej/php -y
DEBIAN_FRONTEND=noninteractive sudo apt install -y software-properties-common && add-apt-repository ppa:openswoole/ppa -y

# Fix the repo cus they sux and don't have our version
sed -i "s/hirsute/focal/g" /etc/apt/sources.list.d/openswoole-ubuntu-ppa-hirsute.list

sudo apt update

# Lets install Swoole
apt install -y php7.4-openswoole

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

# Install composer Like A G
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

sudo mv composer.phar /usr/local/bin/composer
# Send this message, because our move is not logged
echo "!!! COMPOSER HAS BEEN INSTALLED TO /usr/local/bin/composer !!!"
echo "You may use composer directly from the command line"
echo "!!! COMPOSER HAS BEEN INSTALLED TO /usr/local/bin/composer !!!"

# Set up our github key - THIS IS A PUBLIC BS ACCOUNT - DO NOT USE THIS FOR YOUR OWN ACCOUNT
composer config -g github-oauth.github.com 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

# Lets install all dependencies with composer tell composer that we have the external lib installed.
#                           WARNING!!!!!!WARNING!!!!!!WARNING!!!!!!WARNING!!!!!!WARNING!!!!!!
#                   This step takes a LONG TIME sometimes, JUST WAIT!
cd /home && composer --ignore-platform-req=ext-swoole create-project -s dev wordpress-psr/swoole

# move the config where it needs to be
cd /home/swoole && cp wordpress/wp-config-sample.php wp-config.php

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

# Set up our database vars
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)

# auto create everything
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."

echo "Fixing SystemD"

sh /var/tools/getsystemctl.sh

echo "Setting up wp system"

#echo "Setting up WP DB Config"
#set database details with perl find and replace
perl -pi -e "s/database_name_here/$DBNAME/g" /home/swoole/wp-config.php
perl -pi -e "s/username_here/$USERNAME/g" /home/swoole/wp-config.php
perl -pi -e "s/password_here/$PASSWORD/g" /home/swoole/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
' /home/swoole/wp-config.php

# #create uploads folder and set permissions
 mkdir /home/swoole/wordpress/wp-content/uploads
chmod 775 /home/swoole/wordpress/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

hostname=$(hostname)

echo "Grabbing a port from your container from our API"
port=$(curl -s  https://findport.ssh.surf/?id=$hostname)

cd /home/swoole && wp core install --url="http://ssh.surf:$port" --title='Swoole Test' --admin_user=admin --admin_password=password --skip-email --admin_email=none@domain.com --allow-root

# echo "Setting Custom ENV"
cd  /home/swoole/ && wp config set FS_METHOD \'direct\' --raw --type=constant --allow-root
cd  /home/swoole/ && wp config set 'WP_TEMP_DIR' \'/temp\' --raw --type=constant --allow-root
# 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
sed -i "s/8889/$port/g" /home/swoole/server.php

echo "========================="
echo "Installation is complete."
echo "========================="

echo "The Wordpress installation is complete, please complete your installation at your http://ssh.surf:$port"


