#!/usr/local/bin/tclsh8.6

#
# Get and send the traffic graph
#
# Called by: metro
#
# Parameters (form or url):
#   - id : selected sensor id, or multiple ids joined either by "+" or by "|"
#   - taille : graph size : "petit" (small), "moyen" (medium), "grand" (large)
#   - a time interval:
#	- either a period ("daily", "weekly", "monthly", "yearly")
#	- either a start time (debut) and a end time (fin), both are time_t
#
# History
#   2006/08/09 : pda/boggia  : design
#   2006/08/10 : pda/boggia  : add uid parameter
#   2006/12/12 : jean/boggia : add parameters periode and taille
#   2007/01/12 : pda         : common initialization
#   2008/07/30 : pda         : generalize to wifi graphs
#   2008/07/31 : pda         : more than one graph on the same graph with "|"
#   2010/12/11 : pda         : i18n
#   2010/12/25 : pda         : use cgi-dispatch
#

#
# Script parameters
#

set conf(idrfmt)	{http://%1$s/bin/gengraph?%2$s&debut=%3$d&fin=%4$d}

#
# Netmagis general library
#

source /usr/local/lib/netmagis/libnetmagis.tcl

# ::webapp::cgidebug ; exit

##############################################################################
# Main procedure
##############################################################################

d cgi-register {} {
    {id		1 1}
    {debut	0 1}
    {fin	0 1}
    {periode	0 1}
    {taille	0 1}
} {
    global conf

    #
    # Check graph id according to user's rights
    #

    set msg [check-metro-id $dbfd $id tabuid titre]
    if {! $tabuid(p_admin) && $msg ne ""} then {
	d error $msg
    }

    #
    # Check time range
    # If 'periode' is given, we ignore 'debut' and 'fin' parameters.
    #

    if {$periode ne ""} then {
	set fin [clock second]
	switch -exact $periode {
	    daily {
		set debut [clock scan "yesterday"]
	    }
	    weekly {
		set debut [clock scan "last week"]
	    }
	    monthly {
		set debut [clock scan "last month"]
	    }
	    yearly {
		set debut [clock scan "last year"]
	    }
	    default {
		d error [mc "Invalid period '%s'" $periode]
	    }
	}
    } elseif {$debut eq "" && $fin eq ""} then {
	set fin   [clock second]
	set debut [clock scan "yesterday"]
    } else {
	if {[catch {clock format $debut}]} then {
	    d error [mc "Invalid date '%s'" $debut]
	}
	if {[catch {clock format $fin}]} then {
	    d error [mc "Invalid date '%s'" $fin]
	}
	if {$debut >= $fin} then {
	    set x $debut
	    set debut $fin
	    set fin $x
	}
    }

    #
    # Check "taille" parameter
    #

    switch -exact $taille {
	petit -
	moyen -
	grand {
	    set urltaille "&taille=$taille"
	}
	"" {
	    set urltaille ""
	}
	default {
	    d error [mc "Invalid graph size '%s'" $taille]
	}
    }

    #
    # Id management. Two possible cases:
    # - multiple ids, joined by "|": in this case, id= ... & id=... & ...
    # - single id, or multiple ids joined by "+": in this case, id=...
    #

    set lid [split $id "|"]
    if {[llength $lid] <= 1} then {
	set hid "id=[::webapp::post-string $id]"
    } else {
	set n 1
	set hid {}
	foreach i $lid {
	    lappend hid "id$n=[::webapp::post-string $i]"
	    incr n
	}
	set hid [join $hid "&"]
    }

    #
    # Output page, fetched from external URL
    #

    set host [get-local-conf "metrohost"]
    set url [format $conf(idrfmt) $host $hid $debut $fin]
    append url $urltaille
    gengraph $url

    #
    # Deconnect from database
    #

    d end
}

##############################################################################
# Main procedure
##############################################################################

d cgi-dispatch "topo" ""
