#!/bin/bash

DATA="$(wget https://www.phpmyadmin.net/home_page/version.txt -q -O-)"
PMA_DL_URL="$(echo $DATA | cut -d ' ' -f 3)"
PMA_VERSION="$(echo $DATA | cut -d ' ' -f 1)"
SETUPTOKEN=$(md5sum<<<$RANDOM | awk '{print $1}')
REQUIRED_OS="buster"
TEMP_PATH="/tmp"
TEMP_DIR="$TEMP_PATH/PMA_Setup-$SETUPTOKEN/$PMA_VERSION"
SETUP_DIR="$TEMP_PATH/PMA_Setup-$SETUPTOKEN"
IP_ADDR=$(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1)


clear
echo -e "\e[1;36mInitializing setup... \e[0m"
mkdir -p $TEMP_DIR
sleep 3
clear


echo -e "\e[1;33m##############################"
echo -e "\e[1;33m# phpMyAdmin shell installer #"
echo -e "\e[33m##############################\e[0m"
echo ""
echo ""

sleep 0.4
echo "Getting current PMA Version..."
sleep 1.5
echo "This setup is going to install phpMyAdmin version $PMA_VERSION"

echo ""

echo -e "\e[1;33mThe following packages are required to run phpMyAdmin:"
echo -e "\e[1;33m------------------------------------------------------\e[0m"
echo "1.) apache2"
echo "2.) php7.3, php7.3-common, php7.3-cli, php7.3-zip,"
echo "    php7.3-pdo, php7.3-mysql, php7.3-mysqli,"
echo "    php7.3-mbstring, php7.3-bcmath, php7.3-geoip,"
echo "    php7.3-apcu, php7.3-xml, php7.3-json, php7.3-curl,"
echo "3.) sudo, curl, nano, git, zip, unzip, lsb-release,"
echo "    ca-certificates, apt-transport-https, wget"

echo ""

echo -e "\e[1;33mThe following packages can be installed optionally:"
echo -e "\e[1;33m---------------------------------------------------\e[0m"
echo "1.) mariadb-server"
echo "2.) mariadb-client"
echo "3.) neofetch"

echo ""
echo ""

read -p "Do you want to continue? [Y/n] " A
echo ""

if [[ $A == "" ]];
then
	A="Y"
fi

