#######################################################################
# ek_cfg.tcl --
#
#   Read and parse the configuration file for events. Note that
#   the original case is preserved, so modify the file carefully!
#   All procedure names begin with the prefix 'EK_'
#
#   Processes a file in this format:
#
# set set-name
# [id id-list]
# [type graphtype-name]
# [+/-]event
# [+/-]event
#  ..
# set set-name
#   ..etc..
#
#   Fills the param(EVENT, ) array as follows:
#
# param(EVENT,#i)         = name of set #i
# param(EVENT,XXX,#i)     = value of parameter XXX for set #i
# param(EVENT,#i,#j)      = name of event #j in set #i
# param(EVENT,NUMSETS)    = number of sets
# param(EVENT,NUMKEYS,#i) = number of keys in set #i
#
#   Default values (see below) are indicated as:
#
# param(EVENT,DEFAULT,XX) = default value (or prefix) for XX
#
# Author: Dan Gunter
# Date  : 5/8/98
#
#######################################################################

set param(EVENT,DEFAULT,GTYPE)     LINE
set param(EVENT,DEFAULT,ID)        HOST
set param(EVENT,DEFAULT,GROUP)     HOST
set param(EVENT,DEFAULT,HOSTALIAS) NULL
set param(EVENT,DEFAULT,VAL)       NULL
set param(EVENT,DEFAULT,EVIS)      +
set param(EVENT,DEFAULT,SVIS)      +
set param(EVENT,DEFAULT,CALCPARM)  NULL
##############################
# 
#
#
proc EK_readFile { filename } {
    global param
    set ctr 0
    set nextline ""

    if { ![file exists $filename] || ![file readable $filename] } {
	return "!File $filename is not readable."
    }
    set fd [open $filename r]
    set param(EVENT,HOSTALIAS) {}
    set nextline [EK_skipComments $fd [gets $fd]]
    while { ![eof $fd] } {
	while { [string range $nextline 0 8] == "hostalias" } {
	    if { $param(EVENT,HOSTALIAS) != "" } {
		set param(EVENT,HOSTALIAS) \
		    [lappend param(EVENT,HOSTALIAS) "&"]
	    }
	    set param(EVENT,HOSTALIAS) \
		[lappend param(EVENT,HOSTALIAS) \
		     "[string trim [string range $nextline 10 end]]"]
	    set nextline [EK_skipComments $fd [gets $fd]]
	}
	set nextline [EK_readSet $fd $ctr $nextline]
	if { [string range $nextline 0 0] == "!" } {
	    return $nextline
	}
	incr ctr
    }
    set param(EVENT,NUMSETS) $ctr
    if { [llength $param(EVENT,HOSTALIAS)] == 0 } {
	set param(EVENT,HOSTALIAS) $param(EVENT,DEFAULT,HOSTALIAS)
    }
    return
}

##############################
# 
#
#
proc EK_readSet { fd ctr line } {
    global param

    if { $line == "" } {
	set line [EK_cleanLine $fd]
    }
    set line [EK_skipComments $fd $line]
    if { [eof $fd] } {
	return
    }
    if { [string range $line 0 3] == "set " } {
	set nm [string trim [string range $line 3 end]]
	if { $nm == "" } {
	    return "!Empty set name"
	}
	set c [string range $nm 0 0]
	if {  $c != "+" && $c != "-" } {
	    set nm $param(EVENT,DEFAULT,SVIS)$nm
	}
	set param(EVENT,$ctr) $nm
	set r [EK_readHeader $fd $ctr]
	if { [string range $r 0 0] == "!" } {
	    return $r
	}
	set r [EK_readEvents $fd $ctr $r]
	if { [string range $r 0 0] == "!" } {
	    return $r
	}
    } else {
	return "!Word 'set' expected on this line:\n$line"
    }
    return $r
}

##############################
# 
#
#
proc EK_readHeader { fd ctr } {
    global param

    set line [EK_cleanLine $fd]
    set line [EK_skipComments $fd $line]
    if { [eof $fd] } {
	return "!EOF encountered before reading set header."
    }
    set param(EVENT,ID,$ctr) {}
    set param(EVENT,HOSTID,$ctr) {}
    set param(EVENT,GTYPE,$ctr) {}
    set param(EVENT,VAL,$ctr) {}
    set param(EVENT,CALCPARM,$ctr) ""
    set param(EVENT,NUMFORMS,$ctr) 0
    while { 1 } {
	if { [string range $line 0 1] == "id" } {
	    set param(EVENT,ID,$ctr) \
		"[string trim [string range $line 2 end]]"
	} elseif { [string range $line 0 5] == "hostid" } {
	    set param(EVENT,HOSTID,$ctr) \
		"[string trim [string range $line 6 end]]"
	} elseif { [string range $line 0 3] == "type" } {
	    set param(EVENT,GTYPE,$ctr) \
		"[string trim [string range $line 4 end]]"
	} elseif { [string range $line 0 2] == "val" } {
	    set param(EVENT,VAL,$ctr) \
		"[string trim [string range $line 3 end]]"
	} elseif { [string range $line 0 6] == "formula" } {
	    set n $param(EVENT,NUMFORMS,$ctr)
	    set param(EVENT,FORM$n,$ctr) \
		"[string trim [string range $line 7 end]]"
	    incr param(EVENT,NUMFORMS,$ctr)
	} elseif { [string range $line 0 3] == "calc" } {
	    if { $param(EVENT,CALCPARM,$ctr) != "" } {
		set param(EVENT,CALCPARM,$ctr) \
		    [lappend param(EVENT,CALCPARM,$ctr) "&"]
	    }
	    set param(EVENT,CALCPARM,$ctr) \
		[lappend param(EVENT,CALCPARM,$ctr) \
		     "[string trim [string range $line 4 end]]"]
	} else {
	    break
	}
	set line [EK_cleanLine $fd]
	set line [EK_skipComments $fd $line]
    }
    foreach p {ID HOSTID GTYPE CALCPARM} {
	if { [llength $param(EVENT,$p,$ctr)] == 0 } {
	    set param(EVENT,$p,$ctr) $param(EVENT,DEFAULT,$p)
	}
    }
    return $line
}

##############################
# 
#
#
proc EK_readEvents { fd ctr line } {
    global param
    set ectr 0

    if { $line == "" } {
	set line [EK_cleanLine $fd]
    }
    set line [EK_skipComments $fd $line]
    while { ![eof $fd] && [string range $line 0 2] != "set" } {
	set c [string range $line 0 0]
	if { $c != "+" && $c != "-" } {
	    set line $param(EVENT,DEFAULT,EVIS)$line
	}
	set param(EVENT,$ctr,$ectr) $line
	incr ectr
	set line [EK_cleanLine $fd]
	set line [EK_skipComments $fd $line]
    }
    if { $ectr == 0 } {
	return "!EOF encountered before reading any events."
    }
    set param(EVENT,NUMKEYS,$ctr) $ectr
    return $line
}

##############################
# 
#
#
proc EK_cleanLine { fd } {
    return [string trim [gets $fd] " \t"]
}

##############################
# 
#
#
proc EK_skipComments { fd line } {
    while { [string range $line 0 0] == "#" && ![eof $fd] } {
	set line [EK_cleanLine $fd]
    }
    return $line
}
