#!/bin/bash
source /OSM/script/os_load_base.sh
ALM_RESTORE_SERVER="$1"
ALM_RESTORE_USER="$2"
ALM_RESTORE_PASSWORD="$3"
CON_TIMEOUT=5
ALM_PROTOCOL_TYLE="$4"
ALM_PORT="$5"
SET_PORT=""
ALARM_RESTORE_RESULT="/tmp/restore_result_$$"

# Catch the exit signals, and clean up temp files
trap "RESTORE_FAIL_CLEAN_UP" HUP INT QUIT TERM

# **************************************************************************** #
# Function Name: ALM_RESTORE_FTP_ISVALID()
# Description: check route is ok with ping
# Parameter: 0
# Return: 0
# Author: xxx
# **************************************************************************** #
ALM_RESTORE_FTP_ISVALID()
{ 
    local L_RET=0
    ping -c 1 -w 1 $ALM_RESTORE_SERVER>/dev/null
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        ping6 -c 1 -w 1 $ALM_RESTORE_SERVER>/dev/null
        L_RET=$?
        if [ 0 -ne ${L_RET} ];then
            OS_echo "ftp is invalid."
            return 1
        fi
    fi
}
# **************************************************************************** #
# Function Name: ALM_RESTORE_CHECK_USER()
# Description: check user is OK with name & pw
# Parameter: 0
# Return: 0
# Author: xxx
# **************************************************************************** #
ALM_RESTORE_CHECK_USER()
{
    local L_RET=0
    lftp -u ${ALM_RESTORE_USER},${ALM_RESTORE_PASSWORD} ${FTP_PROTOCOL}${ALM_RESTORE_SERVER} ${SET_PORT} << EOF
set net:reconnect-interval-base 1 
set net:max-retries 1
set ftp:ssl-allow no
set sftp:auto-confirm yes
set net:timeout 15s
debug 4 -o ${ALARM_RESTORE_RESULT}
cd . >/dev/null
EOF
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "user or password incorrect."
        return 1
    fi
}

# **************************************************************************** #
# Function Name: ALM_RESTORE_IsLFtpSucc
# Description: check whether ftp is succ
# Parameter: 1-result file
# Return: 0-succ 1-fail
# **************************************************************************** #
ALM_RESTORE_IsLFtpSucc() {
    L_RESULT=$1
    if [ -f "$L_RESULT" ];then 
        L_ERROR=`cat $L_RESULT | grep "error"`
        L_UNREACHABLE=`cat $L_RESULT | grep "Network is unreachable"`  
        L_FILE_ERROR=`cat $L_RESULT | grep "Could not get file"` 
        L_LOGIN_ERROR=`cat $L_RESULT | grep "Login incorrect"`
        L_CONNECT_ERROR=`cat $L_RESULT | grep "Socket error"`
        L_CMD_ERROR=`cat $L_RESULT | grep "no such file or directory"`
        L_VERIFY_ERROR=`cat $L_RESULT | grep "Host key verification failed"`

        if [ -n "$L_VERIFY_ERROR" ];then
            return 2
        fi

        if [ -n "$L_ERROR" -o -n "$L_UNREACHABLE" -o -n "$L_FILE_ERROR" -o -n "$L_LOGIN_ERROR" ];then
            return 1
        fi
    else
        return 1
    fi
    return 0
}

# **************************************************************************** #
# Function Name: ALM_RESTORE_ProducelftpCmd()
# Description: produce lftp cmd file
# Parameter: 0
# Return: 0
# Author: xxx
# **************************************************************************** #
ALM_RESTORE_ProducelftpCmd()
{ 
    if [ "$ALM_PROTOCOL_TYLE" -eq 0 ]; then
        FTP_PROTOCOL="ftp://"
        if [ -z "$ALM_PORT" ]; then
            SET_PORT=""
        else
            SET_PORT="-p $ALM_PORT"
        fi
    else
        FTP_PROTOCOL="sftp://"
        SET_PORT="-p $ALM_PORT"
    fi
    return 0
}

# **************************************************************************** #
# Function Name: RESTORE_FAIL_CLEAN_UP
# Description:   Clean up the tmp files when the script receives signals.
# Parameter:     void
# Return:        1-fail
# **************************************************************************** #
RESTORE_FAIL_CLEAN_UP()
{
    OS_echo "Receives signals while restoring files."
    rm -rf $ALARM_RESTORE_RESULT &>/dev/null

    exit 1
}
# --------------------------  main  ------------------------------------------ #
#1. set&&produce lftp command
ALM_RESTORE_ProducelftpCmd

#2. 
#如果每次都先ping的话，效率会很低，这里先一把检测用户名，密码，端口号，ip，协议，如果通过不用再ping，
#如果不通，则ping一把，将IP不通的情况单独取出来，返回值为1
#返回值说明：1：IP ping不通
#            2：用户名、密码、协议、端口号中的某一项配置错误
#            3：文件传输过程中发生了错误
#            4: Host key校验失败
ALM_RESTORE_CHECK_USER
iRet=$?

#3.check if ftp success
ALM_RESTORE_IsLFtpSucc ${ALARM_RESTORE_RESULT}
ret=$?
if [ ${ret} -eq 2 ];then
	rm -rf ${ALARM_RESTORE_RESULT} &>/dev/null
    exit 4
fi

rm -rf ${ALARM_RESTORE_RESULT} &>/dev/null

if [ 0 -ne ${iRet} ];then

    ALM_RESTORE_FTP_ISVALID 
    if [ 0 -ne $? ];then
        exit 1
    fi
    
    exit 2
fi

#3. success; 
exit 0

