#!/usr/bin/env bash

# this is only intended for the vagrant shit
# for the love of god don't run this on anything but that
# configuration is almost identical to production

echo -e "> Misuzu Vagrant Auto Configurator "
echo -e ""

echo -e "=> Installing apt requirements"
apt-get update
apt-get install -y software-properties-common dirmngr apt-transport-https

echo -e "=> Adding PHP PPA"
add-apt-repository -y ppa:ondrej/php

echo -e "=> Adding MariaDB 10.6 repostiory"
apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
add-apt-repository -y 'deb [arch=amd64,arm64,ppc64el,s390x] https://ftp.nluug.nl/db/mariadb/repo/10.6/ubuntu focal main'

echo -e "=> Performing full package upgrade"
apt-get update
apt-get full-upgrade -y

echo -e "=> Installing required packages"
apt-get install -y nginx-full mariadb-server-10.6 openssl git \
    php8.1 php8.1-bcmath php8.1-cli php8.1-common php8.1-curl php8.1-dev \
    php8.1-fpm php8.1-gd php8.1-igbinary php8.1-imagick php8.1-intl \
    php8.1-ldap php8.1-mbstring php8.1-mysql php8.1-opcache php8.1-readline \
    php8.1-redis php8.1-sqlite3 php8.1-xml php8.1-zip

SSL_DHPARAM=/vagrant/devel/nginx/dhparam.pem
SSL_CRT=/vagrant/devel/nginx/misuzu.crt
SSL_KEY=/vagrant/devel/nginx/misuzu.key

echo -e "=> Generating dhparam.pem"
[ -f "$SSL_DHPARAM" ] || openssl dhparam -out $SSL_DHPARAM 2048

echo -e "=> Generating SSL certificate"
[ -f "$SSL_CRT" ] || [ -f "$SSL_KEY" ] || openssl req -subj '/O=Flashii/C=NL/CN=localhost' -new -newkey rsa:2048 -sha256 -days 9001 -nodes -x509 -keyout $SSL_KEY -out $SSL_CRT

echo -e "=> Replacing NGINX configuration"

echo -e "==> Removing existing configuration folder"
rm -rf /etc/nginx

echo -e "==> Linking Misuzu config folder"
ln -fs /vagrant/devel/nginx /etc/nginx

echo -e "==> Restarting NGINX"
service nginx restart

echo -e "=> Adjusting PHP configuration"

echo -e "==> Set display_startup_errors to On"
sed -i 's/display_startup_errors = Off/display_startup_errors = On/g' /etc/php/8.1/fpm/php.ini

echo -e "==> Increase max upload size to 150M"
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 150M/g' /etc/php/8.1/fpm/php.ini

echo -e "==> Increase max body size to 150M"
sed -i 's/post_max_size = 8M/post_max_size = 150M/g' /etc/php/8.1/fpm/php.ini

echo -e "==> Change FPM user to vagrant"
sed -i 's/user = www-data/user = vagrant/g' /etc/php/8.1/fpm/pool.d/www.conf
sed -i 's/listen.owner = www-data/listen.owner = vagrant/g' /etc/php/8.1/fpm/pool.d/www.conf

echo -e "==> Change FPM group to vagrant"
sed -i 's/group = www-data/group = vagrant/g' /etc/php/8.1/fpm/pool.d/www.conf
sed -i 's/listen.group = www-data/listen.group = vagrant/g' /etc/php/8.1/fpm/pool.d/www.conf

echo -e "==> Restarting PHP-FPM"
service php8.1-fpm restart

echo -e "=> Adjusting MariaDB configuration"

echo -e "==> Bind to all addresses"
sed -i 's/= 127.0.0.1/= 0.0.0.0/g' /etc/mysql/mariadb.conf.d/50-server.cnf
service mysql restart

echo -e "==> Creating MariaDB database"
mysql -vv -e "CREATE DATABASE misuzu COLLATE 'utf8mb4_bin'"

echo -e "==> Creating MariaDB user"
mysql -vv -e "CREATE USER 'misuzu'@'localhost' IDENTIFIED BY 'toastiscool100'"
mysql -vv -e "CREATE USER 'misuzu'@'%' IDENTIFIED BY 'toastiscool100'"

echo -e "==> Granting database access to MariaDB user"
mysql -vv -e "GRANT EXECUTE, SELECT, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, INDEX, INSERT, REFERENCES, TRIGGER, UPDATE, LOCK TABLES  ON misuzu.* TO 'misuzu'@'localhost'"
mysql -vv -e "GRANT EXECUTE, SELECT, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, INDEX, INSERT, REFERENCES, TRIGGER, UPDATE, LOCK TABLES  ON misuzu.* TO 'misuzu'@'%'"

echo -e "==> Reloading MariaDB privileges"
mysql -vv -e "FLUSH PRIVILEGES"

# Taken from https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md
# Remove when composer dependencies are dropkicked

echo -e "=> Installing Composer"

echo -e "==> Fetching expecting checksum"
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"

echo -e "==> Downloading installer"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

echo -e "==> Hashing installer"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

echo -e "==> Confirming checksum"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

echo -e "==> Installing to /bin/composer"
php composer-setup.php --quiet --install-dir=/bin --filename=composer

echo -e "==> Removing installer"
rm composer-setup.php
# End of composer things

echo -e "=> Adjusting Misuzu configuration"

MSZ_CONFIG=/vagrant/config/config.ini

echo -e "==> Replacing Misuzu config.ini"
rm $MSZ_CONFIG
cp /vagrant/devel/misuzu/config.ini $MSZ_CONFIG

echo -e "==> Updating Git submodules"
sudo -u vagrant git -C /vagrant submodule update --init

# ENTER: JANK
echo -e "==> Enable Misuzu debug mode"
sudo -u vagrant touch /vagrant/.debug

echo -e "==> Running composer install as vagrant"
sudo -u vagrant composer install -d /vagrant

echo -e "==> Adding frequent cron jobs as vagrant"
(sudo -u vagrant crontab -l 2>/dev/null; echo "0,10,20,30,40 * * * * php8.1 /www/misuzu/msz cron") | sudo -u vagrant crontab -

echo -e "==> Adding infrequent cron jobs as vagrant"
(sudo -u vagrant crontab -l 2>/dev/null; echo "50 * * * * php8.1 /www/misuzu/msz cron --slow") | sudo -u vagrant crontab -

echo -e "==> Nuking /www"
rm -rf /www

echo -e "==> Creating /www"
mkdir /www

echo -e "==> Linking /vagrant to /www/misuzu"
ln -fs /vagrant /www/misuzu

echo -e "Done!"
