# -*- shell-script -*-

# 00minimal - Default hardware database routines and variables.

# 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.17 2004/06/09 06:49:44 martins Exp $

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

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

vardir="/var/lib/lsvpd"
db_prefix="db"
db_default="${vardir}/${db_prefix}"
[ -n "$db" ] || db="$db_default"

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

db_set_subdir_vars_hook ()
{
    local i
    for i in $db_subdir_vars_hooks ; do
	$i
    done
}

db_subdir_vars_hooks="db_set_subdir_vars_basic"

db_set_subdir_vars_basic ()
{
    db_bus_subdir="bus"
    db_bus_dir="${db}/${db_bus_subdir}"

    db_os_subdir="linux"
    db_os_dir="${db}/${db_os_subdir}"

    db_misc_dir="${db_os_dir}/misc"
    db_proc_dir="${db_misc_dir}/proc"
    db_uname_dir="${db_misc_dir}/uname"
}

# May get re-run for a different value of $db later.
db_set_subdir_vars_hook

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

vpd_dir_set_hook ()
{
    local node="$1"
    local subnode="$2" ; [ -n "$subnode" ] || subnode="0"

    if [ $subnode -eq 0 ] ; then
	vpd_dir="${node}/linux,vpd/000"
    else
	vpd_dir=$(printf "${node}/linux,vpd/%03d" $subnode)
    fi
}

vpd_exists ()
{
    local node="$1"
    local subnode="$2"

    local vpd_dir
    vpd_dir_set_hook "$node" "$subnode"
    [ -e "$vpd_dir" ]
}

vpd_subdirs_list_hook ()
{
    local node="$1"

    # shopt -s nullglob
    vpd_subdirs=$(echo "${node}/linux,vpd/"[0-9][0-9][0-9])
}

vpd_fields_list_hook ()
{
    local vpd_dir="$1"

    local f="${vpd_dir}/.lsvpd"
    [ -r "$f" ] && read vpd_fields <"$f"
}

# For non-unique field names, this interface can be used in 2 ways.
# The 3 digit sequence number for fields (after the first, numbered 0,
# but omitted) can be appended to the field name (as in ZZ.002), or
# the sequence number can be provided as an optional 3rd argument.
vpd_field_get ()
{
    local vpd_dir="$1"
    local field="$2"
    local seq="$3"

    local f="${vpd_dir}/${field}"
    if [ -n "$seq" ] ; then
	if [ "$seq" -gt 0 ] ; then
	    f=$(printf "%s.%03d" "$f" "$seq")
	fi
    fi
    if [ -r "$f" ] ; then
	local v
	read v <"$f"
	echo "$v"
    fi
}
