#!/bin/bash

#Take Arguments
PHPMYADMIN_DOWNLOAD_LINK="https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip"
BLOWFISH_SECRET=""
MYSQL_SERVER_ADDRESS="mysql"

#Upgrade system
apt update
apt upgrade -y

#Install Prerequisites
apt install -y vim wget zip

#disable interactive mode
export DEBIAN_FRONTEND=noninteractive

#Install apache+php
apt install -y apache2 php php-mysqli php-mbstring

#Clean install
apt autoclean

#Install PHP My Admin
cd /var/www/html
rm index.html
wget $PHPMYADMIN_DOWNLOAD_LINK
unzip phpMyAdmin-*
mv phpMyAdmin-*/* ./
rm -rf phpMyAdmin-*

#Configure PHPMyAdmin
cp config.sample.inc.php config.inc.php
sed -i "s|cfg\['blowfish_secret'\] = ''|cfg['blowfish_secret'] = '$BLOWFISH_SECRET'|" /var/www/html/config.inc.php
sed -i "s|'localhost'|'$MYSQL_SERVER_ADDRESS'|" /var/www/html/config.inc.php

#create TMP directory and set rights on it
mkdir tmp
chown www-data:www-data tmp/
chmod 700 tmp/

#Restart apache2 service
service apache2 restart