# -*- 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.11 2004/06/18 06:59:47 martins Exp $

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

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

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

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

dynamic_vpd_hooks=""

run_dynamic_vpd_hooks ()
{
    for i in $dynamic_vpd_hooks ; do
	$i
    done
}

linux_dynamic_vpd="linux,dynamic-vpd"

dynamic_vpd_ok ()
{
    # bashism
    [ "$EUID" -eq 0  -a "$db" = "$db_default" ]
}

node_contains_dynamic_vpd ()
{
    local node="$1"

    [ -e "${node}/${linux_dynamic_vpd}" ]
}

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

list_linux_vpd ()
{
    list_linux_vpd_basic "$db_bus_dir"
}

list_linux_vpd_basic ()
{
    local dir="$1"

    local f d

    # Force dereference of $dir in case it is a symlink.
    find "${dir}/." -name linux,vpd | \
    while read f ; do
	local t="${f%/*}" # dirname
	echo "${t}/"
    done | \
    sort | \
    while read d ; do
	if node_contains_dynamic_vpd "$d" ; then
	    dynamic_vpd_ok && \
		echo "${d}linux,vpd"
	else
	    echo "${d}linux,vpd"
	fi
    done
}

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

get_model ()
{
    get_model_basic
}

get_serial ()
{
    get_serial_basic
}

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
		# This gets the first CPU - so it works on SMP.
		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"
}

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"
}
