#!/bin/bash

str_dir_name=`dirname $0`
. $str_dir_name/xcatlib.sh

#the nics' information contain: 
#1. ip address
#2. nic network
#3. nic type
#4. custom scripts
#all of them are saved by different variable
#this function can split the varable for each nic, and svae the information for each nic
#into an hash, the key is nic name, the value are all information, which joined by ','
function splitconfig(){
    if [ ! "$1" ];then
        return
    fi
    old_ifs=$IFS
    IFS=$','
    array_conf_temp=($1)
    IFS=$old_ifs
    for i in ${array_conf_temp[@]}
    do
        D=
        if [ `echo $i | grep "!"` ];then
            D="!"
        else
            D=":"
        fi
        key=`echo $i | cut -d"$D" -f 1`
        str_temp_value=`echo $i | cut -d"$D" -f 2`
        
        str_temp=$(hashget hash_defined_nics $key)
        if [ -n "$str_temp" ];then
           str_temp=$str_temp",${str_temp_value}"
        else
           str_temp="$str_temp_value"
           str_all_nics=$str_all_nics"$key "
        fi
        hashset hash_defined_nics $key $str_temp
    done
}

bool_cfg_inst_nic=0
str_inst_nic=''
str_ib_nics=''
str_os_type=`uname | tr 'A-Z' 'a-z'`
bool_remove=0
num_iba_ports=
str_all_nics=''
for arg in "$@"
do
    if [ "$arg" = "-s" ];then
        bool_cfg_inst_nic=1
    elif [ "$arg" = "-r" ];then
        bool_remove=1
    elif [ "${arg:0:10}" = "--ibaports" ];then
        num_iba_ports=${arg#--ibaports=}
    fi
done

logger -t xcat -p local4.info "confignics is called: config install nic:$bool_cfg_inst_nic, remove: $bool_remove, iba ports: $num_iba_ports"
echo "confignics on $NODE: config install nic:$bool_cfg_inst_nic, remove: $bool_remove, iba ports: $num_iba_ports"

str_temp=''
if [ ! $INSTALLNIC ];then
    str_temp="mac"
else
    str_temp=$INSTALLNIC
fi

if [ "$str_temp" = "mac" ];then
    if [ "$str_os_type" = "aix" ];then
        old_ifs=$IFS
        IFS=$' '
        str_temp=`ifconfig -l`
        array_nicnames_temp=($str_temp)
        IFS=$old_ifs
        for temp_nic in ${array_nicnames_temp[@]}
        do
            entstat -t $temp_nic | grep -i "$MACADDRESS"
            if [ $? -eq 0 ];then
                str_inst_nic=$temp_nic
                break
            fi
        done
    else
        str_inst_nic=`ifconfig -a | grep -i "$MACADDRESS" | awk '{print $1;}'`
    fi
elif [ `echo $str_temp | grep -E "e(n|th)[0-9]+"` ];then
    str_inst_nic=$str_temp
fi

if [ $bool_cfg_inst_nic -eq 1 ];then
    configeth -s $str_inst_nic 
fi

bool_exit_flag=0
#check the required attributes
if [ -z "$NICIPS" ];then
    if [ -n "$NICTYPES" ];then
        logger -t xcat -p local4.info "confignics: nicips attribute is not defined. so the nictypes attribute can not be defined."
        echo "confignics on $NODE: nicips attribute is not defined. so the nictypes attribute can not be defined."
        bool_exit_flag=1
    fi

    if [ -n "$NICNETWORKS" ];then
        logger -t xcat -p local4.info "confignics: nicips attribute is not defined. so the nicnetworks attribute can not be defined."
        echo "confignics on $NODE: nicips attribute is not defined. so the nicnetworks attribute can not be defined."
        bool_exit_flag=1
    fi
else
    if [ -z "$NICTYPES" ];then
        logger -t xcat -p local4.info "confignics: nictypes attribute is not defined."
        echo "confignics on $NODE: nictypes attribute is not defined."
        bool_exit_flag=1
    fi

    if [ -z "$NICNETWORKS" ];then
        logger -t xcat -p local4.info "confignics: nicnetworks attribute is not defined."
        echo "confignics on $NODE: nicnetworks attribute is not defined."
        bool_exit_flag=1
    fi
fi

if [ $bool_exit_flag -eq 1 ];then
    exit 0
fi

splitconfig $NICIPS
splitconfig $NICTYPES
splitconfig $NICNETWORKS
splitconfig $NICCUSTOMSCRIPTS

#get state of nic in "UP" status
#If bonded redhat then "SLAVE" or "MASTER" will be in the first line of stanza
#do not configure the loopback nic
if [ $bool_remove -eq 1 ];then
    if [ "$str_os_type" = "aix" ];then
        str_temp=`ifconfig -a | grep flags | grep -vi loopback | grep -v SALVE | grep -v MASTER | awk -F: {'print $1'}`
    else
        str_temp=`ip link show | grep -v link | grep -vi loopback | grep -v SALVE | grep -v MASTER | awk {'print $2'} | sed s/://`
    fi
    old_ifs=$IFS
    IFS=$'\n'
    array_nics_temp=($str_temp)
    IFS=$old_ifs
    for str_temp_nic in ${array_nics_temp[@]}
    do
        #the nic type should be ethernet
        echo $str_temp_nic | grep -E "e(n|th)[0-9]+"
        if [ $? -ne 0 ];then
            continue
        fi
        if [ "$str_os_type" != "aix" ];then
            brctl show 2>/dev/null | grep $str_temp_nic
            #the nic belongs to a bridge, go to next
            if [ $? -eq 0 ];then
                continue
            fi
        fi
        #the nic is defined this time
        str_temp=$(hashget hash_defined_nics $str_temp_nic)
        if [ -n "$str_temp" ];then
            continue
        fi
        
        if [ "$str_temp_nic" = "$str_inst_nic" ];then
            continue
        fi
        
        #ignore the vlan interface
        echo $str_temp_nic | grep "\."
        if [ $? -eq 0 ];then
            continue
        fi

        logger -t xcat -p local4.info "confignics: remove nic $str_temp_nic"
        echo "confignics on $NODE: remove nic $str_temp_nic"
        configeth -r $str_temp_nic
    done
fi

old_ifs=$IFS
IFS=$' '
array_nics_temp=($str_all_nics)
IFS=$old_ifs
for key in ${array_nics_temp[@]}
do
    key=`echo $key | sed 's/^ \+//' | sed 's/ \+$//'`
    str_nic_type=
    str_value=$(hashget hash_defined_nics $key)
    if [ "$key" = "$str_inst_nic" ];then
        continue
    fi
    old_ifs=$IFS
    IFS=$','
    array_temp=($str_value)
    IFS=$old_ifs

    if [ "${array_temp[3]}" ];then
        logger -t xcat -p local4.info "confignics: processing custom scripts: ${array_temp[3]} for interface $key" 
        echo "confignics on $NODE: processing custom scripts: ${array_temp[3]} for interface $key"
        ${array_temp[3]}
    else
        if [ -n "${array_temp[2]}" ];then
            logger -t xcat -p local4.info "confignics: ip address,nic type and network are required. $key: $str_value ."
            echo "confignics on $NODE: ip address,nic type and network are required. $key: $str_value ."
            continue
        fi
        if [ "${array_temp[1]}" ];then
            str_nic_type=`echo ${array_temp[1]} | tr "[A-Z]" "[a-z]"`
        else
            if [ `echo $key | grep -E '(eth|en)[0-9]+'` ];then
                str_nic_type="ethernet"
            elif [ `echo $KEY | grep -E 'ib[0-9]+'` ];then
                str_nic_type="infiniband"
            fi
        fi

        if [ $str_nic_type = "ethernet" ];then
             logger -t xcat -p local4.info "confignics: call 'configeth $key ${array_temp[0]} ${array_temp[2]}'"
             echo "confignics on $NODE: call 'configeth $key ${array_temp[0]} ${array_temp[2]}'"
             configeth $key ${array_temp[0]} ${array_temp[2]}
        elif [ $str_nic_type = "infiniband" ];then
            if [ $str_ib_nics ];then
                str_ib_nics=$str_ib_nics","$key
            else
                str_ib_nics=$key
            fi
        else
            logger -t xcat -p local4.info "confignics: unknown type $str_nic_type for NIC: $key"
            echo "confignics on $NODE: unknown type $str_nic_type for NIC: $key"
        fi
    fi
done

if [ -n "$str_ib_nics" ];then
    logger -t xcat -p local4.info "confignics: executed script: configib for nics: $str_ib_nics, ports: $num_iba_ports"
    echo "confignics on $NODE: executed script: configib for nics: $str_ib_nics, ports: $num_iba_ports"
    NIC_IBNICS=$str_ib_nics NIC_IBAPORTS=$num_iba_ports configib
else
    if [ $bool_remove -eq 1 ];then
        logger -t xcat -p local4.info "confignics: executed script: 'configib -u' to remove all ib nics and configuration files"
        echo "confignics on $NODE: executed script: 'configib -r' to remove all ib nics and configuration files"
        configib
    fi
fi
