#!/bin/bash
# Change db passwords originally set in init script on first run
# rootpass arg sort of half works

source ../.env

if [[ $1 == "rootpass" ]] ; then
	echo "New:"$DOTENV_MYSQL_ROOT_PASSWORD"  Old:"$DOTENV_MYSQL_ROOT_PASSWORD_OLD
	echo "Changing root db passwords in 5 seconds..."
	sleep 6
	docker-compose exec db mariadb --database=mysql -uroot -p$DOTENV_MYSQL_ROOT_PASSWORD_OLD -e "ALTER USER 'root' IDENTIFIED BY '"$DOTENV_MYSQL_ROOT_PASSWORD"';"
	docker-compose exec db mariadb --database=mysql -uroot -p$DOTENV_MYSQL_ROOT_PASSWORD -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '"$DOTENV_MYSQL_ROOT_PASSWORD"';"
	docker-compose exec db mariadb --database=mysql -uroot -p$DOTENV_MYSQL_ROOT_PASSWORD -e "FLUSH PRIVILEGES;"
	exit 0
fi


echo "Changing app db passwords in 5 seconds..."
sleep 6

# Flask
docker-compose exec db mariadb --database=mysql -uroot -p$DOTENV_MYSQL_ROOT_PASSWORD_OLD -e "ALTER USER 'flasku'@'backend.backnet' IDENTIFIED BY '"$DOTENV_MYSQL_FLASK_PASSWORD"';"

# Gitea
docker-compose exec db mariadb --database=mysql -uroot -p$DOTENV_MYSQL_ROOT_PASSWORD_OLD -e "ALTER USER 'gitea'@'gitea.backnet' IDENTIFIED BY '"$DOTENV_MYSQL_GITEA_PASSWORD"';"


docker-compose exec db mariadb --database=mysql -uroot -p$DOTENV_MYSQL_ROOT_PASSWORD_OLD -e "FLUSH PRIVILEGES;"


