# -*- shell-script -*-

# 00minimal - Default hardware database querying routines.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2002, 2003, 2004

# Maintained by  Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 00minimal,v 1.3 2004/02/19 04:32:21 martins Exp $

# This module is always loaded.
true || return 0

######################################################################

function ensure_db ()
{
    if [ ! -d "$db" ] ; then
	printf "$0: Error - database not initialised \"${db}\".
\tPlease run \"/etc/init.d/lsvpd start\".\n" 1>&2
	exit 1
    fi
}

######################################################################

function list_linux_vpd_files_all ()
{
    local f d

    # Force dereference of $db symlink.
    find "${db_bus_dir}/." -name linux,vpd | \
    while read f ; do
	local t=$(dirname "$f")
	echo "${t}/"
    done | \
    sort | \
    while read d ; do
	echo "${d}linux,vpd"
    done
}

######################################################################

function get_model ()
{
    get_model_basic
}

function get_serial ()
{
    get_serial_basic
}

function get_model_basic ()
{
    local model

    local f="${db_misc_dir}/model"
    [ -r "$f" ] && read model <"$f"

    if [ -z "$model" ] ; then
	f="${db_proc_dir}/cpuinfo"
	if [ -r "$f" ] ; then
	    local i
	    for i in "machine" "model name" ; do
		model=$(sed -n -e "s/^${i}[ \t]*:[ \t]*//p" "$f" | \
		    { local x ; read x ; echo $x ; } )
		[ -n "$model" ] && break
	    done
	fi
    fi

    [ -n "$model" ] || model="unknown"

    echo "$model"
}

function get_serial_basic ()
{
    local serial

    local f="${db_misc_dir}/serial"
    [ -r "$f" ] && read serial <"$f"

    if [ -z "$serial" ] ; then
	f="${db_uname_dir}/nodename"
	[ -r "$f" ] && read serial <"$f"
    fi

    [ -n "$serial" ] || serial="unknown"

    echo "$serial"
}

######################################################################

function stripkey ()
{
    case "$1" in
	\**)
            echo "${1#\*}"
	    ;;
    esac
}
