#!/bin/bash

if [ `whoami` != "root" ] ;then
	echo "Permission denied.Only root can execute the script"
	exit
fi


echo "Change the timezone of the system will restart the httpd, mysql and omcd. Are you sure to continue?(y/n)"
read response

if [ "$response" != "y" ] ;then
	exit
fi

echo "Please input the timezone(Asia/Shanghai or GMT for example):"
read timezone


if [ -e /usr/share/zoneinfo/$timezone ] ;then
	if [ "${timezone}xxxx" = "xxxx" ] ;then
		echo "Input invalid."
        	exit
	fi	
	echo "Change the timezone to $timezone"	
else
	echo "The timezone[$timezone] does not exist"
	exit
fi


if [ -e /etc/php.ini ] ;then
	sed "/date.timezone/d" /etc/php.ini > /tmp/php.ini
	sed -e "/Date/a date.timezone = ${timezone}" /tmp/php.ini > /tmp/php_bak.ini
	rm -rf /tmp/php.ini
	cp -f /tmp/php_bak.ini /etc/php.ini
	chmod 755 /etc/php.ini
	rm -rf /tmp/php_bak.ini
else
	sed "/date.timezone/d" /usr/local/lib/php.ini > /tmp/php.ini
        sed -e "/Date/a date.timezone = ${timezone}" /tmp/php.ini > /tmp/php_bak.ini
        rm -rf /tmp/php.ini
        cp -f /tmp/php_bak.ini /usr/local/lib/php.ini
        chmod 755 /usr/local/lib/php.ini
	rm -rf /tmp/php_bak.ini
fi

ln -sf /usr/share/zoneinfo/$timezone /etc/localtime
echo "ZONE=$timezone" > /etc/sysconfig/clock
echo "UTC=true" >> /etc/sysconfig/clock
echo "ARC=false" >> /etc/sysconfig/clock

omcd stop
httpd restart
mysql restart
omcd start

echo "Finish to change the timezone to [$timezone]."

