#! /usr/bin/sh
#set environment language
export LANG=C
if [ "x${1:-}" != "xselfrun" ]; then
    /bin/sh $0 selfrun $*
    exit $?
fi
shift

####################
# global variable
####################
PRODUCT_NAME='PCIINFO'
MODULE=pciinfo
VERSION="B.11.31.1209"	    #current pciinfo bundle version
BUNDLE=${MODULE}.depot
TYPE='none'	      #type: install, uninstall, version, status, none

KCMODULE=/usr/sbin/kcmodule
LSDEV=/usr/sbin/lsdev
DEV_C=/dev/${MODULE}

# 0: success
# 1: Error
PCIINFO_EXECUTE_SUCCESS=0
PCIINFO_EXECUTE_ERROR=1

RET_VALUE=$PCIINFO_EXECUTE_SUCCESS

#####################
# function: get_version()
# return: 0 means success, otherwise failed
# usage: get pciinfo version for installed and the one want to install.
#####################
get_version()
{
	echo "in get_version()"

    installed_version=`swlist ${PRODUCT_NAME} | grep ${PRODUCT_NAME} | awk 'NR==1' | awk '{print $3}'` #installed pciinfo bundle version

    echo "version=${VERSION},installed_version=${installed_version}"
	echo "out get_version()"
}

#####################
# function: get_os_version()
# return: 0 means success, otherwise failed
# usage: get os kernel version.
#####################
get_os_version()
{
	echo "in get_os_version()"

    hpux_os_version=`swlist -l product OE | grep OE | awk 'NR==1' | awk '{print $2}'` #installed os version

    echo "hpux_os_version=${hpux_os_version}"
	echo "out get_os_version()"
}

#####################
# function: install_bundle()
# return: 0 means success, otherwise failed
# usage: install pciinfo depot
#####################
install_bundle()
{
	echo "in install_bundle()"
	local ret;
    swinstall -s `pwd`/pciinfo.depot ${PRODUCT_NAME}
	ret=$?
	if [ $ret -ne 0 ]; then #error happen
    {
		echo "Error: Failed at installing pciinfo.depot and errorcode=${ret}"
		RET_VALUE=$PCIINFO_EXECUTE_ERROR
	}
    fi

	echo "out install_bundle()"
	return $ret;
}

#####################
# function: uninstall_bundle()
# return: 0 means success, otherwise failed
# usage: uninstall pciinfo depot
#####################
uninstall_bundle()
{
	echo "in uninstall_bundle()"
	local ret;
	swremove -x autoreboot=true ${PRODUCT_NAME}
	ret=$?
	if [ $ret -ne 0 ]; then #error happen
	{
		echo "Error: Failed at uninstalling pciinfo.depot and errorcode=${ret}"
		RET_VALUE=$PCIINFO_EXECUTE_ERROR
	}
    fi
	echo "out uninstall_bundle()"
	return $ret;
}

#####################
# function: load_pciinfo()
# return: 0 means success, otherwise failed
# usage: load pciinfo module
#####################
load_pciinfo()
{
	echo "in load_pciinfo()"

	local exist=`${KCMODULE} | grep ${MODULE}`
    local loaded=`${KCMODULE} | grep ${MODULE} | grep loaded`
	if [ ! -z "${exist}" ] && [ -z "${loaded}" ]; then
	    ${KCMODULE} -s ${MODULE}=best
	fi

	echo "out load_pciinfo()"
}

#####################
# function: create_device()
# return: NULL
# usage: create pciinfo device file
#####################
create_device()
{
	echo "in create_device()"

    local loaded=`${KCMODULE} | grep ${MODULE} | grep loaded`
	if [ ! -e ${DEV_C} ] && [ ! -z "${loaded}" ]; then
	{
		major=`${LSDEV} -hd ${MODULE} | head -n 1 | awk '{print $1}'`
		if [ ! -z ${major} ]; then
		{
			mknod ${DEV_C} c ${major} 0
	    	local ret=$?
	    	if [ ${ret} -ne 0 ];then
				RET_VALUE=$PCIINFO_EXECUTE_ERROR
	    	fi

		}
		fi
	}
	fi

	echo "out create_device()"
}

#####################
# function: check_status()
# return: 0 means success, otherwise failed
# usage: check whether pciinfo is loaded and the device file is exist, 
#        if not, try to load it and create the device file.
#####################
check_status()
{
	echo "in check_status()"

	load_pciinfo
	create_device

	# double check whether the module is loaded and
	# the device file is exist
	local loaded=`${KCMODULE} | grep ${MODULE} | grep loaded`
	if [ -z "${loaded}" ] || [ ! -e ${DEV_C} ]; then
	    RET_VALUE=$PCIINFO_EXECUTE_ERROR
		echo "Error: The pciinfo module isn't loaded or the /dev/pciinfo isn't exist"
	fi
	echo "out check_status()"
}

####################
# Main
####################
if [ "$1" = 'install' ]; then
{
    echo "install bundle"
	install_bundle
}
elif [ "$1" = 'version' ]; then
{
    echo "get version"
    get_version

}
elif [ "$1" = 'os_version' ]; then
{
    echo "get os version"
    get_os_version

}
elif [ "$1" = 'uninstall' ]; then
{
	echo "uninstall bundle"
	uninstall_bundle
}
elif [ "$1" = 'status' ]; then
{
    echo "check status"
    check_status
}
else
{
    echo "The parameter $1 is wrong and please try it again"
    RET_VALUE=$PCIINFO_EXECUTE_ERROR
}
fi

exit $RET_VALUE
