#!/bin/bash

# update-lsvpd-db - Initialise lsvpd database.

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

# This file is part of the Linux lsvpd package.

# 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: update-lsvpd-db.in,v 1.58 2005/06/16 05:37:34 martins Exp $

if [ -z "$LSVPD_LIBDIR" ] ; then
    LSVPD_LIBDIR="/lib/lsvpd" ; export LSVPD_LIBDIR
fi

PATH="${LSVPD_LIBDIR}:/sbin:${PATH}:/usr/sbin:/usr/local/sbin" ; export PATH

. "${LSVPD_LIBDIR}/lsvpd-functions.bash"

shopt -s nullglob

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

usage () {
    cat <<EOF 1>&2
usage: $0 [options]
options: -h      print this usage message
         -d      Print debug messages to stderr
EOF
    exit 1
}

# Options handling
options=':'
options="${options}h"
options="${options}d"
while getopts ${options} opt
do
    case ${opt}
    in
	h ) usage
	    ;;
	d )
	    LSVPD_DEBUG=true
	    ;;
        \?) echo "unknown option \`${OPTARG}'" 1>&2
	    echo 1>&2
	    usage
            ;;
    esac
done
shift $((${OPTIND} - 1))

[ -n "$1" ] && usage

. "${LSVPD_LIBDIR}/debug.bash"

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

process_buses ()
{
    local sequence_number bus_list bus_info bus_type bus_node

    list_buses

    for bus_info in $bus_list ; do

	bus_type="${bus_info%%@*}"
	bus_node="${bus_info#*@}"

	add_bus "$bus_type" "$bus_node"

	set_sequence_number_hook "bus_${bus_type}"
	if [ -n "$sequence_number" ] ; then
	    local vpd_dir
	    vpd_dir_set_hook "$bus_node"
	    [ -e "$vpd_dir" ] && \
		vpd_field_ensure "$vpd_dir" "AX" "${bus_type}${sequence_number}"
	else
	    : failed to get sequence_number
	fi
    done
}

process_adapters ()
{
    local adapter_list adapter_info type bus_info

    list_adapters

    for adapter_info in $adapter_list ; do
	type="${adapter_info%@*}"
	bus_info="${adapter_info#*@}"
	add_adapter "$type" "$bus_info"
	local ax=""
	local osname=""
	adapter_set_ax_osname_hook "$type" "$bus_info"
	[ -n "$ax" ] && \
	    add_adapter_name_and_crosslink "$type" "$bus_info" "$ax" "$osname"
    done
}

process_devices ()
{
    local device_list device_info type binfo

    list_devices

    for device_info in $device_list ; do
	type="${device_info%/*}"
	binfo="${device_info#*/}"

	add_device "$type" "$binfo"
	local device_name
	get_device_name "$type" "$binfo"
	[ -n "$device_name" ] && \
	    device_add_name_and_crosslink "${type}" "${binfo}" "$device_name"
    done
}

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

do_setup "scan"

datetime=$(date '+%Y-%m-%d-%H%M%S')
db_basename="${db_prefix}-${datetime}"
db="${vardir}/${db_basename}"
LSVPD_DB_DIR="$db" ; export LSVPD_DB_DIR

db_initialise

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

# Buses.
process_buses

# Adapters (before devices, since adapter matching should be done first).
process_adapters

# Devices.
process_devices

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

db_link="${vardir}/${db_prefix}"

# If old directory instead of symlink, move it to a safe place.  This
# should not happen.
[ -d "$db_link" -a ! -L "$db_link" ] && \
    debug_cmd mv "$db_link" "${db_link}-0000-00-00-000000.$$"

if ! debug_cmd ln -snf "$db_basename" "$db_link" ; then
    echo "$0: failed to created updated database link \"$db_link\"" >&2
    exit 1
fi
