#!/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.46 2004/06/21 23:37:03 martins Exp $

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

libdir="/lib/lsvpd"

. "/lib/lsvpd/lsvpd-functions.bash"

shopt -s nullglob

for i in /lib/lsvpd/common.d/* ; do
    . "$i"
done

for i in /lib/lsvpd/scan.d/* ; do
    . "$i"
done

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

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
debug=false

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

[ -n "$1" ] && usage

. "/lib/lsvpd/debug.bash"

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

db_prepare ()
{
    local db="${vardir}/${db_prefix}"

    # Keep old device-trees, relying on something else to clean them up.
    if [ -d "$db" -a ! -h "$db" ] ; then
	# If old directory instead of symlink, move it to a safe place.
	# This is easier than trying to figure out when the directory was made.
	mv "$db" "${db}-0000-00-00-000000"
    else
	# Otherwise just remove whatever's there (symlink, plain file, ...).
	rm -f "$db" || {
	    echo "$0: fatal error: failed to remove old device-tree \"${db}\"" 1>&2
	    exit 1
	}
    fi
}

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

process_adapters_of_type ()
{
    local type="$1"

    adapter_setup "$type"

    # $num is only used for adapters that the OS doesn't seem to know about.
    # At the moment, this is needed on non-sysfs systems.
    local num=500
    for n in $(list_adapters "$type") ; do
	local ax osname
	ax="" ; osname=""
	do_adapter "$type" "$n"
	adapter_set_ax_osname_hook "$type" "$n"
	if [ -n "$ax" ] ; then
	    do_adapter_name_and_crosslink \
		"$type" "$n" "$ax" "$osname"
	fi
    done
}

process_devices_of_type ()
{
    local type="$1"

    device_setup "$type"

    local n
    for n in $(list_devices "$type") ; do
	local device_bus_dir adapter_os_dir
	do_device "$type" "$n"
	if [ -n "$device_bus_dir" -a -n "$adapter_os_dir" ] ; then
	    local name=$(get_device_name "$type" "$n")
	    if [ -n "$name" ] ; then
		do_device_name_and_crosslink \
		    "$adapter_os_dir" "$name" "$device_bus_dir"
	    fi
	fi
    done
}

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

db_prepare

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

db_initialise

ln -s "$db_basename" "${vardir}/${db_prefix}"

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

# Adapters (before devices, since adapter matching should be done first).
for type in "scsi" "ethernet" "ide" "display" ; do
    process_adapters_of_type "$type"
done

# Devices.
for type in "scsi" "ide" ; do
    process_devices_of_type "$type"
done
