#!/usr/bin/env bash

sudo hostnamectl set-hostname ubuntu-focal-BASEWORX-TAKENPLACE

add-apt-repository ppa:ondrej/php -y

apt-get update
apt-get install -yq apache2 php8.0 mysql-server mysql-client php8.0-mysql tree unzip zip
apt-get install -y unixodbc-dev
apt-get install -y unixodbc

apt-get install -y php8.0 php8.0-dev php8.0-xml php8.0-mbstring php8.0-curl php8.0-dev php8.0-zip libmcrypt-dev php-pear php8.0-bcmath php8.0-bcmath php8.0-mbstring php8.0-mysql -y --allow-unauthenticated
apt-get install -y php8.0-xml php8.0-gd
apt-get install -y unixodbc-dev

apt-get install -y libapache2-mod-php8.0 apache2
a2dismod mpm_event
a2enmod mpm_prefork
a2enmod php8.0

update-alternatives --set php /usr/bin/php8.0

# Composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
#commented out as the checksum changes occasionally
# 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');"
mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

# Node.js & npm & yarn
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
sudo bash n lts
sudo npm install -g n
rm n
npm install --global yarn

pecl install mcrypt-1.0.4 #1.0.2 for PHP 7.3

service apache2 restart

if ! [ -L /var/www/html ]; then
  rm /var/www/html/index.html
  rm -d /var/www/html
  ln -fs /vagrant /var/www/html
fi

rm /etc/apache2/sites-available/laravel.conf
touch /etc/apache2/sites-available/laravel.conf

echo "<VirtualHost *:80>" >> /etc/apache2/sites-available/laravel.conf
echo "    ServerName yourdomain.tld" >> /etc/apache2/sites-available/laravel.conf
echo "" >> /etc/apache2/sites-available/laravel.conf
echo "    ServerAdmin webmaster@localhost" >> /etc/apache2/sites-available/laravel.conf
echo "    DocumentRoot /var/www/html/public_html" >> /etc/apache2/sites-available/laravel.conf
echo "" >> /etc/apache2/sites-available/laravel.conf
echo "    <Directory /var/www/html/public_html>" >> /etc/apache2/sites-available/laravel.conf
echo "        AllowOverride All" >> /etc/apache2/sites-available/laravel.conf
echo "        Require all granted" >> /etc/apache2/sites-available/laravel.conf
echo "    </Directory>" >> /etc/apache2/sites-available/laravel.conf
echo "" >> /etc/apache2/sites-available/laravel.conf
echo "    ErrorLog ${APACHE_LOG_DIR}/error.log" >> /etc/apache2/sites-available/laravel.conf
echo "    CustomLog ${APACHE_LOG_DIR}/access.log combined" >> /etc/apache2/sites-available/laravel.conf
echo "</VirtualHost>" >> /etc/apache2/sites-available/laravel.conf
echo "" >> /etc/apache2/sites-available/laravel.conf

a2dissite 000-default.conf
a2ensite laravel.conf
a2enmod rewrite
service apache2 restart

systemctl enable apache2.service

apt-get install -y curl
apt-get install -y python-software-properties
apt-get install -y software-properties-common
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

apt-get -y autoclean
apt-get -y autoremove

echo "CREATE USER 'takenpla_takenplace2gaido'@'%' IDENTIFIED BY 'takenpla_takenplace2gaido';" >> db_setup.sql
echo "CREATE DATABASE takenpla_takenplace2gaido;" >> db_setup.sql
echo "GRANT ALL PRIVILEGES ON takenpla_takenplace2gaido.* TO 'takenpla_takenplace2gaido'@'%';" >> db_setup.sql
echo "FLUSH PRIVILEGES;" >> db_setup.sql

mysql < db_setup.sql

rm db_setup.sql

echo "alias art=\"php artisan\"" >> /home/vagrant/.bashrc
echo "" >> /home/vagrant/.bashrc
echo "alias mfs=\"php artisan migrate:fresh --seed\"" >> /home/vagrant/.bashrc
echo "" >> /home/vagrant/.bashrc
echo "alias tink=\"php artisan tinker\"" >> /home/vagrant/.bashrc
echo "" >> /home/vagrant/.bashrc
