#!/bin/bash

# Automatically update the database password and configuration files

# The list of Data that needs to be changed
# 1. MYSQL_ROOT_PASSWORD
# 2. MYSQL_PASSWORD
# 3. Domain name in database
# 4. Domain name, Mysql admin user password changed in configuration files
# 5. The password of admin panel
# 6. Flush the cache

read -p "Enter the old password for MYSQL_ROOT_PASSWORD: " OLD_MYSQL_ROOT_PASSWORD
# Connect to mysql and check if the password is correct
mysql -u root -p$OLD_MYSQL_ROOT_PASSWORD -e "show databases;" > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "The password is incorrect. Please try again."
    exit 1
fi
# 1. Modify MYSQL_ROOT_PASSWORD for localhost
mysql -u root -p$OLD_MYSQL_ROOT_PASSWORD -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '${MYSQL_ROOT_PASSWORD}';flush privileges;"
# is the password changed?
for i in {1..5}; do
    mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "show databases;" > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "Password changed successfully."
        break
    fi
    echo "Password not changed. Retrying in 3 seconds... ($i/5)"
    sleep 5
done
# Connect to mysql with new root password
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "show databases;" > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "The password is incorrect. Please try again."
    exit 1
fi
# Modify MYSQL_ROOT_PASSWORD for %
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '${MYSQL_ROOT_PASSWORD}';"

# 2. Modify MYSQL_PASSWORD for %
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "ALTER USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY '${MYSQL_PASSWORD}';"

# 3. Modify domain name in database
NEW_DOMAIN="${POD_NAMESPACE}-0.cloudclusters.net"
NEW_URL="https://${NEW_DOMAIN}/"
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "UPDATE magentodatabase.core_config_data SET value = \"${NEW_URL}\" WHERE path = 'web/unsecure/base_url';"

# 4. Modify domain name and mysql admin password in configuration files
sed -i "s/'password' => '[^']*'/'password' => '${MYSQL_PASSWORD}'/" /cloudclusters/magento/app/etc/env.php
sed -i "/'downloadable_domains'/,/],/c\    'downloadable_domains' => [\n        '${NEW_DOMAIN}'\n    ]," /cloudclusters/magento/app/etc/env.php

# 5. Modify the password of admin panel
EMAIL=$(mysql -u root -p"$MYSQL_ROOT_PASSWORD" -D magentodatabase -se "SELECT email FROM admin_user WHERE username='admin';")
FIRSTNAME=$(mysql -u root -p"$MYSQL_ROOT_PASSWORD" -D magentodatabase -se "SELECT firstname FROM admin_user WHERE username='admin';")
LASTNAME=$(mysql -u root -p"$MYSQL_ROOT_PASSWORD" -D magentodatabase -se "SELECT lastname FROM admin_user WHERE username='admin';")
php /cloudclusters/magento/bin/magento admin:user:create --admin-user=admin --admin-password=$MAGENTO_PASSWORD --admin-email=$EMAIL --admin-firstname=$FIRSTNAME --admin-lastname=$LASTNAME

# 6. Flush the cache
/cloudclusters/magento/bin/magento cache:clean
/cloudclusters/magento/bin/magento cache:flush

echo -e '\e[32mConfiguration updated successfully!\e[0m'
