#!/bin/sh

#---------------------------------------------------------------------
#COPYRIGHT (C) UNIFY 2013, All Rights reserved
#
#MODULE          : hwupdate
#LONG-NAME       : HW node.cfg conversion.
#
#PROJECT         : OpenScape Voice
#SUBSYSTEM       : Toolkit.
#
#DESCRIPTION     :
#ADDITIONAL-INFO :
#
#REVISION HISTORY:
#Date
#mm/dd/yy    CQ        Surname    Description of Change
#01/09/14    -         Rosa.A     Initial Code.
#---------------------------------------------------------------------


elementsRX200=(
   "eth0_device_node1,igb",        \
   "eth0_device_node2,igb",        \
   "eth1_device_node1,igb",        \
   "eth1_device_node2,igb",        \
   "eth2_device_node1,igb",        \
   "eth2_device_node2,igb",        \
   "eth3_device_node1,igb",        \
   "eth3_device_node2,igb",        \
   "eth4_device_node1,igb",        \
   "eth4_device_node2,igb",        \
   "eth5_device_node1,igb",        \
   "eth5_device_node2,igb",        \
   "eth6_device_node1,igb",        \
   "eth6_device_node2,igb",        \
   "eth7_device_node1,igb",        \
   "eth7_device_node2,igb",        \
   "hardware_platform_id,WS6",     \
   "hw_platform,RX200",            \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth6",                   \
   "port5,eth7",                   \
   "port6,eth5",                   \
   "port7,eth4",                   \
);

elementsRX330=(
   "eth0_device_node1,e1000",      \
   "eth0_device_node2,e1000",      \
   "eth1_device_node1,e1000",      \
   "eth1_device_node2,e1000",      \
   "eth2_device_node1,e1000",      \
   "eth2_device_node2,e1000",      \
   "eth3_device_node1,e1000",      \
   "eth3_device_node2,e1000",      \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,W",       \
   "hw_platform,RX330",            \
   "cluster_dev,eth5%SPACE%eth1",  \
   "bonding_dev0,eth4%SPACE%eth0", \
   "bonding_dev1,eth6%SPACE%eth2", \
   "bonding_dev2,eth7%SPACE%eth3", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsx3650T=(
   "eth0_device_node1,e1000",      \
   "eth0_device_node2,e1000",      \
   "eth1_device_node1,e1000",      \
   "eth1_device_node2,e1000",      \
   "eth2_device_node1,e1000",      \
   "eth2_device_node2,e1000",      \
   "eth3_device_node1,e1000",      \
   "eth3_device_node2,e1000",      \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,S",       \
   "hw_platform,x3650T",           \
   "cluster_dev,eth2%SPACE%eth4",  \
   "bonding_dev0,eth0%SPACE%eth6", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth3%SPACE%eth7", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);


elementsRX200S7=(
   "eth0_device_node1,igb",        \
   "eth0_device_node2,igb",        \
   "eth1_device_node1,igb",        \
   "eth1_device_node2,igb",        \
   "eth2_device_node1,igb",        \
   "eth2_device_node2,igb",        \
   "eth3_device_node1,igb",        \
   "eth3_device_node2,igb",        \
   "eth4_device_node1,igb",        \
   "eth4_device_node2,igb",        \
   "eth5_device_node1,igb",        \
   "eth5_device_node2,igb",        \
   "eth6_device_node1,igb",        \
   "eth6_device_node2,igb",        \
   "eth7_device_node1,igb",        \
   "eth7_device_node2,igb",        \
   "hardware_platform_id,WS7",     \
   "hw_platform,RX200S7",            \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsx3550M2=(
   "eth0_device_node1,bnx2",       \
   "eth0_device_node2,bnx2",       \
   "eth1_device_node1,bnx2",       \
   "eth1_device_node2,bnx2",       \
   "eth2_device_node1,bnx2",       \
   "eth2_device_node2,bnx2",       \
   "eth3_device_node1,bnx2",       \
   "eth3_device_node2,bnx2",       \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,T",       \
   "hw_platform,x3550M2",          \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsx3550M3=(
   "eth0_device_node1,bnx2",       \
   "eth0_device_node2,bnx2",       \
   "eth1_device_node1,bnx2",       \
   "eth1_device_node2,bnx2",       \
   "eth2_device_node1,bnx2",       \
   "eth2_device_node2,bnx2",       \
   "eth3_device_node1,bnx2",       \
   "eth3_device_node2,bnx2",       \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,TM3",     \
   "hw_platform,x3550M3",          \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsx3550M4=(
   "eth0_device_node1,bnx2",       \
   "eth0_device_node2,bnx2",       \
   "eth1_device_node1,bnx2",       \
   "eth1_device_node2,bnx2",       \
   "eth2_device_node1,bnx2",       \
   "eth2_device_node2,bnx2",       \
   "eth3_device_node1,bnx2",       \
   "eth3_device_node2,bnx2",       \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,TM4",     \
   "hw_platform,x3550M4",          \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsx3550M5=(
   "eth0_device_node1,bnx2",       \
   "eth0_device_node2,bnx2",       \
   "eth1_device_node1,bnx2",       \
   "eth1_device_node2,bnx2",       \
   "eth2_device_node1,bnx2",       \
   "eth2_device_node2,bnx2",       \
   "eth3_device_node1,bnx2",       \
   "eth3_device_node2,bnx2",       \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,TM5",     \
   "hw_platform,x3550M5",          \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsSR530=(
   "eth0_device_node1,bnx2",       \
   "eth0_device_node2,bnx2",       \
   "eth1_device_node1,bnx2",       \
   "eth1_device_node2,bnx2",       \
   "eth2_device_node1,bnx2",       \
   "eth2_device_node2,bnx2",       \
   "eth3_device_node1,bnx2",       \
   "eth3_device_node2,bnx2",       \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,SR5",     \
   "hw_platform,SR530",            \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsSR630=(
   "eth0_device_node1,bnx2",       \
   "eth0_device_node2,bnx2",       \
   "eth1_device_node1,bnx2",       \
   "eth1_device_node2,bnx2",       \
   "eth2_device_node1,bnx2",       \
   "eth2_device_node2,bnx2",       \
   "eth3_device_node1,bnx2",       \
   "eth3_device_node2,bnx2",       \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,SR6",     \
   "hw_platform,SR630",            \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsSR630V3=(
   "eth0_device_node1,bnx2",       \
   "eth0_device_node2,bnx2",       \
   "eth1_device_node1,bnx2",       \
   "eth1_device_node2,bnx2",       \
   "eth2_device_node1,bnx2",       \
   "eth2_device_node2,bnx2",       \
   "eth3_device_node1,bnx2",       \
   "eth3_device_node2,bnx2",       \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,SR6V3",   \
   "hw_platform,SR630V3",          \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsGENERIC=(
   "eth0_device_node1,bnx2",       \
   "eth0_device_node2,bnx2",       \
   "eth1_device_node1,bnx2",       \
   "eth1_device_node2,bnx2",       \
   "eth2_device_node1,bnx2",       \
   "eth2_device_node2,bnx2",       \
   "eth3_device_node1,bnx2",       \
   "eth3_device_node2,bnx2",       \
   "eth4_device_node1,e1000",      \
   "eth4_device_node2,e1000",      \
   "eth5_device_node1,e1000",      \
   "eth5_device_node2,e1000",      \
   "eth6_device_node1,e1000",      \
   "eth6_device_node2,e1000",      \
   "eth7_device_node1,e1000",      \
   "eth7_device_node2,e1000",      \
   "hardware_platform_id,Generic",   \
   "hw_platform,Generic",          \
   "cluster_dev,eth3%SPACE%eth7",  \
   "bonding_dev0,eth0%SPACE%eth4", \
   "bonding_dev1,eth1%SPACE%eth5", \
   "bonding_dev2,eth2%SPACE%eth6", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,eth4",                   \
   "port5,eth5",                   \
   "port6,eth6",                   \
   "port7,eth7",                   \
);

elementsVirtual=(
   "eth0_device_node1,e1000",      \
   "eth0_device_node2,e1000",      \
   "eth1_device_node1,e1000",      \
   "eth1_device_node2,e1000",      \
   "eth2_device_node1,e1000",      \
   "eth2_device_node2,e1000",      \
   "eth3_device_node1,e1000",      \
   "eth3_device_node2,e1000",      \
   "eth4_device_node1,",           \
   "eth4_device_node2,",           \
   "eth5_device_node1,",           \
   "eth5_device_node2,",           \
   "eth6_device_node1,",           \
   "eth6_device_node2,",           \
   "eth7_device_node1,",           \
   "eth7_device_node2,",           \
   "hardware_platform_id,V",       \
   "hw_platform,Virtual",          \
   "cluster_dev,eth3%SPACE%eth3",  \
   "bonding_dev0,eth0%SPACE%eth0", \
   "bonding_dev1,eth1%SPACE%eth1", \
   "bonding_dev2,eth2%SPACE%eth2", \
   "port0,eth0",                   \
   "port1,eth1",                   \
   "port2,eth2",                   \
   "port3,eth3",                   \
   "port4,",                       \
   "port5,",                       \
   "port6,",                       \
   "port7,",                       \
   "mtc_ctl_url_1,",               \
   "mtc_ctl_url_2,",               \
   "rsa_1_gateway,0.0.0.0",        \
   "rsa_2_gateway,0.0.0.0",        \
   "rsa_1_ip,0.0.0.0",             \
   "rsa_2_ip,0.0.0.0",             \
   "rsa_1_netmask,0.0.0.0",        \
   "rsa_2_netmask,0.0.0.0",        \
   "rsa_enabled,NO",               \
   "raid_dev,",                    \
);

recomputecksum( )
{
  #<FilterAndRecompute>
     CKSUMCFG=$1
     cat $CKSUMCFG| sed -e 's/\t/ /g' > ${CKSUMCFG}.col
     numcfglines=`wc -l ${CKSUMCFG}.col | awk '{ print $1 }'`
     if [ $numcfglines -le 2 ];
     then
        echo "Sorry, too small a node.cfg"
        rexit $NoCfgFile
     else
        numcfglines=`expr $numcfglines - 2`
     fi
     head -n $numcfglines  ${CKSUMCFG}.col > /tmp/ncpe.garbage
     checksum=`md5sum /tmp/ncpe.garbage | awk '{ print $1 }'`
     echo >> /tmp/ncpe.garbage
     echo "# checksum: $checksum" >>/tmp/ncpe.garbage
     cp -f /tmp/ncpe.garbage $CKSUMCFG
     rm -f /tmp/ncpe.garbage ${CKSUMCFG}.col
  #</FilterAndRecompute>
}

compute( ) {
   hardware=$1
   case "$hardware" in
   "RX200")
      array=elementsRX200[@]
      ;;
   "RX330")
      array=elementsRX330[@]
      ;;
   "RX200S7")
      array=elementsRX200S7[@]
      ;;
   "x3650T")
      array=elementsx3650T[@]
      ;;
   "x3550M2")
      array=elementsx3550M2[@]
      ;;
   "x3550M3")
      array=elementsx3550M3[@]
      ;;
   "x3550M4")
      array=elementsx3550M4[@]
      ;;
   "x3550M5")
      array=elementsx3550M5[@]
      ;;
   "SR530")
      array=elementsSR530[@]
      ;;
   "SR630")
      array=elementsSR630[@]
      ;;
   "SR630V3")
      array=elementsSR630V3[@]
      ;;
   "Generic")
      array=elementsGENERIC[@]
      ;;
   "Virtual")
      array=elementsVirtual[@]
      ;;
   *)
      echo "[hwupgrade] Unknown hardware. Check your input."
      exit $fail
      ;;
   esac
   elements=(`echo ${!array}`)
   count=${#elements[@])}
   idx=0
   expr="sed "
   while [ "$idx" -lt "$count" ];
   do
      tag=`echo ${elements[$idx]} | awk -F',' '{ print $1 }'`
      val=`echo ${elements[$idx]} | awk -F',' '{ print $2 }'`
      expr="$expr -e s/^$tag.*/$tag:%SPACE%$val/g"
      idx=$[idx+1]
   done

   for fileidx in `find $REPOCFG -iname node.cfg -print`
   do
      $expr $fileidx | sed -e 's/%SPACE%/ /g' > ${fileidx}.new
      cp -f $fileidx{.new,}
      rm -f ${fileidx}.new
      recomputecksum $fileidx
   done 

}

success=0
fail=2

REPOCFG=/repository/config
compute $1
exit $success
