#!/bin/bash

# ======================================================#
# Shell Script to update the files of a ModX-Instance	#
# Published under Creative Commons Licence				#
#														#
# Author: Claudine WEBER								#
# Email: claudine.weber@gmail.com						#
# Website: www.claudine-weber.lu						#
# 														#
# Date : 25.10.2009										#
# Modified : 28.06.2011									#
# Version : 5.0											#
# Checking if modx contains non standard language files	#
# ======================================================#

#Global Variables declaration. Local vars are not needed as we don't write functions
ARCH_FILE=Archive.zip
ASS=assets
ASS_OLD=assets_old
MAN=manager
MAN_OLD=manager_old
EASY_DIR=easynewsletter
GAL_DIR=gallery
CONF_FILE=config.inc.php
LANG_NUM=0
MAC=__MACOSX
DS=.DS_Store

echo -e "#=======================================================#"
echo -e "# ModX Evo File Update Script				#"
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 "# Date : 25.10.2009					#"
echo -e "# Modified : 28.06.2011					#"
echo -e "# Version : 5.0						#"
echo -e "# Checking if modx contains non standard language files #"
echo -e "# ======================================================#\n"

#======================== BEGIN ARCHIVE CHECK BLOCK ===============================
# Check if "Archive.zip" was uploaded to the Server (Does file exist?)
echo "Checking if the file \"$ARCH_FILE\" exists ..."       
if [ ! -f $ARCH_FILE ];
	then
		echo -e "The file \"$ARCH_FILE\" was not found. Please upload it via FTP!\n"
		exit 1
	else
		echo -e "The file \"$ARCH_FILE\" was found. Continuing ModX File Update ...\n"
fi

#======================= BEGIN RENAMING OF EXISTING FOLDERS========================
#Renaming the folder "assets" to "assets_old"
echo -e "Renaming \"$ASS\" to \"$ASS_OLD\" ..."
if [ -d $ASS ];
	then 
		mv $ASS $ASS_OLD
		echo -e "Folder \"$ASS\" successfully renamed!\n"
	else
		echo -e "Folder \"$ASS\" could not be renamed!\n"
		ls
		exit 1
fi

#Renaming the folder "manager" to "manager_old"
echo -e "Renaming \"$MAN\" to \"$MAN_OLD\" ..."
if [ -d $MAN ];
	then 
		mv $MAN $MAN_OLD
		echo -e "Folder \"$MAN\" successfully renamed!\n"
	else
		echo -e "Folder \"$MAN\" could not be renamed!\n"
		ls
		exit 1
fi

#====================== UNZIP AND REMOVING OF NON NEEDED FILES ====================
# Unzipping "Archive.zip" and removing unused files and folders
echo -e "Unzipping \"$ARCH_FILE\" ..."
unzip -o $ARCH_FILE
echo -e "Removing unused files ..."
if [ -d $MAC ]
	then 
		echo -e "Removing unused folder \"$MAC\" ..."
		rm -rf $MAC
fi
if [ -f $DS ]
	then 
		echo -e "Removing unused file \"$DS\" ..."
		rm $DS
fi
echo -e "Removing Archive file ..."
rm $ARCH_FILE

#======================= MOVING IMPORTANT FILES & FOLDERS =========================
#Moving the directory "assets_old/modules/easynewsletter" to "assets/modules/easynewsletter" 
echo -e "Moving the \"Easynewsletter\"- Module from \"$ASS_OLD/modules/\" to \"$ASS/modules/\"!"
if [ -d $ASS_OLD"/modules/"$EASY_DIR ]
	then 
		mv $ASS_OLD"/modules/"$EASY_DIR $ASS"/modules/" 
		echo -e "The \"Easynewsletter\"- Module has been successfully moved!\n"
	else 
		echo -e "This ModX-Instance has no Gallery-Module!\n"
fi

#Moving the directory "assets_old/modules/gallery" to "assets/modules/gallery" 
echo -e "Moving the \"Gallery\"- Module from \"$ASS_OLD/modules/\" to \"$ASS/modules/\"!"
if [ -d $ASS_OLD"/modules/"$GAL_DIR ]
	then 
		mv $ASS_OLD"/modules/"$GAL_DIR $ASS"/modules/" 
		echo -e "The \"Gallery\"- Module has been successfully moved!\n"
	else 
		echo -e "This ModX-Instance has no Gallery-Module!\n"
fi

#Moving the file "manager_old/includes/config.inc.php" to "manager/includes/config.inc.php" 
echo -e "Moving the Configuaraion file from \"$MAN_OLD/includes/\" to \"$MAN/includes/\"!"
if [ -f $MAN_OLD"/includes/"$CONF_FILE ]
	then 
		mv $MAN_OLD"/includes/"$CONF_FILE $MAN"/includes/"
			echo -e "The Configuration File has been successfully moved!\n"
	else 
		echo -e "This ModX-Instance has no Configuration file!\n"
fi

