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

installdir="/usr/sbin"
docdir="/usr/share/doc"
version="1.7.39"
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*)
                if [ "$3" != "y/n" ]; then
                    ret="a" 
                    flag=1
                else  
                    echo -n "$1 [$2] "
                    read ret
                    if [ -z "$ret" ]; then
                        ret="$2"
                        flag=1
                    fi    
                fi
                ;;
            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 procfgd, version $version"
    echo "Usage: UNINSTALL"
    exit 1
}

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

/bin/ps -C procfgd > /dev/null 
if [ $? -eq 0 ]; then
    prompt_yesno "Procfgd is running. Are you sure you want to continue (y/n) ?" "y" "y/n"
    if [ "$ret" == "n" ]; then
        echo "Exiting..."
        exit 0
    fi	   
fi

careful_remove "procfgd" "$installdir" || failure
careful_remove "procfgd_adduser" "$installdir" || failure

man_cfg="/etc/man.config"
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"
careful_remove "procfgd.1.gz" "$man_path" || failure

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

./INSTALL_BOOT uninstall "$overall"

exit 0
