#!/bin/bash

URL="http://free.update.lkv2.mikbill.pro"
FILE_NAME="stat"
UPDATE_NAME="mbstat_updater"

WWW_DIR="/var/bill/prod/mbstat/public"
UPD_DIR="/var/bill/updates/mbstat"
BKP_DIR="/var/bill/backups/mbstat"
TMP_DIR="/tmp"

DATENAME=$(date +%F_%H-%M)

if [ ! -d "$WWW_DIR" ]; then
	echo "mbstat dir not found"
	exit 0
fi

if [ ! -d $BKP_DIR ]; then
	mkdir -p $BKP_DIR
fi

cd $UPD_DIR
# check updater version
if [ ! -f $UPDATE_NAME.current.checksum ]; then
	echo 0 > $UPDATE_NAME.current.checksum
fi

wget -O $UPDATE_NAME.downloaded.checksum -q $URL/$UPDATE_NAME.checksum
if [ $? -ne 0 ]; then
	echo "Download FAILED! please check DNS and connection to $URL/$UPDATE_NAME.checksum"
	if [ -f $$UPDATE_NAME.downloaded.checksum ]; then
		rm -f $$UPDATE_NAME.downloaded.checksum
	fi
	exit 0
fi
MD5_CURRENT=$(cat $UPDATE_NAME.current.checksum)
MD5_DOWNLOADED=$(cat $UPDATE_NAME.downloaded.checksum)
if [ "$MD5_CURRENT" != "$MD5_DOWNLOADED" ]; then
	echo "download new updater version!"
	wget -q -O $TMP_DIR/mbstat_update.sh $URL/$UPDATE_NAME.sh
	if [ $? -ne 0 ]; then
		echo "Download FAILED! please check DNS and connection to $URL/$UPDATE_NAME.sh"
		rm -f $TMP_DIR/mbstat_update.sh
		exit 0
	fi
	
	if [ -f $TMP_DIR/mbstat_update.sh ]; then
		echo "$MD5_DOWNLOADED" > $UPD_DIR/$UPDATE_NAME.current.checksum
		
		rm -f $UPD_DIR/mbstat_update.sh
		mv $TMP_DIR/mbstat_update.sh $UPD_DIR/mbstat_update.sh
		chmod a+x $UPD_DIR/mbstat_update.sh
		echo "RUN new version UPDATER SOFT!"

		./mbstat_update.sh
		echo "Update of UPDATER SOFT Success!"
	fi

	exit
fi

# check update version
wget -O $FILE_NAME.downloaded.checksum -q $URL/$FILE_NAME.checksum
if [ $? -ne 0 ]; then
	echo "Download FAILED! please check DNS and connection to $URL/$FILE_NAME.checksum"
	if [ -f $FILE_NAME.downloaded.checksum ]; then
		rm -f $FILE_NAME.downloaded.checksum
	fi
	exit 0
fi

if [ ! -f $FILE_NAME.current.checksum ]; then
	echo 0 > $FILE_NAME.current.checksum
fi

MD5_CURRENT=$(cat $FILE_NAME.current.checksum)
MD5_DOWNLOADED=$(cat $FILE_NAME.downloaded.checksum)
if [ "$MD5_CURRENT" != "$MD5_DOWNLOADED" ]; then
	echo "download mbstat update..."
	cd $TMP_DIR
	wget -O $FILE_NAME.tar.gz -q $URL/$FILE_NAME.tar.gz
	if [ $? -ne 0 ]; then
        echo "Download FAILED! please check DNS and connection to $URL/$FILE_NAME"
		if [ -f $FILE_NAME.tar.gz ]; then
			rm -f $FILE_NAME.tar.gz
		fi 
        exit 0
	fi
	echo "$MD5_DOWNLOADED" > $UPD_DIR/$FILE_NAME.current.checksum

	if [ -d $WWW_DIR ]; then
		if [ -d $BKP_DIR ]; then
			echo "backup current version to $BKP_DIR/mbstat.$DATENAME.tar.gz"
			tar -zcf mbstat.$DATENAME.tar.gz --exclude='stat/sys' $WWW_DIR
			mv mbstat.$DATENAME.tar.gz $BKP_DIR/
		fi

		echo "install new stat update"
		tar -zxf $FILE_NAME.tar.gz --strip-components=2 -C $WWW_DIR
		rm -f $FILE_NAME.tar.gz
	else
		echo "$WWW_DIR not found! check path"
	fi
else
	echo "installed latest stat"
fi

backup_name=$(date +%F_%H-%M)
for php_ini_file in $(find /etc -type f -name "php.ini")
do
  if [ -f "${php_ini_file}" ]; then
    chattr -i "${php_ini_file}"
    sed -e '/^[^;]*disable_functions/s/=.*$/= shell_exec,proc_terminate,pcntl_exec,passthru,proc_nice,dl,show_source,apache_note,apache_setenv,debugger_off,debugger_on,closelog,define_syslog_variables,ini_restore,openlog,syslog,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_uname,posix_setgid,posix_setuid,posix_setegid,posix_getpwuid,posix_setpgid,mail,apache_child_terminate,chgrp,chown,curl_multi_exec,define_syslog_variables,highlight_file,parse_ini_string,stream_select,imap_open,xmlrpc_entity_decode,inject_code,ini_alter,xmlrpc_encode,xmlrpc_decode,xmlrpc_get_type,xmlrpc_is_fault,xmlrpc_set_type,phpAds_remoteInfo,phpAds_XmlRpc,phpAds_xmlrpcDecode,phpAds_xmlrpcEncode,ftp_exec,ftp_get,ftp_nb_fput,ftp_raw,ftp_rawlist,symlink,assert,create_function,phpinfo,posix_ttyname,getenv,posix_getlogin,get_current_user,get_cfg_var,diskfreespace,getlastmo,getmygid,getmyinode,getmyuid,extract,putenv,pfsockopen,base64_decode,parse_ini_file,call_user_func_array,call_user_func/' -i.${backup_name} ${php_ini_file}
    service php-fpm restart > /dev/null 2>&1
  fi
done