if [ $A == "Y" -o $A == "y" ];
then
	echo -e "\e[1;36mPlease wait...\e[0m"
	echo ""

	echo -e "\e[1;33m--->> Upgrading & updating OS... <<--- \e[0m"
	apt update && apt upgrade -y
	echo ""
	sleep 1

	echo -e "\e[1;33m--->> Checking OS compatibility... <<--- \e[0m"
	apt install lsb-release -y
	echo ""
	if [ $(lsb_release -sc) == $REQUIRED_OS ];
	then
		echo -e "\e[1;32mYour OS is compatible! \e[0m";
		echo ""
		sleep 1

		echo -e "\e[1;33m--->> Setting-up basic packages...  <<--- \e[0m"
		apt install -y sudo curl nano git zip unzip ca-certificates apt-transport-https wget
		echo ""
		sleep 1

		read -p "Do you want to install the optional packages? [Y/n] " B
		echo ""
		if [[ $B == "" ]];
		then
			B="Y"
		fi
		if [ $B == "y" -o $B == "Y" ];
		then
			echo -e "\e[1;36mPlease wait...\e[0m"
			echo ""

			echo -e "\e[1;33m--->> Setting-up optional packages... <<--- \e[0m"
        		apt install -y neofetch
			apt install -y mariadb-server mariadb-client
        		echo ""
        		sleep 1

		fi

		echo -e "\e[1;33m--->> Setting-up required packages... <<--- \e[0m"
		apt install -y apache2
		apt install -y php7.3 php7.3-common php7.3-cli php7.3-pdo php7.3-mysql php7.3-mysqli php7.3-mbstring php7.3-bcmath php7.3-geoip php7.3-apcu php7.3-xml php7.3-json php7.3-curl php7.3-zip
		echo ""
		sleep 1

		echo -e "\e[1;33m--->> Downloading phpMyAdmin... <<--- \e[0m"
		wget https://files.phpmyadmin.net/phpMyAdmin/${PMA_VERSION}/phpMyAdmin-${PMA_VERSION}-all-languages.tar.gz -P $TEMP_DIR
		echo ""
		sleep 1

		echo -e "\e[1;33m--->> Unpacking phpMyAdmin... <<--- \e[0m"
		echo -e "\e[1;36mPlease wait...\e[0m"
		sleep 1.5
		tar xvf $TEMP_DIR/phpMyAdmin-${PMA_VERSION}-all-languages.tar.gz -C $TEMP_DIR
		sleep 1
		echo ""

		echo -e "\e[1;33m--->> Setting-up phpMyAdmin... <<--- \e[0m"
		echo -e "\e[1;36mPlease wait...\e[0m"
		sleep 1
		mv $TEMP_DIR/phpMyAdmin-*/ /usr/share/phpmyadmin
		mkdir -p /var/lib/phpmyadmin/tmp
		chown -R www-data:www-data /var/lib/phpmyadmin
		mkdir /etc/phpmyadmin/
		sleep 1
		wget https://cdn.kleine-vorholt.eu/installers/lamp/deb10/config.inc.sample -P $TEMP_DIR
		mv $TEMP_DIR/config.inc.sample $TEMP_DIR/config.inc.php
		cp $TEMP_DIR/config.inc.php /usr/share/phpmyadmin/config.inc.php
		sleep 1
		wget https://cdn.kleine-vorholt.eu/installers/lamp/deb10/phpmyadmin.conf -P $TEMP_DIR
		echo ""
		read -p "On which request path should phpMyAdmin be enabled? (without last slash!) [/phpmyadmin]: " PMA_PATH
		echo -e "\e[1;36mPlease wait...\e[0m"
		if [[ $PMA_PATH == "" ]];
		then
			PMA_PATH="/phpmyadmin"
		fi
		printf "Alias $PMA_PATH /usr/share/phpmyadmin" >> $TEMP_DIR/phpmyadmin.conf
		sleep 1
		echo -e "\e[1;32mOK! \e[0m";
		mv $TEMP_DIR/phpmyadmin.conf /etc/apache2/conf-available/phpmyadmin.conf
		a2enconf phpmyadmin
		echo ""
		sleep 1


		read -p "Do you want to create a user to access phpMyAdmin as root? [Y/n] " C
		echo ""
		if [[ $C == "" ]];
		then
			C="Y"
		fi
		if [ $C == "Y" -o $C == "y" ];
		then
			echo -e "\e[1;36mPlease wait...\e[0m"
			echo ""
			sleep 1.5
			echo -e "\e[1;33m--->> phpMyAdmin user creation <<--- \e[0m"
			echo ""
			read -p "Please enter a username [pmaroot]: " PMA_ROOT_USER
			read -s -p "Please enter a password [$SETUPTOKEN]: " PMA_ROOT_PASS
			if [[ $PMA_ROOT_USER == "" ]];
			then
				PMA_ROOT_USER="pmaroot"
			fi
			if [[ $PMA_ROOT_PASS == "" ]];
			then
				PMA_ROOT_PASS=$SETUPTOKEN
			fi
			printf "CREATE USER '$PMA_ROOT_USER'@'localhost' IDENTIFIED BY '$PMA_ROOT_PASS'; " >> $TEMP_DIR/superuserSQL.sql
			printf "GRANT ALL PRIVILEGES ON *.* TO '$PMA_ROOT_USER'@'localhost' WITH GRANT OPTION; " >> $TEMP_DIR/superuserSQL.sql
			printf "FLUSH PRIVILEGES; " >> $TEMP_DIR/superuserSQL.sql
			echo ""
			echo -e "\e[1;36mPlease wait. This can take a while... \e[0m"
			sleep 6
			mysql -q < $TEMP_DIR/superuserSQL.sql
			rm $TEMP_DIR/superuserSQL.sql
			sleep 2
			echo -e "\e[1;32mOK! \e[0m";
			sleep 1
			echo ""
		fi

		echo -e "\e[1;33m--->> Restarting services... <<--- \e[0m"
		echo -e "\e[1;36mPlease wait...\e[0m"
		sleep 2
		/etc/init.d/apache2 restart
		echo ""
		sleep 2
		
		echo -e "\e[1;33m--->> Downloading uninstaller... <<--- \e[0m"
		echo -e "\e[1;36mPlease wait...\e[0m"
		mkdir -p /opt/pmainstaller
		wget https://cdn.kleine-vorholt.eu/installers/lamp/deb10/uninstall -P /opt/pmainstaller
		chmod 755 /opt/pmainstaller/uninstall
		sleep 2
		echo ""

		echo -e "\e[1;33m--->> Cleaning up setup files... <<--- \e[0m"
		echo -e "\e[1;36mPlease wait...\e[0m"
		rm -R $SETUP_DIR
		sleep 4
		echo ""

		echo -e "\e[1;32mThanks for using our phpMyAdmin installer! :) \e[0m"
		echo -e "\e[1;30mMore information: https://kleine-vorholt.net/ \e[0m"
		echo ""
		echo -e "\e[1;33mYour phpMyAdmin is now live! : http://$IP_ADDR$PMA_PATH/ \e[0m"
		echo -e "\e[1;33mIf you wan't to uninstall this software run 'sh /opt/pmainstaller/uninstall' \e[0m"

	else
		echo -e "\e[1;31mYour OS is incompatible! (Debian $(lsb_release -sc) ≠ Debian $REQUIRED_OS) \e[0m";
		rm -R $SETUP_DIR
fi

else
	echo "Abort."
	rm -R $SETUP_DIR
	exit 1
fi

