#!/bin/bash

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

function log_info()
{
    infomsg=$1
    echo $(date "${DATA_FORMAT}")  "Info:${infomsg}" | tee -a ${LOG_FILE}
}

function log_warning()
{
    warnmsg=$1
    echo $(date "${DATA_FORMAT}")  "Warn:${warnmsg}" | tee -a ${LOG_FILE}
}

function log_error()
{
    errorcode=$1
    errormsg=$2
    echo $(date "${DATA_FORMAT}")  "Error:${errormsg}" | tee -a ${LOG_FILE}
    exit ${errorcode}
}

function download_packages()
{
    log_info "Start downloading packages."
    if [[ -f /root/CloudResetPwdAgent.zip ]];then
        sudo rm -rf /root/CloudResetPwdAgent.zip
    fi

    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

    declare -i max_count=60
    while [[ ! -f /root/CloudResetPwdAgent.zip ]] && [[ max_count -gt 0 ]]
    do
        sleep 2s
        let max_count-=1
        log_info "Waiting for downloading CloudResetPwdAgent."
    done

    if [[ -f /root/CloudResetPwdAgent.zip ]];then
        log_info "Success to downloading packages."
    else
        log_warning "Failed to downloading packages."
        exit 2
    fi
}

function uninstall()
{
    log_info "Start to uninstall packages."
    if [[ $(ls -lh /CloudrResetPwdAgent) ]];then
        sudo /CloudrResetPwdAgent/bin/cloudResetPwdAgent.script remove
        sudo rm -rf /CloudrResetPwdAgent
    fi

    if [[ $(ls -lh /CloudResetPwdUpdateAgent) ]];then
        sudo /CloudResetPwdUpdateAgent/bin/cloudResetPwdUpdateAgent.script stop
        sudo /CloudResetPwdUpdateAgent/bin/cloudResetPwdUpdateAgent.script remove
        sudo rm -rf /CloudResetPwdUpdateAgent
    fi
    
    if [[ -d /Cloud* ]];then
        log_warning "Uninstall cloudResetPwdAgent failed"
    else
        log_info "Uninstall cloudResetPwdAgent successful."
    fi
}

function install()
{
  log_info "Start to install packages."
  unzip -o -d /home CloudResetPwdAgent.zip
    sleep 5s
    cd /home/CloudResetPwdAgent/CloudResetPwdAgent.Linux
    chmod +x setup.sh
    sudo sh setup.sh
    log_info "sh setup.sh successful"
    result=$(service cloudResetPwdAgent status) >> /dev/null
    status=$(echo ${result} | grep 'unrecognized service')
    if [[ -z ${status} ]];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 700 /CloudrResetPwdAgent/bin/cloudResetPwdAgent.script
    chmod 700 /CloudrResetPwdAgent/bin/wrapper
    chmod 600 /CloudrResetPwdAgent/lib/*
    log_info "update file permission successful"
}
###begin
log_info "************************BEGIN Update************************"
download_packages
uninstall
install
update_file_permission