#!/bin/sh
#
#    Copyright (c) 1996-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Common shell functions and macros utilized by all init scripts
#

#
# Common directories
#

BINDIR=//bin
DATADIR=//share
CONFDIR=/etc
LIBDIR=//lib
LIBEXECDIR=//libexec
LOGDIR=/var
MANDIR=//man
SBINDIR=//sbin
USRDIR=//usr
USRBINDIR=//usr/bin
USRLIBDIR=//usr/lib
USRLIBEXECDIR=//usr/libexec
USRSBINDIR=//usr/sbin

CONFIGDIR=${CONFDIR}

#
# Common executables and files
#

CHECK='/sbin/chkconfig'
HOSTNAME='/bin/hostname'
KILLALL='/usr/bin/killall -q'

NULL='/dev/null'

#
# Other, compound macros
#

PATH=//sbin://bin://usr/sbin://usr/bin
export PATH

if $CHECK verbose ; then
    ECHO='echo -e'
    VERBOSE=-v
    QUIET=
else
    ECHO=:
    VERBOSE=
    QUIET=-q
fi

#
# bpid
#
# Retrieve the current blade processor number, assuming a sin/hinv input
# stream.
#
bpid() {
    sed -n -e 's/^Con.\+No: \([[:digit:]]\{1,\}\).*$/\1/gp'
}

#
# swbd
#
# Retrieve the current system platform name, of the form "SWBDn", where n
# is cardinal number, assuming a sin/hinv input stream.
#
swbd() {
    sed -n -e 's/^.\+\(SWBD[[:digit:]]\{1,\}\).\+$/\1/gp'
}

# RE pattern to match an IP address in dotted-decimal formation. Note,
# however, that no range checking is performed. This could match
# 999.751.330.456 as we as it could 192.168.1.1.

IPSED="\(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}\)"

# RE pattern to match an IP host name, which may include all upper-
# and lower-case alpha characters, decimal digits, underscores (_),
# dashes (-) and periods (.).

HOSTSED="\([[:alnum:]\._-]\+\)"

#
# sysaddr <key>
#
# Retrieve the system address appropriate for <key>, assuming an input
# stream appropriate for an Internet host name database file (e.g.
# /etc/hosts, ypcat hosts, etc.).
#
sysaddr() {
    # 1) Find the line matching on <key>. If there's a match:
    #    a) Grab the first occurrence of an IP address.
    #    b) Delete everything else.
    #    c) Print it.
    # 2) Otherwise, throw away any other line.

    sed -e "/${1}/{; \
		s/^[[:space:]]*${IPSED}[[:space:]].\+$/\1/gp; \
	    }; \
	    /^.*$/d"
}

#
# sysname <key>
#
# Retrieve the system name appropriate for <key>, assuming an input
# stream appropriate for an Internet host name database file (e.g.
# /etc/hosts, ypcat hosts, etc.).
#
sysname() {
    # 1) Find the line matching on <key>. If there's a match:
    #    a) Delete any occurences of IP addresses.
    #    b) Grab the first hostname with characters from [[:alnum:]._-].
    #    c) Delete everything else.
    #    d) Print it.
    # 2) Otherwise, throw away any other line.

    sed -e "/${1}/{; \
		s/^[[:space:]]*${IPSED}[[:space:]]*//g; \
		s/\(^${HOSTSED}\)[[:space:]].\+$/\1/gp; \
	    }; \
	    /^.*$/d"
}
