#!/bin/sh
#
# Copyright (C) 1995  Lars Berntzon
#
##################################################################
#		H O S T I N F O
#		---------------
# Description:
#	This script is responsible for getting all information
#	about a host that is stored in the machines database.
#	The output from this command is rows with three columns.
#	The first column is the database column name, e.g. 'key',
#	'osname' e.t.c. The second column is the name of the machine
#	The third column is the value or values separated by semicolons.
#
# Usage:
#	hostinfo [-d]
#
#	The -d option also presents a row with the description
#	of the column.
#
##################################################################
PATH=$PATH:/usr/etc:/sbin:/usr/sbin:/etc:/usr/ucb:/usr/lib:/usr/lib/nis
ORIG_PATH=$PATH
TOP=`dirname $0` || TOP=`echo $0 | sed 's%/[^/]*$%%'`
HOST=${HOST:-`uname -n`} || HOST=`hostname`
REL=`uname -r`
ARCH=`(arch) 2> /dev/null` || ARCH=`uname -m`
KARCH=`(uname -m) 2> /dev/null` || KARCH=`arch -k`
SYSNAME=`uname -s`
doDescription=false

export SYSNAME ARCH KARCH HOST PATH ORIG_PATH REL

#
# Various configuration variables to be used by scripts.
#
if [ -x /bin/nawk -o -x /usr/bin/nawk ]; then
    AWK=nawk
else
    AWK=awk
fi
export AWK

#
# Check special arguments.
#
while [ $# != 0 ]
do
    case "$1" in
    -d)
	#
	# Present description of this column (except the key column).
	#
	doDescription=true;;
    esac
    shift
done

#
# Hackers note, scripts may write full rows of data to be fed directly to
# mkmachines. They do this by writing on file descriptor #3, i.e. like:
#
#	echo sAhOsTiNfO <column> $HOST <data> >&3
#
exec 3>&1

#
# Loop through all columns available, the standard in bin and
# all other residing in subdirectory to this top directory;
#
for colfile in $TOP/*/columns.dat
do
    BASE=`dirname $colfile`

    #
    # Setup path, new path for every base directory.
    #
    PATH=$BASE/$SYSNAME:$BASE/$KARCH:$BASE/$ARCH:$BASE:$ORIG_PATH

     
    cat "$colfile" | sed -e '/^#/d' -e '/^[ 	]*$/d' |
    while read col descriptionText
    do
	#
	# Present a description if requested.
	#
	if [ $doDescription = true ]; then
	    if [ "$descriptionText" != "" ]; then
		echo sAhOsTiNfO $col description.text $descriptionText
	    fi
	else
	    #
	    # Execute the column scripts, just don't care about errors.
	    #
	    result=`($col) 2> /dev/null < /dev/null |
		    sed 's/;/_/g' |
		    $AWK 'BEGIN{line=""}; {line=line sep $0; sep=";"}; END{print line}'`
	    if [ ! -z "$result" ]; then
		echo sAhOsTiNfO $col $HOST $result
		echo sAhOsTiNfO ${col}_date $HOST `date`
	    fi
	fi
    done
done

#
# History of changes:
# -------------------
# hostinfo,v
# Revision 1.10  1998/02/13 09:21:18  lasse
# Also description text should be printed with sAhOsTiNfO prepended.
#
# Revision 1.9  1997/09/27 18:39:03  lasse
# Added hostinfo tagging
#
# Revision 1.8  1996/03/12 19:42:20  lasse
# Checking in from TN.
#
# Revision 1.7  1995/10/16  22:51:41  lasse
# bckup
#
# Revision 1.6  1995/09/23  13:46:36  lasse
# Imported
#
# Revision 1.2  1995/09/23  10:48:24  qdtlarb
# Sending home
#
# Revision 1.1.1.1  1995/09/11  09:23:13  qdtlarb
# THis is version 0.6
#
# Revision 1.5  1995/09/10  20:42:09  lasse
# Added copyright to all
#
# Revision 1.4  1995/09/10  19:04:54  lasse
# Added log keyword
#
# Revision 1.15  1995/08/04  15:18:15  qdtlarb
# Added description of columns
#
# Revision 1.14  1995/08/04  14:52:30  qdtlarb
# Uses various number of columns and commands
#
# Revision 1.13  1995/08/04  11:18:40  qdtlarb
# backup
#
# Revision 1.12  1995/08/04  10:54:48  qdtlarb
# Removed the key column from hostinfo, in mkmachines instead
#
# Revision 1.11  1995/07/24  12:27:56  qdtlarb
# backup
#
# Revision 1.10  1995/07/24  12:13:18  qdtlarb
# changed from nis+server to nisplusserver
#
# Revision 1.9  1995/07/24  11:48:10  qdtlarb
# Changed name of host-id host_id and all .dates to _date
#
# Revision 1.8  1995/07/24  11:11:45  qdtlarb
# backup
#
# Revision 1.7  1995/07/24  10:50:59  qdtlarb
# backup
#
# Revision 1.6  1995/07/24  09:36:35  qdtlarb
# backup
#
# Revision 1.5  1995/07/24  09:00:37  qdtlarb
# backup
#
# Revision 1.4  1995/07/20  13:48:10  qdtlarb
# Modifiesd how PATH is set.
#
# Revision 1.3  1995/07/20  13:44:13  qdtlarb
# Many changes in sahostinfo, especially for HP/UX
#
# Revision 1.2  1995/07/17  08:26:22  qdtlarb
# Minor corrections for error output
#
# Revision 1.1.1.1  1995/07/17  07:51:38  qdtlarb
# Original V0_3
#
# Revision 1.2  1995/07/16  15:08:36  lasse
# Backup
#
# Revision 1.1  1995/06/15  11:32:43  lasse
# Created
#
#
