#!/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 PROCfg                                         **
#   **                                                                        **
#   ***************************************************************************/

installdir="/usr/sbin"
docdir="/usr/share/doc"
version="1.7.37"
ret=""
overall=""

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

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_remove() {
    remove="y"
    base=`basename "$1"`
    
    if [ -f "$2/$base" ]; then
        
	if [ -z "$overall" ]; then
            prompt_yesno "Remove file \"$2/$base\" (y/n/a) ?" "y" "y/n/a"
            if [ "$ret" = "a" ]; then
                overall="y"
            elif [ "$ret" != "y" ]; then
                remove="n"
            fi
        fi

        if [ "$remove" != "n" ]; then 
            /bin/rm -f "$2/$base" || return 1
        fi
	
    elif [ -d "$2/$1" ]; then
    
	if [ -z "$overall" ]; then
            prompt_yesno "Remove directory \"$2/$1\" and all its contents (y/n/a) ?" "y" "y/n/a"
            if [ "$ret" = "a" ]; then
                overall="y"
            elif [ "$ret" != "y" ]; then
                remove="n"
            fi
        fi

        if [ "$remove" != "n" ]; then 
            /bin/rm -rf "$2/$1" || return 1
        fi 
	
    else 
       echo "Error: wrong file type of \"$2/$1\" " 
    fi 

    return 0
}

usage(){
    echo "Intel(R) Linux* LAN Adapters procfg, version $version"
    echo "Usage: UNINSTALL"
    exit 1
}

if [ $# -gt 0 ]; then
    usage
fi

careful_remove "procfg" "$installdir" || failure

man_cfg="/etc/man.confg"
man_type="man1"
dirlist=`[ -e $man_cfg ] && grep -e "^MANPATH[^_]" $man_cfg | awk '{print $2}'`

if [ -z "$dirlist" ]; then
    dirlist="/usr/share/man /usr/man"
fi

# prune the list down to only values that exist
for dir in $dirlist ; do
    if [ -e "$dir/$man_type" ]; then 
        man_path="$dir" 
        break
    fi
done

if [ -z "$man_path" ]; then
    man_path="/usr/man"
fi

man_path="$man_path/$man_type"

/bin/rm -f $man_path/procfg.1
careful_remove "procfg.1.gz" "$man_path" || failure

#find all document directories and remove them
verdirs=$(/bin/ls $docdir | /bin/grep "^procfg-" | /bin/grep -v "grep") 
for verdir in $verdirs ; do
    careful_remove "$verdir" "$docdir" || failure
done

exit 0