#!/usr/bin/bash
CWD=`dirname $0`
pushd $CWD >/dev/null

export CLI_HOME=`pwd`
popd >/dev/null

MEM_FREE=80
PRO_ALLOW=4

ISREMOTE=FALSE
for arg in "$@"
do
    if [ $arg = "-c" ];then
         ISREMOTE=TRUE
         break
    fi
done

sftp_cmd=`echo "$@"`
[ "$sftp_cmd" = "-c /usr/libexec/openssh/sftp-server -l INFO -f AUTH" ] || sftp_cmd=""


export LD_LIBRARY_PATH=$CLI_HOME:$CLI_HOME/bin

trap exit 2
trap 'echo "."' 20

umask 022

ulimit -c unlimited 

user=""
if test -n "$LOGNAME" ; then
    user="-u $LOGNAME"
fi

function enter_minisys()
{
    printf "\n"
    printf "\n" 
    cd /OSM/script
    ./os_debug_mode_process.sh $LOGNAME $seg_ip "0"
    cd - &>/dev/null
}

function om_isrun()
{
    /ISM/cli/bin/use_8080_port > /dev/null
    if [ $? -eq 0 ] ; then
    return 0
    else
    return 1
    fi
}

function check_power_on()
{
    if [ -f  /OSM/log/runlog/sys_power_on_record  ] ;then
        return 0
    elif [ -n "`cat /proc/cmdline | grep os_rescue`" ] ;then    
        return 0
    fi
    return 1
}

function check_group()
{
    L_CHECK_UTOPUSERS_GROUP=`id -Gn | grep UTOPUSERS`
    L_CHECK_GUEST_USER_GROUP=`id -Gn | grep utoponlyusers`

    [ -z "$L_CHECK_UTOPUSERS_GROUP" ] && [ -z "$L_CHECK_GUEST_USER_GROUP" ] && echo "System is ready, Please relogin." && return 1
    return 0
}

function cp_ibc_rsa()
{
    rsa_path=/ISM/cli/bin/id_rsa
    rsa_enpath=/ISM/cli/bin/id_rsa_encrypt
    encry_path=/ISM/cli/bin/encyptPaswd
    
    if [ -f /startup_disk/conf/conf_local/ibc_info.ini ];then
        encryptInfo=`cat /startup_disk/conf/conf_local/ibc_info.ini | grep EnableEncyptKey`
        if [ $encryptInfo == "EnableEncyptKey=1" ];then
            cp /startup_disk/conf/conf_local/id_rsa_encrypt /ISM/cli/bin >/dev/null 2>&1
            sudo chown OceanStorMgrUser:mgrgroup $rsa_enpath >/dev/null 2>&1
            sudo chmod 555 $rsa_enpath >/dev/null 2>&1
            return 0
        fi
    fi
    rm $encry_path $rsa_enpath >/dev/null 2>&1
    sudo cp -f /home/ibc_os_hs/.ssh/id_rsa /ISM/cli/bin >/dev/null 2>&1
    sudo chown OceanStorMgrUser:mgrgroup $rsa_path   >/dev/null 2>&1
    sudo chmod 555 $rsa_path >/dev/null 2>&1
    return 0
}
function cp_ibc_pubkey()
{       
    cp_ibc_rsa
    sudo cp -f /home/ibc_os_hs/.ssh/authorized_keys /ISM/cli/bin >/dev/null 2>&1
    sudo chown OceanStorMgrUser:mgrgroup /ISM/cli/bin/authorized_keys >/dev/null 2>&1
    sudo chmod 555 /ISM/cli/bin/authorized_keys   >/dev/null 2>&1
}

START_CLI_LITE()
{

    if [ -f /OSM/log/runlog/sys_power_on_record ]; then
         /OSM/script/os_debug_mode_process_nof.sh $LOGNAME
         exit 0
    fi

    if [ `ps -ef | grep os_debug_cli_lite_mode_process.sh | grep -v grep | wc -l` -lt $PRO_ALLOW ];then
        if [ `free -m  | grep "Mem" | awk '{print $4}'` -gt $MEM_FREE ];then
            cd /OSM/script
            ./os_debug_cli_lite_mode_process.sh $LOGNAME
            cd - &>/dev/null
        else
            cd /OSM/script
            ./os_debug_mode_process_nof.sh $LOGNAME $seg_ip "206"
            cd - &>/dev/null
        fi
    else
        cd /OSM/script
        ./os_debug_mode_process_nof.sh $LOGNAME $seg_ip "206"
        cd - &>/dev/null
    fi
    return 0
}
seg_ip=$(echo $client_ip | awk -F. '{print $1"."$2"."$3}')

if [ -f "/startup_disk/conf/conf_local/data_node" ];then
    if [ -z "$sftp_cmd" ]; then
        START_CLI_LITE
    else
        /bin/bash "$@"
    fi
    exit 0
fi

if check_power_on ; then
    if [ -z "$sftp_cmd" ]; then
        #do nothing
        sftp_cmd=""
    else
        /bin/bash "$@"
        exit 0
    fi    
    
    L_ERROR_CODE=`cat /OSM/log/runlog/sys_power_on_record`
    if [ $ISREMOTE = "TRUE" ];then
        echo "System poweron failed, error code $L_ERROR_CODE."
        exit 0
    fi
    $CLI_HOME/cli_connect "$@"
    exit 0
fi


a=(- \\ \| / )
c=0

if om_isrun  ; then
 true
else
    if [ -z "$sftp_cmd" ]; then
        #do nothing
        sftp_cmd=""
    else
        /bin/bash "$@"
        exit 0
    fi    
    
    if [ $ISREMOTE = "TRUE" ];then
        echo "System is not ready, Please try again later."
        exit 0
    fi
    echo -n System is not ready, Please wait\(Ctrl+c to exit or Input minisystem to minisystem mode\).    
    printf "%s" ${a[$((c%4))]}
    while true; do
    read -t 1 -n 1 prompt
        input=${input}$prompt
    if [[ ${prompt} == $'\e' ]];then
       input=""
        clear
    fi
    if [[ ${prompt} == $'\177' ]];then
        input=`echo $input | head -c -3`
        clear
    fi
    if [[ ${input} == "minisystem" ]];then
        input=""
        enter_minisys;
        exit 0
    fi
    
    if check_power_on ; then 
        clear

        sftp_cmd=`echo "$@" | grep "sftp-server"`
        if [ -z "$sftp_cmd" ]; then
            #do nothing
            sftp_cmd=""
        else
            /bin/bash "$@"
            exit 0
        fi
        
        $CLI_HOME/cli_connect "$@"
        exit 0
    fi
    
    prompt=""
        c=$((c+1))
        printf "\r%s" "System is not ready, Please wait(Ctrl+c to exit or Input minisystem to minisystem mode)."${a[$((c%4))]}$input
        if [ $((c%5)) -eq 0 ];then
            if om_isrun; then
                clear
                if check_group; then
                    break
                else
                    exit 0
                fi
            fi
        fi
    done
fi
if [ "$LOGNAME" = "admin" ];then
    cp_ibc_pubkey    
fi

$CLI_HOME/cli_connect "$@"

