#!/bin/bash
# $Header: /cvs/WebShield/wsrc/files/mgmt/sysinfo-functions,v 1.10 2002/05/27 15:50:09 bwhittak Exp $
# functions used for system management scripts

shopt -s extglob
shopt -s nullglob

# function to grab existing system information and write relevant variables in
# a form suitable for eval
GetSys()
{
    q="'" Q="'\''"
    HOSTNAME=`hostname`
    [[ -f /etc/sysconfig/network ]] && . /etc/sysconfig/network
    for v in HOSTNAME GATEWAY; do
	echo "$v='${!v//$q/$Q}'"
    done
}

# function to grab existing Interface information and write relevant variables
# in a form suitable for eval
GetInt()
{
    local q="'" Q="'\''"
    ifile=/etc/sysconfig/network-scripts/ifcfg-$1
    if [[ -f "$ifile" ]] && . "$ifile"; then
	if [[ "$ONBOOT" != @(no|NO) && -n "$IPADDR" ]]; then
	    echo "STATE$2=on"
	else
	    echo "STATE$2=off"
	fi
	for v in IPADDR NETMASK BROADCAST GATEWAY; do
	    echo "$v$2='${!v//$q/$Q}'"
	done
    else
	for v in STATE IPADDR NETMASK BROADCAST; do
	    echo "$v$2=''"
	done
    fi
}
GetIntA()
{
    local b f n o s v
    eval "local -i ${*/%/=0}"
    for b; do
	for f in /etc/sysconfig/network-scripts/ifcfg-$b*; do
	    n=${f#/etc/sysconfig/network-scripts/ifcfg-}
	    if [[ $o != $n ]]; then
		s=$b[${!b}]
		eval "(( $b++ ))"
		o=$n
		echo "IPADDR$s='' NETMASK$s='' STATE$s=off"
	    fi
	    . $f || continue
	    if [[ "$ONBOOT" != @(no|NO) && -n "$IPADDR" ]]; then
		echo "STATE$s=on"
	    else
		echo "STATE$s=off"
	    fi
	    [[ -n "$IPADDR" ]] && echo "IPADDR$s=$IPADDR"
	    [[ -n "$NETMASK" ]] && echo "NETMASK$s=$NETMASK"
	done
    done
}

# find the pid of the named process if any
Get_named()
{
    ps -e -o pid,args | while read p c a; do
	if [[ "$c" == ?(*/)named ]]; then
	    echo $p
	    return
	fi
    done
}

# What's the named config file name (empty if named isn't set up)
Get_named_file()
{
    local f conf='/etc/named.conf'
    # if the only config file we handle doesn't exist we're out of luck
    [[ -f "$conf" ]] || return 1
    # is there an init script for it?
    [[ -f /etc/init.d/named ]] && for f in /etc/rc3.d/S*named; do
	[[ $f -ef /etc/init.d/named ]] && { echo "$conf"; return 0; }
    done
    # is it running anyway?
    [[ -n "$(Get_named)" ]] && { echo "$conf"; return 0; }
    return 1
}

# function to find domain and nameserver and return in a form suitable for eval
# (must run in a subshell)
GetDNS()
{
    local dom line
    local -a serv nserv
    [[ "$HOSTNAME" = *.* ]] && dom=${HOSTNAME#*.}
    [[ -f /etc/resolv.conf ]] && exec </etc/resolv.conf || exec </dev/null
    while read dir arg rest; do
	[[ -z "$dom" && "$dir" = @(domain|search) ]] && dom=$arg
	[[ "$dir" = nameserver ]] && serv[${#serv[@]}]=$arg
    done
    if named_conf=$(Get_named_file); then
	# read named.conf and extract first (we hope the global) forwarders list
	line=$(<"$named_conf")
	fwpat=$'*forwarders*([ \t]){*([ \t])'
	if [[ "$line" == $fwpat* ]]; then
	    line=${line##$fwpat}
	    line=${line%%'}'*}
	    nserv=( ${line//';'/ } )
	fi
    fi
    [[ -n "$dom" ]] && printf 'DOMAIN=%q\n' "$dom"
    [[ -n "${serv:-$nserv}" ]] && printf 'NSERVER=%q\n' "${serv[*]:-${nserv[*]}}"
    [[ -n "$serv" ]] && printf 'NSERVERr=%q\n' "${serv[*]}"
    [[ -n "$nserv" ]] && printf 'NSERVERn=%q\n' "${nserv[*]}"
}

# function to write content for the /etc/hosts file
# $1 is hostname, $2 is domainname, rest of args are IP addresses
PutHosts()
{
    printf '127.0.0.1\tlocalhost\n'
    [[ -n "$1" && $# -gt 2 ]] && printf "%s\t$1${2:+.$2 $1}\n" "${@:3}"
}
