#!/bin/bash
# Written by Liz Quilty ( liz@rimuhosting.com )
# feel free to pass it out, just keep my name on it for fame purposes :)
# run it as root
# sh wordpress-ugrade-x.x.x.sh
# version 1.3 - keeping the permissions so that the web user can write to things ok
# Version 1.2 - Patched for better portability by http://twitter.com/valthonis
# Make sure you set the FINDDIR variable to the directory your websites are located. Usually this is /var/www but occasionally its /home
# do not set it to be / because then it will also find your 'backups' and possibly overwrite them
FINDDIR=/var/www
# This is the version, change the version if a later release is released found in wp-includes/version.php:$wp_version and $wpmu_version
CURRENT_VER="2.9.2"
CURRENT_MUVER="2.9.1.1"
# this is the current DB schema found in wp-includes/version.php:$wp_db_version = 12329;
CURRENT_DBVER="12329"

if [ $(whoami) != "root" ]
then
	  echo "You need to run this script as root."
	  exit 1
fi

wplist=$(find ${FINDDIR} -wholename "*wp-includes/version.php" )

#echo "wplist is $wplist"

for file in $wplist ; do
	wp_root=$(echo $file | sed s@wp-includes/version.php@@)
	db_ver=$(grep ^\$wp_db_version ${wp_root}/wp-includes/version.php| cut -c18-22)
	wpmu=$(grep wpmu_version ${wp_root}/wp-includes/version.php)
	if [ "${wpmu}" == "" ]; then
		thisis="Standerd Wordpress";
		WPCURRENT_VER=$CURRENT_VER
		WP_URL=http://wordpress.org/latest.zip 
		your_ver=$(grep wp_version "$file" |grep -v global |cut -c16-20)
	else
		thisis="Wordpress MultiUser";
		WPCURRENT_VER=$CURRENT_MUVER
		WP_URL=http://mu.wordpress.org/latest.zip 
		your_ver=$(grep wpmu_version "$file" |grep -v global |cut -c18-25)
	fi
	if [ ${your_ver} !=  ${WPCURRENT_VER} ];then
		echo "You have version $your_ver ${thisis} located at $wp_root that needs updateing to ${WPCURRENT_VER}"
		echo -n "Would you like me to upgrade it? [y/N] "
		read yn
		if [ $yn == "y" ];then
			echo "Upgrading $wp_root"
			mkdir -p /tmp/wpupgrade
			cd /tmp/wpupgrade
			# need to get the wp-config.php for user/pass/sitename
			db_name=$(grep DB_NAME "${wp_root}/wp-config.php" | cut -f4 -d"'")
			db_user=$(grep DB_USER "${wp_root}/wp-config.php" | cut -f4 -d"'")
			db_pass=$(grep DB_PASSWORD "${wp_root}/wp-config.php" | cut -f4 -d"'")
			# we also need the table prefix; not all wp installations use the default!
			table_prefix=$(grep table_prefix "${wp_root}/wp-config.php" | cut -f2 -d"'")

			echo Checking i can connect to the db, if so get and set sitename variable
			RESULT=$(mysqladmin -u ${db_user} -p${db_pass} ping)
			if [ "$RESULT" == "mysqld is alive" ]; then
				echo Database connects fine
				# the SQL queries have been modified to use the table prefix accordingly
				if [ "${wpmu}" == "" ]; then
					siteurl=`echo SELECT option_value FROM ${table_prefix}options WHERE option_name=\'siteurl\' LIMIT 1 | mysql -u ${db_user} -p${db_pass} ${db_name} | sed s/option_value//`
					echo Site URL is $siteurl
					clean_url=$(echo $siteurl| sed s@http://@@g)
				else
					clean_url=`echo SELECT domain FROM ${table_prefix}site WHERE id=\'1\' LIMIT 1 | mysql -u ${db_user} -p${db_pass} ${db_name} | sed s/^domain\ //`
					siteurl="http://${clean_url}"
				fi

				echo Making backup at /root/wp_upgrade/${clean_url}.sql and /root/wp_upgrade/${clean_url}.zip \(you can delete these later\)
				# Consider using something other than the URL as the name of the backup files? Perhaps the site name?
				mkdir -p /root/wp_upgrade/${clean_url}
				mysqldump -u ${db_user} -p${db_pass} ${db_name} > /root/wp_upgrade/${clean_url}.sql &&
				zip -qr /root/wp_upgrade/${clean_url}.zip ${wp_root} &&
				echo -n Checking we have latest wordress ...
				rm -rf /tmp/wpupgrade/latest.zip
				rm -rf /tmp/wpupgrade/wordpress
				# WP doesn't always come down as latest.zip, so let's make sure we save it as such
				wget -q -O latest.zip $WP_URL &&
				echo Unzipping ...
				unzip -oq latest.zip &&
				#getting the original owner permissions
				orig_perm=`ls -l $wp_root/wp-content |  awk '{print $3,$4}'| sed s/\ /:/|  sed -n '2,2p'`

				alias cp=cp #some distros have cp aliased to cp -i which asks before each overwrite
				echo Copying files over ...
				if [ "${wpmu}" == "" ]; then
					cp -a wordpress/* $wp_root/ 
				else
					cp -a wordpress-mu/* $wp_root/ 
				fi
				echo Changing permissions to $orig_perm
				/bin/chown $orig_perm $wp_root
				# checking if your db version is less than the current version, if so upgrade!
				if [ "$db_ver" -lt ${CURRENT_DBVER} ]; then
					echo You will need to go to $siteurl/wp-admin/upgrade.php to complete the upgrade as there is a difference in database versions
				fi
				#echo Cleaning up
				#rm -rf /tmp/wpupgrade/
			else
				echo "Something went wrong, aborting this uppgrade. Please do this site manually"	
			fi

		else
			echo "Leaving $wp_root alone as requested"
		fi
	else
		echo "Located wordpress at $wp_root - Up to date, nothing needs doing"
	fi
done
#echo Any backups are located at /root/wp_upgrade/

