#!/bin/bash
#
# TYPO3 Upgrade Script for Mass Deployment 
# Version: 0.6 - http://www.abaton.at/support/typo3/typo3-update-script/
# Copyright (c) 2009 Christoph Jaeger <jaeger@abaton.at>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#=====================================================================

TYPO3_UPDATE_TEMP_FOLDER="typo3_update_temp_folder"
MYSQL_COMMAND="/usr/bin/mysql"
MYSQLDUMP_COMMAND="/usr/bin/mysqldump"
MYSQLDUMP_EXTRA_OPTIONS=""  # can be overridden by typo3_check_basics()
WGET_COMMAND="/usr/bin/wget"
CURL_COMMAND="/usr/bin/curl"
TYPO3_LOCALCONF="typo3conf/localconf.php"
DOWNLOAD_SERVER="http://your.downloadserver.tld/path/"

#=====================================================================
# Options documentation
#=====================================================================
# TYPO3_UPDATE_TEMP_FOLDER is used for temporary files. Please mind to 
# delete this folder after the update. 
# 
# DOWNLOAD_SERVER is used as a local TYPO3 mirror to multiple 
# downloads. Please mind that we linked the latest typo3_src to the 
# "branch-latest" - for example:
# ln -s typo3_src-4.2.5.tar.gz typo3_src-4.2-latest.tar.gz
# Also we'll need a own md5 checksum:
# md5sum typo3_src-4.2-latest.tar.gz > typo3_src-4.2-latest.tar.gz.md5 
#
#=====================================================================

#=====================================================================
# Change Log
#=====================================================================
#
# VER 0.1 - (2009-01-25)
#           Initial release
# VER 0.2 - (2009-01-26)
#           Added some documentation and code clean-up
# VER 0.3 - (2009-01-27)
#           Added WGET_COMMAND to constants. If-exist checks for 
#           commands. 
# VER 0.4 - (2009-02-09)
#           Some cosmetic fixes 	    
# VER 0.5 - (2009-11-30)
#           added TYPO3 release 4.3
# VER 0.6 - (2010-04-09)
#           added counter statistics
# VER 0.7 - (2010-07-09)
#           added TYPO3 release 4.4
# VER 0.8 - (2011-01-28)
#           added TYPO3 release 4.5
#=====================================================================


if [ -z "$1" ]
then
  printf "Usage: `basename $0` [branch]\n"
  printf  "branch: \t current \t - keep current\n"
  printf  "\t\t 4.0, 4.1, 4.2, 4.3, 4.4 \t - major upgrade\n"
  exit 1
fi

case "$1" in
  current)
	TYPO3_BRANCH=`grep -i '^$TYPO_VERSION' t3lib/config_default.php | cut -d ";" -f 1 | cut -d "'" -f2 | cut -d "." -f 1-2`
	;;
  4.0)
	TYPO3_BRANCH="4.0"
	;;
  4.1)
	TYPO3_BRANCH="4.1"
	;;
  4.2)
	TYPO3_BRANCH="4.2"
	;;
  4.3)
	TYPO3_BRANCH="4.3"
	;;
  4.4)
	TYPO3_BRANCH="4.4"
	;;
  4.5)
	TYPO3_BRANCH="4.5"
	;;
  *)
	printf "ERROR: Unable to figure out TYPO3 version OR unsupported version\n"
	exit 1
	;;
esac
printf "SUCCESS: Using TYPO3 branch: $TYPO3_BRANCH\n"
  


