#!/bin/bash

update_path="/etc/base/update"
update_ctrl="/tmp/update.restart"
update_json="/tmp/databack/update.json"

killall -9 syscontrol

stop_list="
"

update_step="0"
update_errs=""
update_boot="0"
update_oooo=$(find $update_path/ -type f | wc -l)
update_cccc="0"
update_over="0"

clean_base_space(){
    update_step="clean base space"
    echo $update_step
    rm -f /usr/bin/key-server
    rm -f /usr/bin/key-client
    killall regcloud 

    echo '{"update":{"now":"'$update_cccc'","total":"'$update_oooo'","complete":"'$update_over'","reboot":"'$update_boot'","error":"'$update_errs'","step":"'$update_step'","description":""}}' > $update_json
}

update_cpy(){
    local sys_file=$1 #old file path
    local new_file=$2 # new file path
    local md5_old=`md5sum "${sys_file}" | awk {'print $1'}`
    local md5_new=`md5sum "${new_file}" | awk {'print $1'}`
    [ "${md5_old}" = "${md5_new}" ] && return 0
    local i=0
    while [ $i -lt 3 ]; do
        cp "${new_file}" "$sys_file" -a
        sync
        md5_old=`md5sum "$sys_file" | awk {'print $1'}`
        [ "${md5_old}" = "${md5_new}" ] && break
        let i=i+1
    done
    return 0
}

update_sys_app(){
    local update_step="update sys app"
    echo $update_step
    
    for file in $1/*; do    
        local basefile=$(basename "$file")
        if [ -f "${file}" -o -h "${file}" ]; then
            local file_dir=$(dirname "$file")
            local file_dir=${file_dir#$update_path}
            local sys_file="${file_dir}/${basefile}"
            [ "${file_dir}X" = "X" ] && continue
            let update_cccc=update_cccc+1
            echo '{"update":{"now":"'$update_cccc'","total":"'$update_oooo'","complete":"'$update_over'","reboot":"'$update_boot'","error":"'$update_errs'","step":"'$update_step'","description":""}}' > $update_json
            mkdir -p "${file_dir}" > /dev/null 2>&1
            # if file is not exists
            if [ ! -e "$sys_file" ]; then                
                cp "${file}" "$sys_file" -a
                sync
                continue
            fi
            # if file is link or src is a link
            if [ -h "$sys_file" -o -h "$file" ]; then
                rm -f "$sys_file"
                cp "${file}" "$sys_file" -a
                sync
                continue
            fi
            # else file exists
            update_cpy "$sys_file" "${file}"
        elif [ -d "${file}" ]; then
            [ -h "$file" ] && continue
            [ "${basefile}" = "boot" ] && continue
            [ "X${basefile}" != "X" ] && mkdir -p "${file#$update_path}"
            update_sys_app "$file"
        fi
    done
}

update_all_done(){
    update_cccc=$update_oooo
    update_over=1
    
    # set json data
    cd $update_path
    echo '{"update":{"now":"'$update_cccc'","total":"'$update_oooo'","complete":"'$update_over'","reboot":"'$update_boot'","error":"'$update_errs'","step":"'$update_step'","description":""}}' > $update_json
    if [ -f /usr/www/include/versionNumber ]; then
    	ov=`cat /usr/www/include/versionNumber | cut -d '-' -f1`
        nv=`cat ${update_path}/version | xargs echo -n`
        echo "$ov-$nv" > /usr/www/include/versionNumber	
    fi
    (/sbin/regcloud >/dev/null 2>&1 &)
    if [ "$update_boot" = "1" ]; then
        echo 0 > $update_ctrl
    	touch /etc/base/.update.app
    	local num=0
    	while [ $num -le 30 ]; do        
        	local re=`cat $update_ctrl`
        	[ "X${re}" = "X1" ] && break
        	sleep 1
        	let num=num+1
    	done
    	reboot
    fi
}

# make json dir
mkdir -p /tmp/databack/ > /dev/null 2>&1
KERNEL=$(uname -m)
PLATFORM=${update_path}/platform
if [ -f $PLATFORM ]; then
    PKG_KERNEL=$(cat $PLATFORM)
    if [ "$KERNEL" != "$PKG_KERNEL" ]; then
        update_errs="Update package is not correct!!!"
        echo '{"update":{"now":"'$update_cccc'","total":"'$update_oooo'","complete":"'$update_over'","reboot":"'$update_boot'","error":"'$update_errs'","step":"'$update_step'","description":""}}' > $update_json
        exit 1
    fi
fi

#stop all service
echo "stop all process & service"
for svrname in $stop_list; do
    [ -z "$svrname" ] && continue
    svr_shell=/etc/init.d/$svrname
    [ -x "$svr_shell" ] && $svr_shell stop >/dev/null
done

# update prepare
clean_base_space

# update app
update_sys_app $update_path

# finish
update_all_done

exit 0

