Current File : //usr/ddu/scripts/comp_lookup.sh
#!/usr/bin/ksh93
#
# Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
#
#
# Description: Use device compatible name to search for available driver
#              by IPS or local driver db.
#
#              Usage:
#              comp_lookup.sh $1 $2              
#              $1 is for compatible string
#              $2 is IPS publisher name, optional
#

PATH=/usr/bin:/usr/sbin:$PATH; export PATH
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/ddu/lib; export LD_LIBRARY_PATH

#
# Confirm the correct number of commnad line argument
#
if (( $# < 1 )) || (( $# > 2 )); then
    print "$0: Error: Invalid argument." >>$err_log
    exit 1
fi

typeset -r base_dir=$(dirname $0)
typeset -r platform=$(uname -p)
typeset -r bin_dir=$(echo $base_dir |sed 's/scripts$/bin\//')"$platform"
typeset matched_drv=
typeset matched_drv_pkg_type=UNK #SVR4,IPS,DU,P5I,UNK
typeset com_name_str=$1
typeset repo_candi=$2
typeset err_log=/tmp/ddu_err.log

#
# Search through IPS for missing device driver by device 
# compatible string.
#
function find_driver
{
    typeset s_result_file=/tmp/s_result.$$
    typeset ret_val s_result def_repo drv_type candi_drv

    if [[ ! -x $base_dir/pkg_relate.sh ]]; then
        print "$0: Can not find $base_dir/pkg_relate.sh or not executable." \
            >>$err_log
        exit 1
    fi

    #
    # Search for missing driver package via IPS
    #
    if [ ! -z "$repo_candi" ]; then
	ret_val=1
	for repo_index in $repo_candi; do
	    for com_name in $com_name_str; do
        	if [ ! -z "$com_name" ]; then
		    $base_dir/pkg_relate.sh search $com_name $repo_index \
                        > $s_result_file 2>>$err_log
		    if [ $? == 0 ]; then
			ret_val=0
			break
		    fi
		fi
	    done
	    if [ $ret_val -eq 0 ]; then
		s_result=$(cat $s_result_file)
		break
	    fi	
	done
    else
	ret_val=1
	def_repo=$($base_dir/pkg_relate.sh list default \
            2>>$err_log | awk '{print $1}' | uniq)
        if [ $? != 0 ]; then
            print "$0: Error executing ${bin_dir}/$base_dir/pkg_relate.sh"\
                " list default." >>$err_log
            exit 1
        fi
	for com_name in $com_name_str; do
            if [ ! -z "$com_name" ]; then
		$base_dir/pkg_relate.sh search $com_name $repo_candi \
                    > $s_result_file 2>>$err_log
            fi
            ret_val=$?
            if [ $ret_val -eq 0 ]; then
                cat $s_result_file | grep $def_repo >/dev/null 2>&1
                if [ $? -eq 0 ]; then
                    s_result=$(cat $s_result_file | grep $def_repo | sort \
                        | tail -1)
                else
                    s_result=$(cat $s_result_file | sort | tail -1)
                fi
                break
            fi
	done
    fi
    if [ $ret_val -eq 0 ] && [ ! -z $s_result ]; then
        matched_drv=$com_name
        matched_drv_pkg_type=IPS
        echo $s_result | awk '{print $1}' > /tmp/${matched_drv}_info.tmp    
        echo $s_result | awk '{print $2}' > /tmp/${matched_drv}_dlink.tmp    
    else
        #
        # Search for driver package by compatible name has failed.
        # Try searching for the package by driver name via IPS.  
        # Get driver name from the local database.
        #
        # If search for the package by driver name also fails,
        # driver info from local driver db will be returned.
        #
        # The driver info from the local driver db 
        # will include the driver name (as returned 
        # in variable $matched_drv), 
        # and the web URL to download the driver in  
        # /tmp/${matched_drv}_dlink.tmp file, 
        # give the condition of the $matched_drv is
        # not null
        #

        if [[ ! -x ${bin_dir}/find_driver ]]; then
            print "$0: ${bin_dir}/find_driver is missing or not executable." \
                >>$err_log
            exit 1
        fi
        candi_probe=$(${bin_dir}/find_driver -n "$com_name_str")
        if [ $? -eq 0 ]; then
            candi_drv=$(echo $candi_probe | cut -d'|' -f2)
            if [ ! -z "$candi_drv" ]; then
                def_repo=$($base_dir/pkg_relate.sh list default \
                    2>>$err_log | awk '{print $1}' | uniq)
                if [ $? != 0 ]; then
                    print "$0: Error executing" \
                        " ${bin_dir}/$base_dir/pkg_relate.sh" \
                        " list default." >>$err_log
                    #
                    # Don't exit here.  The driver information will be 
                    # returned to the user for display, even if the 
                    # package itself cannot be retrieved.
                    #
                else 
                    $base_dir/pkg_relate.sh search $candi_drv \
                        > $s_result_file 2>>$err_log
                    ret_val=$?
                    if [ $ret_val -eq 0 ]; then
                        cat $s_result_file | grep $def_repo >/dev/null 2>&1
                        if [ $? -eq 0 ]; then
                            s_result=$(cat $s_result_file | grep $def_repo \
                                | sort | tail -1)
                        else
                            s_result=$(cat $s_result_file | sort | tail -1)
                        fi
                    else
                        print "$0: Error executing" \
                            " ${bin_dir}/$base_dir/pkg_relate.sh" \
                            " search $candi_drv." >>$err_log
                        #
                        # Don't exit here.  The driver information will be 
                        # returned to the user for display, even if the 
                        # package itself cannot be retrieved.
                        #
                    fi
                fi

		#
		#drv_type:
		# 1: Solaris Driver
		# 2: Third-Party Driver
		# 3: Opensolaris Driver,
		# 4: No Driver found"
		#
                if [ $ret_val -eq 0 ] && [ ! -z $s_result ]; then
                    matched_drv=$candi_drv
                    matched_drv_pkg_type=IPS
                    echo $s_result | awk '{print $1}' \
                        > /tmp/${matched_drv}_info.tmp    
                    echo $s_result | awk '{print $2}' \
                        > /tmp/${matched_drv}_dlink.tmp    
                else
                    drv_type=$(echo $candi_probe | cut -d'|' -f1)
                    if [ "$drv_type" -eq 2 ] || [ "$drv_type" -eq 3 ]; then
                        matched_drv=$(echo $candi_probe | cut -d'|' -f2)
                        matched_drv_pkg_type=$(echo $candi_probe | cut -d'|' -f5)
                        echo $candi_probe | cut -d'|' -f3 \
                            > /tmp/${matched_drv}_info.tmp
                        echo $candi_probe | cut -d'|' -f6 \
                            > /tmp/${matched_drv}_dlink.tmp
                    fi
                fi
            else
                print "$0: Invalid output format of ${bin_dir}/find_driver" \
                    >>$err_log
                exit 1
            fi
        else
            print "$0: No driver found with ${com_name_str} in local driver DB." \
                >>$err_log
        fi
    fi
    if [ ! -z ${matched_drv} ]; then
        pfexec chmod 666 /tmp/${matched_drv}_info.tmp \
            /tmp/${matched_drv}_dlink.tmp >/dev/null 2>&1
    fi
}


#main
find_driver 
print -u1 "$matched_drv:$matched_drv_pkg_type"