#!/bin/sh
#   /***************************************************************************
#   **                                                                        **
#   **  License                                                               **
#   **  =======                                                               **
#   **                                                                        **
#   **  This software program is released under the terms of a license        **
#   **  agreement between you ('Licensee') and Intel. Do not use or load this **
#   **  software or any associated materials (collectively, the 'Software')   **
#   **  until you have carefully read the full terms and conditions of the    **
#   **  LICENSE located in this software package. By loading or using the     **
#   **  Software, you agree to the terms of this Agreement. If you do not     **
#   **  agree with the terms of this Agreement, do not install or use the     **
#   **  Software.                                                             **
#   ***************************************************************************/
#
#   /***************************************************************************
#   ** INTEL CORPORATION                                                        
#   ** This software is supplied under the terms of the license included     
#   ** above.  All use of this software must be in accordance with the terms   
#   ** of that license.                                      
#   **                                                       
#   **  Abstract:                                            
#   **    Installation script for inic-snmp                  
#   **                                                       
#   ***************************************************************************/

version="1.8.8f"
docs="README LICENSE ldistrib.txt"
installdir="/usr/local/sbin"
mibdir=""
mib_file="INTELLAN.txt"
oid=".1.3.6.1.4.1.343.2.7.2"
snmpd_conf=""
overall=""

prompt_yesno() {
    echo -n "$1 [$2] "
    read ret

    if [ -z "$ret" ]; then
        ret="$2"
    else
        flag=0
        while [ $flag -ne 1 ]; do 
            case $ret in
            y* | Y*)
                ret="y"
                flag=1
                ;;
            a* | A*) 
                ret="a"
                flag=1  
                ;;
            n* | N*)
                ret="n"
                flag=1
                ;;
            *)
                echo -n "$1 [$2] "
                read ret
                if [ -z "$ret" ]; then
                    ret="$2"
                    flag=1
                fi  
                ;;
            esac
        done 
    fi
}

careful_copy() {
    copy="y"
    base=`basename "$1"`
    if [ -f "$2$base" -a -z "$overall" ]; then
        prompt_yesno "File \"$2$base\" exists, overwrite (y/n/a) ?" "y"
        if [ "$ret" = "a" ]; then
            overall="y"
        elif [ "$ret" != "y" ]; then
            copy="n"
        fi
    fi

    if [ "$copy" != "n" ]; then 
        /bin/mkdir -p "$2"
        /bin/cp -f "$1" "$2" || return 1
    fi

    return 0
}

failure() {
    echo "Operation failed$1"
    exit 1
}

get_arch() {
  arch=$(uname -m | sed 's/i.86/ia32/')
  if [ $arch != "ia32" ]; then
      if [ $arch != "ia64" ]; then
         if [ $arch != "x86_64" ]; then
             failure ": unknown architecture."
         fi
      fi
  fi
  return 0
}

get_snmpd_conf(){
    
    man snmpd.conf | col -b | grep /.*/snmpd.conf | grep configuration | grep file > /tmp/snmpddir || failure ": snmpd configuration file unknown. Validate snmp agent is installed."
    snmpd_dirs=`cat /tmp/snmpddir`

    for elem in $snmpd_dirs ; do
	if  echo $elem | egrep "^/.*/snmpd.conf$" > /dev/null; then
	    snmpd_conf=$elem
	    break
	fi
    done
    
    /bin/rm -f /tmp/snmpddir
    
    if [ -z $snmpd_conf ]; then
        man snmpd.conf | col -b | grep /.*/ucdsnmpd.conf | grep configuration | grep file > /tmp/snmpddir || failure ": snmpd configuration file unknown. Validate snmp agent is installed."
        snmpd_dirs=`cat /tmp/snmpddir`

        for elem in $snmpd_dirs ; do
            if  echo $elem | egrep "^/.*/ucdsnmpd.conf$" > /dev/null; then
                snmpd_conf=$elem
                break
            fi
        done
	
        /bin/rm -f /tmp/snmpddir
	
        if [ -z $snmpd_conf ]; then
            failure ": snmpd configuration file unknown."
        fi  
    fi
  
    grep "SuSE" /etc/issue > /dev/null 2>&1
    if [ "$?" -eq 0 ]; then
        return 0
    fi
   
    if [ -f "/etc/redhat-release" ]; then
        redhat=$(cat /etc/redhat-release | awk -F "release" '{print $2}' | awk -F " " '{print $1}' | awk -F "[A-Z, .]" '{print $1 $2}')
    else 
        redhat=""
    fi 
    
    if [ "$redhat" = "29" ] || [ "$redhat" = "3" ] || [ "$redhat" = "9" ] || [ "$redhat" = "" ]; then
        mkdir -p $(echo $snmpd_conf | awk -F "/snmpd.conf"  '{print $1}')
        touch $snmpd_conf 
        snmpd_conf="/etc/snmp/snmpd.conf"
        return 0
    fi
      
    return 0
}

