#!/bin/bash

### log
LOG_FILE="/home/update_resetpwd_log.log"
DATA_FORMAT="+%Y-%m-%d %H:%M:%S"


function log_info()
{
    infomsg=$1
    echo `date "${DATA_FORMAT}"`  "Info:${infomsg}"
    echo `date "${DATA_FORMAT}"`  "Info:${infomsg}" >> ${LOG_FILE}
}

function log_warning()
{
    warnmsg=$1
    echo `date "${DATA_FORMAT}"`  "Warn:${warnmsg}"
    echo `date "${DATA_FORMAT}"`  "Warn:${warnmsg}" >> ${LOG_FILE}
}

function log_error()
{
    errorcode=$1
    errormsg=$2
    echo `date "${DATA_FORMAT}"`  "Error:${errormsg}"
    echo `date "${DATA_FORMAT}"`  "Error:${errormsg}" >> ${LOG_FILE}
    rollbackBackupFile
    exit ${errorcode}
}

function download_packages()
{
    log_info "Start downloading packages."
	if [[ -f /root/CloudResetPwdAgent.zip ]];then
	    log_info "CloudResetPwdAgent already downloads"
	else
		arm_os=`arch |grep arch64`
		x86_64bit=`arch |grep x86_64`
		x86_32bit=`arch |grep x64`
		if [[ "${arm_os}" ]];then
			wget https://cn-south-1-cloud-reset-pwd.obs.cn-south-1.myhuaweicloud.com/arm/linux/64/reset_pwd_agent/CloudResetPwdAgent.zip -q
			log_info "arm os"
		elif [[ "${x86_64bit}" ]];then
			wget http://cn-south-1-cloud-reset-pwd.obs.cn-south-1.myhuaweicloud.com/linux/64/reset_pwd_agent/CloudResetPwdAgent.zip -q
			log_info "x86 64 bit os"
		elif [[ "${x86_32bit}" ]];then
			wget http://cn-south-1-cloud-reset-pwd.obs.cn-south-1.myhuaweicloud.com/linux/32/reset_pwd_agent/CloudResetPwdAgent.zip -q
			log_info "x86 32 bit os"
		else
			log_warning "arch is not support"
			exit 2
		fi
		
		while [[ ! -f /root/CloudResetPwdAgent.zip ]]
	    do
	        sleep 2s
		    log_info "Waiting for downloading CloudResetPwdAgent."
	    done
	fi

	log_info "Success to downloading packages."
}

function uninstall()
{
    log_info "Start to uninstall packages."
    if [[ `ls -lh /Cloud*` ]];then
		sudo /CloudrResetPwdAgent/bin/cloudResetPwdAgent.script remove
		sudo /CloudResetPwdUpdateAgent/bin/cloudResetPwdUpdateAgent.script stop
		sudo /CloudResetPwdUpdateAgent/bin/cloudResetPwdUpdateAgent.script remove
		sudo rm -rf /CloudrResetPwdAgent
		sudo rm -rf /CloudResetPwdUpdateAgent
	fi
	
	if [[ ! `ls -lh /Cloud*` ]];then
	    log_info "Uninstall cloudResetPwdAgent successful."
	else
	    log_warning "Uninstall cloudResetPwdAgent failed"
	fi
}

function install()
{
    log_info "Start to install packages."
    unzip -o -d /home CloudResetPwdAgent.zip
	sleep 5s
	cd /home/CloudResetPwdAgent/CloudResetPwdUpdateAgent.Linux
	chmod +x setup.sh
	sudo sh setup.sh
	log_info "sh setup.sh successful"
	if [[ `ps -ef|grep java |grep -i reset` ]];then
		log_info "install resetpwd successful"
	else
	    log_info "install resetpwd failed"
	fi
}

function update_file_permission()
{
	chmod -f 640 /CloudrResetPwdAgent/logs/resetPwdAgent.log
	chmod -f 640 /CloudrResetPwdAgent/logs/wrapper.log
	chmod 750 /CloudrResetPwdAgent/bin/cloudResetPwdAgent.script
	chmod 700 /CloudrResetPwdAgent/bin/wrapper
	chmod 600 /CloudrResetPwdAgent/lib/commons-codec-1.14.jar
	chmod 600 /CloudrResetPwdAgent/lib/libwrapper.so
	chmod 600 /CloudrResetPwdAgent/lib/resetpwdagent.jar
	chmod 600 /CloudrResetPwdAgent/lib/wrapper.jar
	chmod 640 /CloudrResetPwdAgent/lib/json-20160810.jar
	chmod -f 640 /CloudrResetPwdAgent/lib/log4j-*.jar
	log_info "update file permission successful"
}
###begin
log_info "************************BEGIN Update************************"
download_packages
uninstall
install
update_file_permission