#!/bin/sh

root_url=http://dl.ranfs.com/pub
APP_NAME=rfsh
dl_dir=tmp
z=7z
export RF_LOG_LEVEL=info

if dirname "$0" 1>/dev/null 2>/dev/null; then
    SH_DIR="$(cd "$(dirname "$0")";pwd)"
    cd $SH_DIR
fi

init_env()
{
    sudoprefix=
    IS_ANDROID=false
    TMP_DIR=/tmp
    if id -u 2>/dev/null 1>/dev/null; then
        if ! id | grep "uid=0(" 2>/dev/null 1>/dev/null; then
            if sudo -V >/dev/null 2>&1; then
                sudoprefix=sudo
            else
                echo "error: You need to run the script with root privileges"
                exit 1
            fi
        fi
    fi

    if uname -s 2>/dev/null 1>/dev/null; then
        TOS="`uname -s`"
        ARCH="`uname -m`"
    else
        if echo $OSTYPE | grep darwin  >/dev/null; then
            TOS="Darwin"
            ARCH="x86"
            if echo $MACHTYPE | grep x86_64 >/dev/null; then
                ARCH="x86_64"
            elif echo $CPUTYPE | grep x86_64 >/dev/null; then
                ARCH="x86_64"
            fi
        elif cat /proc/version | grep Linux 2>/dev/null 1>/dev/null; then
            TOS="Linux"
            IS_ANDROID=true
            TMP_DIR=/cache
            if cat /proc/cpuinfo | grep aarch64 2>/dev/null 1>/dev/null; then
               ARCH="aarch64"
            elif cat /proc/cpuinfo | grep arm 2>/dev/null 1>/dev/null; then
               ARCH="armv4tl"
            elif cat /proc/version | grep x86_64 2>/dev/null 1>/dev/null; then
               ARCH="x86_64"
            elif cat /proc/version | grep x86 2>/dev/null 1>/dev/null; then
               ARCH="x86"
            else
                cat /proc/cpuinfo
                echo "error: unsupported Android arch, Please tell us to fix"
            fi
        fi
    fi
}

init_func()
{
    init_env
    $sudoprefix chmod 755 ./${APP_NAME}

    tval=$(./$APP_NAME -q -s "show_app_info=1" | grep plat)
    plat=${tval#*=}
    tval=$(./$APP_NAME -q -s "show_app_info=1" | grep arch)
    arch=${tval#*=}

    ver_url=$root_url/$APP_NAME/all/update.ini
    pkg_url=$root_url/$APP_NAME/allz/$APP_NAME-$plat-$arch.7z
    z_url=$root_url/tools/7zdec/7zdec-$plat-$arch

    if [ "$1" = "-f" ]; then
        echo "start forece update ..."
        update_func
        exit $?
    fi

    ./$APP_NAME -q -s "update_check_url=$ver_url"
    laset_eno=$?
    if [ $laset_eno -eq 2 ]; then
        echo "start update ..."
        update_func
    elif [ $laset_eno -eq 0 ]; then
       echo "This is the latest version."
    else
       : #echo "Get [$ver_url] Failed eno=$?"
    fi
    exit $?
}

update_func()
{
    $sudoprefix mkdir $dl_dir 2>/dev/null

    echo "get $z_url ..."
    $sudoprefix ./$APP_NAME -q -s "if=$z_url" -s "of=$dl_dir/$z"
    if [ $? -ne 0 ]; then
        echo "get $z_url failed"
        return
    fi

    echo "get $pkg_url ..."
    $sudoprefix ./$APP_NAME -q -s "if=$pkg_url" -s "of=$dl_dir/$APP_NAME.7z"
    if [ $? -ne 0 ]; then
        echo "get $pkg_url failed"
        return
    fi

    cd $dl_dir
    $sudoprefix chmod 755 ./$z
    $sudoprefix ./$z x $APP_NAME.7z 1>/dev/null
    if [ $? -ne 0 ]; then
        echo "unpack $APP_NAME.7z error"
        return
    fi

    $sudoprefix rm $z $APP_NAME.7z
    $sudoprefix killall $APP_NAME 2>/dev/null
    $sudoprefix cp -r * ..  >/dev/null

    cd ..
    $sudoprefix rm -rf $dl_dir >/dev/null
    #start error ...?
    stty sane 2>/dev/null
    echo "Update completed successfully..."
}

init_func $1