get_mibdir(){
    man snmpd | col -b | grep /mibs > /tmp/mibdir || failure ": MIBs directory unknown. Validate snmp agent is installed."
    mib_dirs=`cat /tmp/mibdir`

    for elem in $mib_dirs ; do
	if  echo $elem | egrep "^/usr/(.*)/mibs/$" > /dev/null; then
	    mibdir=$elem
	    break
	fi
    done
    
    if [ -z $mibdir ]; then
	failure ": MIBs directory unknown."
    fi

    /bin/rm -f /tmp/mibdir
    return 0
}

configure_snmpd(){
    
    copy="y"

    if [ ! -f $snmpd_conf ]; then
	echo "Creating $snmpd_conf..."	
	touch  $snmpd_conf
    else
	echo "Editing $snmpd_conf..."
    fi
  
    grep "^\( *\)pass $oid" $snmpd_conf >& /dev/null
   
    if [ $? != 0 ]; then  
        echo >> $snmpd_conf 
	echo pass $oid $installdir/inic_extension >> $snmpd_conf 
    else
        copy="y"
        if [ "$overall" !=  "y" ]; then 
            prompt_yesno "Pass for $oid exists in $snmpd_conf, overwrite (y/n/a) ?" "y"
            if [ "$ret" = "a" -o "$ret" = "A" ]; then
                overall="y"
            elif [ "$ret" != "y" -a "$ret" != "Y" ]; then
                copy="n"
            fi
        fi
        if [ "$copy" != "n" ]; then 
            perl -i -p -e "s{^( *)pass "$oid" (.*)}{pass $oid $installdir/inic_extension}" $snmpd_conf 
        fi
    fi
  
    return 0
}

usage(){
    echo "Intel(R) Linux* LAN Adapters inic-snmp INSTALL, version $version"
    echo "Usage: INSTALL [-i <install_directory>][-c <snmpd_config_file>]"
    exit 1
}

while [ $# -gt 0 ]
do
  if [ $# -eq 1 ]; then
     usage
  fi
  case $1 in
  -i) installdir=$2
      ;;
  -c) snmpd_conf=$2
      ;;
  *) usage
      ;;
  esac
  shift
  shift
done

get_arch
if [ -z $snmpd_conf ]; then
    get_snmpd_conf
fi

get_mibdir

echo "Installing inic-snmp in $installdir/..." 
careful_copy $arch/inic_daemon $installdir/ || failure
careful_copy $arch/inic_extension $installdir/ || failure

docdir="/usr/share/doc/inic-snmp-$version"
/bin/mkdir -p $docdir

echo "Installing documentation in $docdir... " 
for file in $docs ; do
    careful_copy "$file" "$docdir/" || failure
done

for file in $mib_file ; do
    echo "Copying $file to $mibdir... " 
    careful_copy $file "$mibdir" || failure
done

configure_snmpd

perl -i -p -e "s{installdir=(.*)}{installdir=\"$installdir\"}" UNINSTALL
perl -i -p -e "s{snmpd_conf=(.*)}{snmpd_conf=\"$snmpd_conf\"}" UNINSTALL

exit 0