#=============== PROMPTING USER FOR NON STANDARD LANGUAGES IF NEEDED  ==============
echo -n "Do you have any non standard language files included? Y / N  "
read LANG_BOOL
if [ $LANG_BOOL = "y" ] || [ $LANG_BOOL = "Y" ] 
then
	echo -n "How many non standard languages do you have?  "
	read LANG_NUMS
	while [ $LANG_NUM -lt $LANG_NUMS ]; 
	do
		echo -n "Please enter the language name and press [ENTER]:"
		read LANG_NAME
		LANG_FILE=$LANG_NAME".inc.php"
		LANG_COUNTRY_FILE=$LANG_NAME"_country.inc.php"
	
		#Moving the non standard language file(s) for AjaxSearch
		echo -e "Moving $LANG_FILE from \"$ASS_OLD/snippets/ajaxSearch/lang/\" to \"$ASS/snippets/ajaxSearch/lang/\"!"
		if [ -f $ASS_OLD"/snippets/ajaxSearch/lang/"$LANG_FILE ]
		then 
			mv $ASS_OLD"/snippets/ajaxSearch/lang/"$LANG_FILE $ASS"/snippets/ajaxSearch/lang/" 
			echo -e "The File \"$LANG_FILE\" for AjaxSearch has been successfully moved!\n"
		else 
			echo -e "This ModX-Instance has no $LANG_FILE-File for AjaxSearch!\n"
		fi
	
		#Moving the non standard language file(s) for ditto
		echo -e "Moving $LANG_FILE from \"$ASS_OLD/snippets/ditto/lang/\" to \"$ASS/snippets/ditto/lang/\"!"
		if [ -f $ASS_OLD"/snippets/ditto/lang/"$LANG_FILE ]
		then 
			mv $ASS_OLD"/snippets/ditto/lang/"$LANG_FILE $ASS"/snippets/ditto/lang/" 
			echo -e "The File \"$LANG_FILE\" for Ditto has been successfully moved!\n"
		else 
			echo -e "This ModX-Instance has no $LANG_FILE-File for Ditto!\n"
		fi

		#Moving the non standard language file(s) for eform
		echo -e "Moving $LANG_FILE from from \"$ASS_OLD/snippets/eform/lang/\" to \"$ASS/snippets/eform/lang/\"!"
		if [ -f $ASS_OLD"/snippets/eform/lang/"$LANG_FILE ]
			then 
				mv $ASS_OLD"/snippets/eform/lang/"$LANG_FILE $ASS"/snippets/eform/lang/" 
				echo -e "The Luxembourgish Language File for Ditto has been successfully moved!\n"
			else 
				echo -e "This ModX-Instance has no Luxembourgish Language File for EForm!\n"
		fi

		#Moving the non standard language file(s) for DocManager
		echo -e "Moving $LANG_FILE from from \"$ASS_OLD/modules/docmanager/lang/\" to \"$ASS/modules/docmanager/lang/\"!"
		if [ -f $ASS_OLD"/modules/docmanager/lang/"$LANG_FILE ]
			then 
				mv $ASS_OLD"/modules/docmanager/lang/"$LANG_FILE $ASS"/modules/docmanager/lang/" 
				echo -e "The Luxembourgish Language File for DocManager has been successfully moved!\n"
			else 
				echo -e "This ModX-Instance has no Luxembourgish Language File for DocManager!\n"
		fi

		#Moving the non standard language file(s) for TinyMCE
		echo -e "Moving $LANG_FILE from from \"$ASS_OLD/plugins/tinymce/lang/\" to \"$ASS/plugins/tinymce/lang/\"!"
		if [ -f $ASS_OLD"/plugins/tinymce/lang/"$LANG_FILE ]
			then 
				mv $ASS_OLD"/plugins/tinymce/lang/"$LANG_FILE $ASS"/plugins/tinymce/lang/" 
				echo -e "The Luxembourgish Language File for TinyMCE has been successfully moved!\n"
			else 
				echo -e "This ModX-Instance has no Luxembourgish Language File for TinyMCE!\n"
		fi

		#Moving the non standard country file(s) for manager
		echo -e "Moving $LANG_COUNTRY_FILE from from \"$MAN_OLD/includes/lang/country/\" to \"$MAN/includes/lang/country/\"!"
		if [ -f $MAN_OLD"/includes/lang/country/"$LUX_COUNTRY ]
			then 
				mv $MAN_OLD"/includes/lang/country/"$LUX_COUNTRY $MAN"includes/lang/country/" 
				echo -e "The Luxembourgish Country File for Manger has been successfully moved!\n"
			else 
				echo -e "This ModX-Instance has no Luxembourgish Country File for Manager!\n"
		fi

		#Moving the non standard language file(s) for manager
		echo -e "Moving the Luxembourgish Language File from \"$MAN_OLD/includes/lang/\" to \"$MAN/includes/lang/\"!"
		if [ -f $MAN_OLD"/includes/lang/"$LANG_FILE ]
			then 
				mv $MAN_OLD"/includes/lang/"$LANG_FILE $MAN"includes/lang/" 
				echo -e "The Luxembourgish Language File for Manger has been successfully moved!\n"
			else 
				echo -e "This ModX-Instance has no Luxembourgish Language File for Manager!\n"
		fi
		let LANG_NUM=LANG_NUM+1
	done
else
	echo -e "You have no non standard language files.\n"
fi	

#=========================== REMOVING OBSOLETE FOLDERS  ===========================
#Removing the folders "assets_old" and "manager_old"
echo -e "Removing the now unused folder \"$ASS_OLD\" ...\n"
rm -rf $ASS_OLD
echo -e "Removing the now unused folder \"$MAN_OLD\" ...\n"
rm -rf $MAN_OLD

#Set correct rights for language files
echo -e "Updating language files permissions ..."
cd $MAN"/includes/lang/"
chmod +x *
cd ../../..
echo -e "Language files permissions successfully updated!\n"
echo -e "All file and folders have been successfully updated. Please open your webbrowser and run install!\n"
