#!/bin/bash
#===============================================================================
#
#          FILE: updatectrl
#
#         USAGE: updatectrl <Ctrl tarball>
#
#   DESCRIPTION: Install or update from the Ctrl tarball
#
#       OPTIONS: <Ctrl tarball>
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Wed Jul  6 22:41:23 EDT 2011
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                      # Enable job control

[ "$1" == "" ] && exec echo "Usage: $0 <Ctrl tarball>"

# Clean and extract
rm -rf /tmp/Ctrl.tmp
mkdir /tmp/Ctrl.tmp
tar zxvf "$1" -C /tmp/Ctrl.tmp

# Force to overwrite .functions and .psqlrc
for FILE in .functions .psqlrc .bash_xl .bash_profile .bashrc; do
        cp -fv /tmp/Ctrl.tmp/$FILE ~/
done

if [ -d ~/bin ] ; then
        # Backup the Ctrl first
        ssh TEST "tar zcf www/bin/Ctrl-$(date -d "-1 minute" +%Y%m%dT%H%M).tar.gz .gamerc bin .psqlrc .functions .bash_xl .bash_profile .bashrc"

	# If ~/bin/ exist(update only)
	cd /tmp/Ctrl.tmp/bin/

	# Force to renew crontab.default
	rm -fv ~/bin/crontab.default

	# Remove files that we dont want to copy
	for file in \
	createdb_post_* a b c d ; do
		rm -f $file
	done

	# Remove files that we dont want to overwrite
	for file in \
	worlds.* hosts customized_function updatectrl setup.ini_* rsync.exclude* do_not_overwrite \
	ibackup.conf asciiart_chars min_ports.* sendscript.* S_RootCmds.ini* log2any.* S_GMToolCmds.ini* \
	webtool_log2db* NAT_IP_table pg_error_log.ignore_error pg_error_log.ignore_statement \
	createdb_post_* a b c d log2any_* *.ipy *_config.py log2any log2any.2ri log2any.default_template \
        log2any.log2db log2any.patterns ; do 
		cp -i $file ~/bin/ < /dev/null 2> /dev/null
		rm -f $file
	done
	
	# Customized list of "do not overwrite"
	for do_not_overwrite in `cat ~/bin/do_not_overwrite 2> /dev/null`;do
		rm -fv /tmp/Ctrl.tmp/bin/$do_not_overwrite
	done	

	# For new S_RootCmds rule
	(
	cd ~/bin/
	for S_RootCmds in S_RootCmds.ini_??;do 
		if [ -e $S_RootCmds ] ; then 
			GAMETYPE="$(echo $S_RootCmds|tail -c 3)"
			sed 's/,[0-9]\(,.*,\)/,2\1/g' S_RootCmds.ini_$GAMETYPE > S_RootCmds.ini_live_$GAMETYPE
			mv -v $S_RootCmds S_RootCmds.ini_test_$GAMETYPE
			S_ROOTCMDS_MOVED=1
		fi
	done
	cd -
	if [ "$S_ROOTCMDS_MOVED" == "1" ] ; then # Rebuild SetDir for test servers(for the the S_RootCmds.ini linking)
		for set_name in `cat ~/bin/worlds.test`;do 
			SKServer Build SetDir $set_name
		done
		echo "It is okay if you see any No such file message above. :-)"
	fi
	)


	cd -
	# Remove symbolic link files
        find /tmp/Ctrl.tmp/bin -type l | xargs rm -fv

	# Update
        cp -frv /tmp/Ctrl.tmp/bin/* ~/bin/

	# Show the differents between old and new gamerc
	cp -fv /tmp/Ctrl.tmp/.gamerc ~/.gamerc.new
        # For some situation that ~/.gamerc may not exist
        [ -e ~/.gamerc ] || cp -fv /tmp/Ctrl.tmp/.gamerc ~/.gamerc
	sed -i -e 's/ \+$//g' ~/.gamerc ~/.gamerc.new
	diff -p -u ~/.gamerc ~/.gamerc.new|highlight - "^-.*" "^+.*" "^@@.*"

	# Sync to every server
	grep -q CTRL /etc/hosts || syncscript
else
	# If ~/bin/ doesn't exist(new install)
        mkdir -p ~/bin
        cp -axv /tmp/Ctrl.tmp/bin/* ~/bin/
        echo -e "LIVE1\nLIVE2" > ~/bin/worlds.live
        echo -e "TEST\nFull\nLIVEBackup" > ~/bin/worlds.test
        cp /tmp/Ctrl.tmp/.gamerc ~/
        cp /etc/hosts ~/.hosts
	#grep -q .gamerc ~/.bashrc || echo 'source ~/.gamerc' >> ~/.bashrc
	mkdir -p ~/DBbackup ~/patch ~/www/core/test
	echo > ~/bin/customized_function

	echo
	echo "IMPORTANT: Please edit ~/bin/hosts and ~/.gamerc first."
fi

# Make sure all the shell scripts executable
file ~/bin/* | egrep 'shell|xz|py' | awk -F: '{print "chmod 755 "$1}' | sh

# Clean
#mv -v $1 /tmp/
#rm -rf /tmp/Ctrl.tmp
#
# Update myself
cat $0 > ~/bin/updatectrl
#rm -f "$0"
