#!/bin/bash

#=====================================================================#
# 																	  #
# Shell script to quickly update the files of a ModX Revo Instance	  #
# Published under Creative Commons License							  #
# 																	  #
# Author: Claudine WEBER											  #
# Email: claudine.weber@gmail.com									  #
# Website: www.claudine-weber.lu									  #
# 																	  #
# Created: 10.11.2011												  #
# 																	  #
# Modified: 15.01.2012												  #
# Version: 4.0.1													  #
# 																	  #
# Changed: Bugfix on line 221 checking if assets folder exists        #
#  	                          instead of does not exist               #
# 																	  #
#=====================================================================#

# Variable declarations. These variables have a global scope as we don't use functions
ASS_DIR=assets
ASS_DIR_OLD=assets_old
CONN_DIR=connectors
CONN_DIR_OLD=connectors_old
CORE_DIR=core
CORE_DIR_OLD=core_old
MAN_DIR=manager
MAN_DIR_OLD=manager_old
INDEX_FILE=index.php
INDEX_FILE_OLD=index.php.old
CORE_CONFIG_FILE=config.core.php
CORE_CONFIG_FILE_OLD=config.core.php.old

COMP_DIR=components
CONF_DIR=config
CONF_FILE=config.inc.php

MAC=__MACOSX
DS=.DS_Store

# Text output colors
txtred=$(tput setaf 1) #red
txtgrn=$(tput setaf 2) #green
txtylw=$(tput setaf 3) #yellow
txtblu=$(tput setaf 4) #blue
txtrst=$(tput sgr0) #reset to default

echo "${txtblu}"
echo -e "#####################################################################"
echo -e "#                                                                   #"
echo -e "# Shell script to quickly update the files of a ModX Revo Instance  #"
echo -e "# Published under Creative Commons License                          #"
echo -e "#                                                                   #"
echo -e "# Author: Claudine WEBER                                            #"
echo -e "# Email: claudine.weber@gmail.com                                   #"
echo -e "# Website: www.claudine-weber.lu                                    #"
echo -e "#                                                                   #"
echo -e "# Created: 10.11.2011                                               #"
echo -e "#                                                                   #"
echo -e "# Modified: 15.01.2012						    #"
echo -e "# Version: 4.0.1					    	    #"
echo -e "# 							            #"
echo -e	"# Changed: Bugfix on line 221 checking if assets folder exists      #"
echo -e "#  	                          instead of does not exist         #"
echo -e "#                                                                   #"
echo -e "#####################################################################"
echo "${txtrst}"

#==========================RELEASE CHECK BLOCK=========================#
# User input needed to determine these variable values
echo -e "Please enter the name of the newly uploaded release (e.g. modx-2.1.5-pl) without appending the file extension:"
read -e release
# Check if [release].zip was really uploaded to the server
echo -e "Checking if the file \"$release.zip\" exists ..."
if [ -e $release.zip ]
	then
		echo -e "${txtylw}The requested ModX release archive was found. Continuing update ...\n${txtrst}"
	else
		echo -e "${txtred}Ups ... I couldn't find the requested ModX release archive. Please upload it via SFTP.\n\n${txtrst}"
		exit -1
fi

#===============START RENAMING EXISTING FILES AND FOLDERS==============#
# Renaming the folder "assets" to "assets_old" if exists
echo -e "Renaming $ASS_DIR to $ASS_DIR_OLD ..."
if [ -d $ASS_DIR ]
	then
		mv $ASS_DIR $ASS_DIR_OLD
		echo -e "${txtylw}The folder \"$ASS_DIR\" was successfully renamed!\n${txtrst}"
	else
		echo -e "${txtred}Ups ... I couldn't find the folder \"$ASS_DIR\"!\n${txtrst}"
		ls -la a*
		exit -1
fi

# Renaming the folder "connectors" to "connectors_old" if exists
echo -e "Renaming \"$CONN_DIR\" to \"$CONN_DIR_OLD\" ..."
if [ -d $CONN_DIR ]
	then
		mv $CONN_DIR $CONN_DIR_OLD
		echo -e "${txtylw}The folder \"$CONN_DIR\" was successfully renamed!\n${txtrst}"
	else
		echo -e "${txtred}Ups ... I couldn't find the folder \"$CONN_DIR\"!\n${txtrst}"
		ls -la c*
		exit -1