typo3_check_basics() {
if ! [ -L typo3_src ]; then
  printf "ERROR: NO symlinked TYPO3 installation\n" 
  exit 1
fi

# get current user and group
TYPO3_CHOWN_USER=`ls -l t3lib/config_default.php | awk '{ print $3 }'`
TYPO3_CHOWN_GROUP=`ls -l t3lib/config_default.php | awk '{ print $4 }'`

# if we are running on PLESK - override mysqldump extra options  
if [ -f /etc/psa/.psa.shadow ]; then
   MYSQLDUMP_EXTRA_OPTIONS="-u admin -p`cat /etc/psa/.psa.shadow`"
fi

# check if wget command exists
if [ ! -x $WGET_COMMAND ]; then
  printf "ERROR: $WGET_COMMAND does not exist or is not executeable\n"
  exit 1
fi

# check if mysql command exists
if [ ! -x $MYSQL_COMMAND ]; then
  printf "ERROR: $MYSQL_COMMAND does not exist or is not executeable\n"
  exit 1
fi

# check if mysqldump command exists
if [ ! -x $MYSQLDUMP_COMMAND ]; then
  printf "ERROR: $MYSQLDUMP_COMMAND does not exist or is not executeable\n"
  exit 1
fi

# create temp folder
if ! [ -d $TYPO3_UPDATE_TEMP_FOLDER ]; then
  mkdir $TYPO3_UPDATE_TEMP_FOLDER
else
  rm -f $TYPO3_UPDATE_TEMP_FOLDER/* 
fi
}

typo3_backup_database() {
# get database name
if [ ! -f "$TYPO3_LOCALCONF" ] ; then
  printf "ERROR: TYPO3 config file $TYPO3_LOCALCONF does not exist\n"
  exit 1
else
  TYPO3_DATABASE=`grep -i '^$typo_db =' ${TYPO3_LOCALCONF} | cut -d ";" -f 1 | cut -d "'" -f 2 | tail -1`
fi

# check if database exists
$MYSQL_COMMAND $MYSQLDUMP_EXTRA_OPTIONS $TYPO3_DATABASE -e "show tables;" > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
  $MYSQLDUMP_COMMAND $MYSQLDUMP_EXTRA_OPTIONS --opt $TYPO3_DATABASE > ${TYPO3_UPDATE_TEMP_FOLDER}/${TYPO3_DATABASE}.sql;
  printf "SUCCESS: TYPO3 DB ${TYPO3_DATABASE} Backup done\n"
else
 printf "ERROR: Database $TYPO3_DATABASE does not exist\n"
 exit 1
fi

}
  
typo3_get_source() {

cd $TYPO3_UPDATE_TEMP_FOLDER
$WGET_COMMAND -q ${DOWNLOAD_SERVER}/typo3_src-${TYPO3_BRANCH}-latest.tar.gz
$WGET_COMMAND -q ${DOWNLOAD_SERVER}/typo3_src-${TYPO3_BRANCH}-latest.tar.gz.md5

# md5 check of downloaded file
md5sum typo3_src-${TYPO3_BRANCH}-latest.tar.gz > typo3_src-${TYPO3_BRANCH}-latest.tar.gz.md5-download
if diff typo3_src-${TYPO3_BRANCH}-latest.tar.gz.md5 typo3_src-${TYPO3_BRANCH}-latest.tar.gz.md5-download > /dev/null
then
	printf "SUCCESS: Download and MD5 OK\n"
else
	printf "ERROR: Download error or MD5 mismatch\n"
	exit 1
fi

cd ..
}

typo3_get_update() {
TYPO3_SOUCE_DIR=`tar tfvz $TYPO3_UPDATE_TEMP_FOLDER/typo3_src-${TYPO3_BRANCH}-latest.tar.gz | tail -1 | awk {'print $6'} | cut -d "/" -f 1`
TYPO3_NEW_VERSION=`echo ${TYPO3_SOUCE_DIR} | cut -d "-" -f 2`
tar xfz $TYPO3_UPDATE_TEMP_FOLDER/typo3_src-${TYPO3_BRANCH}-latest.tar.gz
rm -f typo3_src
ln -s $TYPO3_SOUCE_DIR typo3_src
chown -Rf $TYPO3_CHOWN_USER:$TYPO3_CHOWN_GROUP $TYPO3_SOUCE_DIR
printf "SUCCESS: TYPO3 Update to ${TYPO3_NEW_VERSION} successful\n"
printf "STATS: number of TYPO3 updated of branch ${TYPO3_BRANCH}: `${CURL_COMMAND} -s http://rdh-hw1.aboliton.at/typo3_update_stats/index.php?version=${TYPO3_BRANCH}`\n\n"
printf "HINT: Remove the folder "$TYPO3_UPDATE_TEMP_FOLDER" after successful update!\n"
}

typo3_check_basics
typo3_backup_database
typo3_get_source
typo3_get_update

