#!/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:                                            
#   **    Uninstall script for inic-snmp                  
#   **                                                       
#   ***************************************************************************/

installdir="/usr/local/sbin"
snmpd_conf=""
mibdir=""
oid=".1.3.6.1.4.1.343.2.7.2"
mib_file="INTELLAN.txt"
docdir="/usr/share/doc"

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
}

get_snmpd_conf(){
    echo "Looking for snmpd configuration file ... "
    man snmpd.conf | col -b | grep /.*/snmpd.conf | grep configuration | grep file > /tmp/snmpddir
    if [ $? != 0 ]; then 
       failure ": snmpd configuration file unknown. Validate snmp agent is installed."
       return 1
    fi
    
    snmpd_dirs=`cat /tmp/snmpddir`

    for elem in $snmpd_dirs ; do
	if  echo $elem | egrep "^/.*/snmpd.conf$" > /dev/null; then
	    uninst_conf=$elem
	    break
	fi
    done
    
    if [ -z $uninst_conf ]; then
        failure ": snmpd configuration file unknown."
    fi

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

clean_snmpd_conf(){
    
    if [ ! -f $uninst_conf ]; then
       return 0
    fi
  
    echo "Removing line \"pass $oid $installdir/inic_extension\" from $uninst_conf ..."
    perl -i -p -e "s{^( *)pass "$oid" "$installdir"/inic_extension\n}{}" $uninst_conf
  
    return 0
}

get_mibdir(){
    echo "Looking for MIBs directory..."
    man snmpd | col -b | grep /mibs > /tmp/mibdir 
    if [ $? != 0 ]; then 
        failure ": MIBs directory unknown. Validate snmp agent is installed."
        return 1
    fi 
    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
}

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

failure() {
    echo "Operation failed$1"
    return 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

uninstdir=$installdir
uninst_conf=$snmpd_conf

echo "Removing inic-snmp from $uninstdir..."
/bin/rm -f $uninstdir/inic_daemon >& /dev/null
/bin/rm -f $uninstdir/inic_extension >& /dev/null

if [ -z $uninst_conf ]; then
   get_snmpd_conf
fi

if [ "$uninst_conf" != "" ]; then
    clean_snmpd_conf
fi

get_mibdir
if [ "$mibdir" != "" ]; then
    for file in $mib_file ; do
        echo "Removing $mibdir$file ..."
        /bin/rm -f $mibdir$file >& /dev/null
    done
fi

echo "Removing inic-snmp documents from $docdir ..."
/bin/rm -rf $docdir/inic-snmp-*

exit 0