fi

# Renaming the folder "core" to "core_old" if exists
echo -e "Renaming \"$CORE_DIR\" to \"$CORE_DIR_OLD\" ..."
if [ -d $CORE_DIR ]
	then
		mv $CORE_DIR $CORE_DIR_OLD
		echo -e "${txtylw}The folder \"$CORE_DIR\" was successfully renamed!\n${txtrst}"
	else
		echo -e "${txtred}Ups ... I couldn't find the folder \"$CORE_DIR\"!\n${txtrst}"
		ls -la c*
		exit -1
fi

# Renaming the folder "manager" to "manager_old" if exists
echo -e "Renaming \"$MAN_DIR\" to \"$MAN_DIR_OLD\" ..."
if [ -d $MAN_DIR ]
	then
		mv $MAN_DIR $MAN_DIR_OLD
		echo -e "${txtylw}The folder \"$MAN_DIR\" was successfully renamed!\n${txtrst}"
	else
		echo -e "${txtred}Ups ... I couldn't find the folder \"$MAN_DIR\"!\n${txtrst}"
		ls -la m*
		exit -1
fi

# Renaming the file "index.php" to "index.php.old"
echo -e "Renaming the file \"$INDEX_FILE\" to \"$INDEX_FILE_OLD\" ..."
if [ -e $INDEX_FILE ]
	then
		mv $INDEX_FILE $INDEX_FILE_OLD
		echo -e "${txtylw}The file \"$INDEX_FILE\" was successfully renamed!\n${txtrst}"
	else
		echo -e "${txtred}Ups ... I couldn't find the file \"$INDEX_FILE\"!\n${txtrst}"
		ls -la *i
		exit -1
fi

# Renaming the file "config.core.php" to "config.core.php.old"
echo -e "Renaming the file \"$CORE_CONFIG_FILE\" to \"$CORE_CONFIG_FILE_OLD\" ..."
if [ -e $CORE_CONFIG_FILE ]
	then
		mv $CORE_CONFIG_FILE $CORE_CONFIG_FILE_OLD
		echo -e "${txtylw}The file \"$CORE_CONFIG_FILE\" was successfully renamed!\n${txtrst}"
	else
		echo -e "${txtred}Ups ... I couldn't find the file \"$CORE_CONFIG_FILE\"!\n${txtrst}"
		ls -la c*
		exit -1
fi

#========UNZIPPING ARCHIVE AND REMOVING UNUSED FILES AND FOLDERS=======#
# Unzipping [release].zip and removing unused files and folders
echo -e "Unzipping \"$release.zip\" ..."
unzip -o $release.zip
echo -e "${txtylw}The archive has been successfully extracted!\n${txtrst}"

if [ -d $MAC ]
	then
		echo -e "Removing unused folder \"$MAC\" ..."	
		rm -rf $MAC 
		echo -e "${txtylw}The file \"$MAC\" was successfully removed!\n${txtrst}"
fi

if [ -e $DS ]
	then
		echo -e "Removing unused file \"$DS\" ..."
		rm $DS
		echo -e "${txtylw}The file \"$DS\" was successfully removed!\n${txtrst}"
fi

echo -e "Removing the archive file ..."
rm $release.zip
echo -e "${txtylw}The file \"$release.zip\" was successfully removed!\n${txtrst}"

#==================MOVING FILES AND FOLDERS INTO PLACE=================#
# Moving the content of the folder [release] one level up
echo -e "Moving the content of the folder \"$release\" one level up ..."
cd $release
mv * ..
echo -e "Removing the now empty folder \"$release\" ..."
cd ..
rm -rf $release
echo -e "${txtylw}The folder \"$release\" was successfully removed!\n${txtrst}"

