# This is Rick Jerz's script to upgrade Moodle4 on his VPS server.
#   I could create a script to do everything in one step, but I prefer
#   to purposely force myself into a few smaller steps, just to make sure
#		I know what I am doing.
#
# In this script, I essentially prep the new moodle with components from the
#   "current" moodle, then do a backup of the "current", rename the "current",
#   and then move the newly preped moodle into position.
# Perhaps this is somewhat like "GIT," but I can control the plugins, and
# 	 more specifically, some "code tweaks" a little better with this method.
#
# Revised:
# 5/9/2023: Initial script.
# 5/26/24: Added fix for removing courseindex.php using SED.
# 5/27/24: Revised to use SED instead of file copying.
#
# Requirements:
#   Download the new moodle to a temporary folder (mdupcodedir).
#   Uncompress it and rename it to the production moodle name (mdupcodename).
#   Run this script.
#   Define the variables, below.
#      mdcodedir: Moodle location on web.
#      mdname: Folder and name of your production moodle.
#			 mdupcodedir: Location of the newest downloaded moodle.
#      mdupcodename: Folder name of the new moodle.
#      buscript: Location and name of your backup script.
#
#   This needs a backup script in place to do a backup before upgrading.
# 	A backup could be manually done.  If so, comment out the "source" line.
#
# Notes:
# The current Moodle is renamed to "moodlex" as a precaution.
# I tend to manually upgrade plugins on the current moodle before running this script.
#   Plugins are then copied into the "newest" moodle.

# clear
# Timer
((start = SECONDS))

# Today's Date
NOW=$(date +%F)

# Define variables for this upgrade script.
# "Home" Location of moodle folder
mdcodedir='/home/edugen/public_html/'
# Moodle application folder name.
mdname="moodle4"
# Location of the moodle that is downloaded and unpacked.
mdupcodedir='/home/edugen/moodle4_temp/'
# Folder name of the unpacked moodle, typically "moodle" but I prefer moodle4
#  as a precaution.
mdupcodename='moodle4'
# Location of your backup routine.
# I have kept the backup separate because sometimes I run it by itself.
buscript="/home/edugen/scripts/bu_m4-vs08-now.sh"

echo "------------------------------------------------------------------"
echo "Upgrade "$mdname" in "$mdcodedir" using "$mdupcodedir$mdupcodename
echo "------------------------------------------------------------------"
echo "Today's date is: "$NOW

# Verify that there is a new moodle folder ready to upgrade.
if [ ! -d "$mdupcodedir"$mdupcodename ]; then
	echo "$mdupcodedir"$mdupcodename" does not exist. Cannot continue!"
	echo
	exit
fi

# Move config.php
# ----------------------------------------
echo
echo "Move config.php"
cp $mdcodedir$mdname/config.php $mdupcodedir$mdupcodename

# Make the following "code" changes.
#   Note: You need to know what you are doing for code changes.
echo
echo "Code Changes"
echo "------------"

# Modify Boost so that the home icon goes to a web page external to moodle.
#   A direct URL to edu-gen.com.
echo "Modify Boost navbar.mustache file with direct URL to homepage."
sed -i 's`{{{ config.homeurl }}}`https://www.edu-gen.com`' $mdupcodedir$mdupcodename/theme/boost/templates/navbar.mustache

# Modify Boost to remove the courseindex block.
#     $courseindexopen = (get_user_preferences('drawer-open-index', true) == true);
# Note: In MacOS, you need the '.bak' in the sed command.
#   Since a ".bak" file is created, I remove the .bak file.
#		In UNIX (vs08) I use a more standard form of the SED command.
echo "Modify Boost drawers.php file to not display the courseindex."
sed -i "s/(get_user_preferences('drawer-open-index', true) == true)/false/" $mdupcodedir$mdupcodename/theme/boost/layout/drawers.php

# Done making code changes

# Copy all additional plugins.
# Note: Manually check and upgrade your additional plugins.
# I like to use a minimum of additional plugins.  I have four.
# You can add more plugins, but you need to know where they are located.
# ----------------------------------------
echo
echo "Copy additional plugins"
echo "-----------------------"

# Copy checklist plugin
echo "Copy checklist plugin."
cp -R $mdcodedir$mdname/mod/checklist $mdupcodedir$mdupcodename/mod/

# Copy collapsed topics plugin
echo "Copy Collapsed Topics plugin."
cp -R $mdcodedir$mdname/course/format/topcoll $mdupcodedir$mdupcodename/course/format/

# Copy sharing cart plugin
echo "Copy Sharing Cart plugin."
cp -R $mdcodedir$mdname/blocks/sharing_cart $mdupcodedir$mdupcodename/blocks/

# Copy configurable reports
echo "Copy Configurable Reports plugin."
cp -R $mdcodedir$mdname/blocks/configurable_reports $mdupcodedir$mdupcodename/blocks/

# Done copying plugins

# Call the moodle backup routine.
# Future improvement: Check to see if this routine exists.
# Comment-out this "source" line if you don't want to do a backup.
#
# Note: I quote this variable because the variable filename has a space character in it.
echo
echo "Backup the current Moodle4"
echo "--------------------------"
source "$buscript"

# Enable maintenance mode
#   Maintenance mode can be place before the backup, if desired.
echo "Enable maintenance mode"
php /$mdcodedir$mdname/admin/cli/maintenance.php --enable

# ----------------------------------------
# Now move the upgraded Moodle into htdocs
echo
echo "Begin moving the new upgraded Moodle into "$mdcodedir
echo "-----------------------------------------------------"

# 1. Delete the Moodlex copy, if it exits
echo "1. Delete the temporary "$mdcodedir$mdname"x, if it exits."
rm -rf $mdcodedir$mdname"x"

# 2. First, rename the old Moodle.  This is a precautionary step.
echo "2. Rename the current Moodle by appending an x to the end."
mv $mdcodedir$mdname $mdcodedir$mdname"x"

# 3. Now move the new moodle into htdocs
echo "3. Move the new moodle into "$mdcodedir
mv -i $mdupcodedir$mdupcodename $mdcodedir$mdname

# Disable maintenance mode
echo "Disable maintenance mode"
php /$mdcodedir$mdname/admin/cli/maintenance.php --disable
# Purge caches
echo "Purge caches"
php /$mdcodedir$mdname/admin/cli/purge_caches.php

echo
echo "The upgrade is completed."
echo
read -t 3 -p "Displaying the contents of "$mdcodedir" in 3 seconds, or press [Enter]."
echo ""
ls -alh $mdcodedir

((end = SECONDS)) # Used for overall timing
((duration_sec = end - start))
((duration_min = 1 + duration_sec / 60))

echo
echo "**********************   Finished!   **************************************************"
echo "Total duration = $duration_sec seconds. Under $duration_min minutes elapsed time for this process."
echo
echo "Last IMPORTANT step!"
echo "Log into Moodle to manually complete this upgrade."
echo
