# 1. CHANGE MYSQL PASSWORD
echo "# 1. change MYSQL ROOT PASSWORD AND ADMIN PASSWORD"
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}';"

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


# 2. CHANGE PANEL URL
echo "# 2. Modify domain in database and config file"
NEW_DOMAIN="${POD_NAMESPACE}-0.cloudclusters.net"
NEW_URL="https://${NEW_DOMAIN}"
MOODLE_PATH="/cloudclusters/moodle/html"
read -e -p "Enter the old_domain (example: https://merrylandfarm.com not https://merrylandfarm.com/ ) " OLD_DOMAIN
#read -e -p "Enter the old mysql admin password (example: OZmNLAU2) " OLD_MYSQL_PASSWORD
sed -i "s/\$CFG->dbpass\s*=\s*'[^']*'/\$CFG->dbpass = '$MYSQL_PASSWORD'/" ${MOODLE_PATH}/config.php
#sed -i "s/\$CFG->dbpass    = '${OLD_MYSQL_PASSWORD}';/\$CFG->dbpass    = '${MYSQL_PASSWORD}';/" ${MOODLE_PATH}/config.php
#sed -i "s|\$CFG->wwwroot = '${OLD_DOMAIN}';|\$CFG->wwwroot = '${NEW_URL}';|" "${MOODLE_PATH}/config.php"
sed -i "s|^\$CFG->wwwroot\s*=.*|\$CFG->wwwroot   = '${NEW_URL}';|" ${MOODLE_PATH}/config.php

if grep -q ${MYSQL_PASSWORD} ${MOODLE_PATH}/config.php && grep -q ${NEW_URL} ${MOODLE_PATH}/config.php; then
     echo "Configuration file update successful!"
else
     echo "Configuration file update failed. Please check manually!"
     exit 1
fi

gosu www-data /usr/bin/php ${MOODLE_PATH}/admin/tool/replace/cli/replace.php --search=//${OLD_DOMAIN} --replace=//${NEW_URL} --shorten=false

# If you want to change the password of the UI panel, execute the following command.
# gosu www-data  /usr/bin/php ${MOODLE_PATH}/admin/cli/reset_password.php  --ignore-password-policy 

# 3. clean cache and restart service
echo "3. clean cache and restart service"
gosu www-data /usr/bin/php  $MOODLE_PATH/admin/cli/purge_caches.php
supervisorctl restart all
supervisorctl status