#=============MOVING REQUIRED FILES AND FOLDERS INTO PLACE=============#
# Moving the configuration file into place
echo -e "Moving \"$CONF_FILE\" from \"$CORE_DIR_OLD/$CONF_DIR/\" to \"$CORE_DIR/$CONF_DIR/\""
if [ -e $CORE_DIR_OLD/$CONF_DIR/$CONF_FILE ]
	then
		mv $CORE_DIR_OLD/$CONF_DIR/$CONF_FILE $CORE_DIR/$CONF_DIR/
		if [ -e $CORE_DIR/$CONF_DIR/$CONF_FILE ]
		then
			echo -e "${txtylw}The file \"$CONF_FILE\" was successfully moved!\n${txtrst}"
		else
			echo -e "${txtred}Ups ... I encountered a problem while moving \"$CONF_FILE\"!\n${txtrst}"
			exit -1
		fi
	else
		echo -e "${txtred}Ups ... I couldn't find the file \"$CONF_FILE\" in \"$CORE_DIR_OLD/$CONF_DIR\"!\n${txtrst}"
		exit -1
fi

# Moving the content of the core_old/components folder into core/components
echo -e "Moving the contents of the \"$CORE_DIR_OLD/$COMP_DIR\" folder to \"$CORE_DIR/$COMP_DIR/\""
if [ -d $CORE_DIR_OLD/$COMP_DIR ]
	then
		if [ ! -d $CORE_DIR/$COMP_DIR ]
			then
				mkdir $CORE_DIR/$COMP_DIR
		fi
		mv $CORE_DIR_OLD/$COMP_DIR/* $CORE_DIR/$COMP_DIR/
		echo -e "${txtylw}The contents of the folder \"$CORE_DIR_OLD/$COMP_DIR\" where successfully moved to \"$CORE_DIR/$COMP_DIR\"!\n${txtrst}"
	else
		echo -e "${txtred}Ups ... I couldn't find the folder \"$CORE_DIR_OLD/$COMP_DIR\"!\n${txtrst}"
		exit -1
fi

# Moving the content of the assets_old folder into assets
echo -e "Moving \"$ASS_DIR_OLD\" to \"$ASS_DIR\" ..."
if [ ! -d $ASS_DIR ]
	then 
		mv $ASS_DIR_OLD $ASS_DIR
		echo -e "${txtylw}The folder \"$ASS_DIR_OLD\" has been successfully moved to \"$ASS_DIR\"!\n${txtrst}"
	else
		mv $ASS_DIR_OLD/* $ASS_DIR/
		echo -e "${txtylw}The contents of \"$ASS_DIR_OLD\" have been successfully copied to \"$ASS_DIR\"!\n${txtrst}"
fi

#==============REMOVING THE NOW UNUSED FILES AND FOLDERS===============#
# Removing the now unused folder "assets_old"
echo -e "Removing the now unused folder \"$ASS_DIR_OLD\" ..."
if [ -d $ASS_DIR ]
	then
		if [ -d $ASS_DIR_OLD ]
			then
				rm -rf $ASS_DIR_OLD
				echo -e "${txtylw}The folder \"$ASS_DIR_OLD\" has been successfully removed!\n${txtrst}"
		fi
	else
		echo -e "${txtred}Ups ... I couldn't find the folder \"$ASS_DIR\". Please crosscheck before running setup!\n${txtrst}"
fi

# Removing the now unused folder "core_old"
if [ -d $CORE_DIR ]
	then
		echo -e "Removing the now unused folder \"$CORE_DIR_OLD\" ..."
		if [ -d $CORE_DIR_OLD ]
			then
				rm -rf $CORE_DIR_OLD
				echo -e "${txtylw}The folder \"$CORE_DIR_OLD\" has been successfully removed!\n${txtrst}"
		fi
	else
		 echo -e "${txtred}Ups ... I couldn't find the folder \"$CORE_DIR\". Please crosscheck before running setup!\n${txtrst}"
fi

# Removing the now unused folder "manager_old"
if [ -d $MAN_DIR ]
	then
		echo -e "Removing the now unused folder \"$MAN_DIR_OLD\" ..."
		if [ -d $MAN_DIR_OLD ]
			then
				rm -rf $MAN_DIR_OLD
				echo -e "${txtylw}The folder \"$MAN_DIR_OLD\" has been successfully removed!\n${txtrst}"
		fi
	else
		echo -e "${txtred}Ups ... I couldn't find the folder \"$MAN_DIR\". Please crosscheck before running setup!\n${txtrst}"
fi

# Removing the now unused folder "connectors_old"
if [ -d $CONN_DIR ]
	then	
		echo -e "Removing the now unused folder \"$CONN_DIR_OLD\" ..."
		if [ -d $CONN_DIR_OLD ]
			then
				rm -rf $CONN_DIR_OLD
				echo -e "${txtylw}The now unused folder \"$CONN_DIR_OLD\" has been successfully removed!\n${txtrst}"
			fi
	else
       	echo -e "${txtred}Ups ... I couldn't find the folder \"$MAN_DIR\". Please crosscheck before running setup!\n${txtrst}"
fi

# Removing the now unused file "index.php.old"
if [ -e $INDEX_FILE ]
	then
		echo -e "Removing the now unused file \"$INDEX_FILE_OLD\" ..."
		if [ -e $INDEX_FILE_OLD ]
			then
				rm $INDEX_FILE_OLD
			echo -e "${txtylw}The now unused file \"$INDEX_FILE_OLD\" has been successfully removed!\n${txtrst}"
		fi
	else
		echo -e "${txtred}Ups ... I couldn't find the file \"$INDEX_FILE\". Please crosscheck before running setup!\n${txtrst}"
fi

# Removing the now unused file "config.core.php"
if [ -e $CORE_CONFIG_FILE ]
	then
		echo -e "Removing the now unused file \"$CORE_CONFIG_FILE_OLD\" ..."
		if [ -e $CORE_CONFIG_FILE_OLD ]
			then
				rm $CORE_CONFIG_FILE_OLD
				echo -e "${txtylw}The now unused file \"$CORE_CONFIG_FILE_OLD\" has been successfully removed!\n${txtrst}"
			fi
	else
		echo -e "${txtred}Ups ... I couldn't find the file \"$INDEX_FILE\". Please crosscheck before running setup!\n${txtrst}"
fi

#=====================CHECKING FOR .HTACCESS FILE=====================#
if [ -e ht.access ]
	then
		if [ -e .htaccess ]
			then
				echo -e "Removing the unused file \"ht.access\"..."
				rm ht.access
				echo -e "${txtylw}The unused file \"ht.access\" has been successfully removed!\n${txtrst}"
			else
				echo -e "Moving the file \"ht.access\" to \".htaccess\" ..."
				mv ht.access .htaccess
				echo -e "${txtylw}The file \"ht.access\" was successfully moved!\n${txtrst}"
			fi
	else
		echo -e "${txtred}Ups ... I couldn't find the file \"ht.access\". Please crosscheck before running setup!\n${txtrst}"
fi

#=====================SETTING THE RIGHTS FOR THE FOLDERS=====================#
if [ -f mus_revo_rights_v1.sh ]
	then
		echo -e "Setting the rights for the file \"mus_revo_rights_v1.sh\"..."
		chmod 777 mus_revo_rights_v1.sh
		echo -e "${txtylw}The rights for \"mus_revo_rights_v1.sh\" has been successfully set!${txtrst}"
		./mus_revo_rights_v1.sh
	else
		echo -e "${txtred}Ups ... I couldn't find the file \"mus_revo_rights_v1.sh\". Please upload it via ftp and execute it manually!\n${txtrst}"
fi

#============================FINAL MESSAGE=============================#
echo -e "${txtgrn}"
echo -e "#####################################################################"
echo -e "#       Thank you for using MUS - my ModX Revo Update Script!       #"
echo -e "#                                                                   #"
echo -e "# All file and folders have been successfully updated.              #"
echo -e "# Please open your webbrowser and run install by typing             #"
echo -e "# \"http://[your_site]/setup\" in the navigation bar!                 #"
echo -e "#                                                                   #"
echo -e "# For further questions, please contact me using the form on my     #"
echo -e "# website: www.claudine-weber.lu or by sending an email to          #"
echo -e "# info@claudine-weber.lu                                            #"
echo -e "#                                                                   #"
echo -e "#####################################################################"
echo -e "${txtrst}"

