#!/bin/bash
set +x

source /opt/huawei/snas/script/inspect_mml/CommonFunc.sh
LOG_FILE="/var/log/inspect.log"
CIFS_FILE="/tmp/tmpfileCifsShareDir"
MMM_FILE="/tmp/tmpfilemmlRedundantRatio"
NFS_FILE="/tmp/tmpfileNfsShareDir"

function LOG
{
   time=$(date)
   echo [${time}][$$]$@ >> $LOG_FILE
}

function RedundantRatio()
{
    if [ "$1" == "1" ] && [ "$2" == "1" ];then
        echo "+1"
        return 0
    fi

    if [ "$1" == "2" ] && [ "$2" == "2" ];then
        echo "+2"
        return 0
    fi

    if [ "$1" == "2" ] && [ "$2" == "1" ];then
        echo "+2:1"
        return 0
    fi

    if [ "$1" == "3" ] && [ "$2" == "3" ];then
        echo "+3"
        return 0
    fi

    if [ "$1" == "3" ] && [ "$2" == "1" ];then
        echo "+3:1"
        return 0
    fi

    if [ "$1" == "4" ] && [ "$2" == "4" ];then
        echo "+4"
        return 0
    fi
}

#获取cifs共享目录
function GetCifsShareDir()
{
    /tmp/${tmpdir}/nas.sh "/usr/local/bin/nas_proto_cifs -c show-page -f 0 -t 50" |grep "<path>" >$CIFS_FILE
    iRet=$?
    if [ $iRet -ne 0 ];then
        sharedircifs=1
        LOG "[$FUNCNAME][$LINENO] does not cifs sharedir"
    fi
    IFS_OLD=$IFS
    IFS=$'\n'
    for item in $(cat $CIFS_FILE | awk -F">" '{print $2}' |awk -F"<" '{print $1}' |sed 's/localfs/'mnt\\/fs'/g');do
        cifs_path=$(echo "$item" | sed 's/&lt;/\</g'| sed 's/&gt;/\>/g'|sed 's/&quot;/\"/g' | sed 's/&apos;/'\''/g' | sed 's/&amp;/\&/g')
        cifs_Oripath=$item
        /tmp/${tmpNasdir}/nas.sh "${cifs_path}" >/dev/null 2>&1
        if [ $? -ne 0 ];then
            #目录不存在，共享存在
            echo "Share_Type=CIFS//RedundantRatio=no//Share_Dir=$cifs_Oripath"
            LOG "[$FUNCNAME][$LINENO] The cifs directory($cifs_path),origine Dir name($cifs_Oripath) does not exist"
            continue
        fi
        /opt/huawei/snas/script/inspect_mml/snas_GetDirProtectlvl "${cifs_path}" >$MMM_FILE
        if [ $? -ne 0 ];then
           errinfo=`cat $MMM_FILE`
           LOG "[$FUNCNAME][$LINENO] Get RedundantRatio Fail! cifspath=[$cifs_path],errinfo=[$errinfo]"
           continue
        fi
        dos2unix $MMM_FILE >/dev/null 2>&1
        Rdc=$(grep Rdc $MMM_FILE |awk -F= '{print $2}' |tr -d ' ')
        Rdn=$(grep Rdn $MMM_FILE |awk -F= '{print $2}' |tr -d ' ')
        Ratio=$(RedundantRatio $Rdc $Rdn)
        echo "Share_Type=CIFS//RedundantRatio=$Ratio//Share_Dir=$cifs_Oripath"
        LOG "[$FUNCNAME][$LINENO] cifs origine Dir name($cifs_Oripath) Share_Dir:$cifs_path||RedundantRatio:$Ratio ||Rdc:$Rdc||Rdn:$Rdn"
    done
    IFS=$IFS_OLD
    return 0
}

#获取nfs共享目录
function GetNfsShareDir()
{
    /tmp/${tmpdir}/nas.sh "/usr/local/bin/nas_proto_nfs -c show_share -f 1 -t 50" |grep "<share_path>" >$NFS_FILE
    iRet=$?
    if [ $iRet -ne 0 ];then
        sharedirnfs=1
        LOG "[$FUNCNAME][$LINENO] does not nfs sharedir"
    fi

    IFS_OLD=$IFS
    IFS=$'\n'
    for item in $(cat $NFS_FILE | awk -F">" '{print $2}' |awk -F"<" '{print $1}' | sed 's/^/\/mnt\/fs\/share/'g);do
        nfs_path=$(echo "$item" | sed 's/&lt;/\</g'| sed 's/&gt;/\>/g'|sed 's/&quot;/\"/g' | sed 's/&apos;/'\''/g' | sed 's/&amp;/\&/g')
        nfs_Oripath=${item}
        /tmp/${tmpNasdir}/nas.sh "${nfs_path}" >/dev/null 2>&1
        if [ $? -ne 0 ];then
            echo "Share_Type=NFS//RedundantRatio=no//Share_Dir=$nfs_Oripath"
            LOG "[$FUNCNAME][$LINENO] nfs origine Dir($nfs_Oripath),The nfs directory($nfs_Oripath)($nfs_path) does not exist"
            continue
        fi
        /opt/huawei/snas/script/inspect_mml/snas_GetDirProtectlvl "${nfs_path}" >$MMM_FILE
        if [ $? -ne 0 ];then
           errinfo=`cat $MMM_FILE`
           LOG "[$FUNCNAME][$LINENO] Get RedundantRatio Fail! nfspath=[$nfs_path],errinfo=[$errinfo]"
           continue
        fi
        dos2unix $MMM_FILE >/dev/null 2>&1
        Rdc=$(cat $MMM_FILE |grep Rdc |awk -F= '{print $2}')
        Rdn=$(cat $MMM_FILE |grep Rdn |awk -F= '{print $2}')
        Ratio=$(RedundantRatio $Rdc $Rdn)
        echo "Share_Type=NFS//RedundantRatio=$Ratio//Share_Dir=$nfs_Oripath"
        LOG "[$FUNCNAME][$LINENO] nfs origine Dir($nfs_Oripath) Share_Dir:$nfs_path||RedundantRatio:$Ratio ||Rdc:$Rdc||Rdn:$Rdn"
    done
    IFS=$IFS_OLD
}

tmpdir=$(date +%s)
mkdir -p /tmp/${tmpdir}
touch /tmp/${tmpdir}/nas.sh
echo "#!/bin/bash" >/tmp/${tmpdir}/nas.sh
echo "\$*" >>/tmp/${tmpdir}/nas.sh
chmod +x /tmp/${tmpdir}/nas.sh

tmpNasdir="${tmpdir}/Nas${tmpdir}"
mkdir -p /tmp/${tmpNasdir}
touch /tmp/${tmpNasdir}/nas.sh
echo "#!/bin/bash" >/tmp/${tmpNasdir}/nas.sh
echo "path=\$*" >>/tmp/${tmpNasdir}/nas.sh
echo "stat \"\$path\"" >>/tmp/${tmpNasdir}/nas.sh
chmod +x /tmp/${tmpNasdir}/nas.sh

sharedircifs=0
sharedirnfs=0

GetCifsShareDir

GetNfsShareDir

if [ $sharedircifs -eq 1 ] && [ $sharedirnfs -eq 1 ];then
    echo "Share_Type=--//RedundantRatio=--//Share_Dir=--"
fi

if [ -n "$tmpdir" ] && [ -d "/tmp/${tmpdir}" ];then
    rm -rf /tmp/${tmpdir}
    LOG "[$FUNCNAME][$LINENO] delete tmp Dir:(/tmp/${tmpdir})"
fi
rm -rf $CIFS_FILE
rm -rf $MMM_FILE
rm -rf $NFS_FILE

exit 0
