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

#
# Display graph and form to update dates
#
# Called by: eq
#
# Parameters (form or url):
#   - id : selected sensor id, or multiple ids joined either by "+" or by "|"
#   - hdebut : start time
#   - jdebut : start date
#   - hfin : end time
#   - jfin : end date
#
# History
#   2006/05/24 : pda/jean/boggia : design
#   2006/08/10 : pda/boggia      : add ttl parameter
#   2007/01/12 : pda             : common initialization
#   2008/07/30 : pda             : generalize to wifi graphs
#   2008/07/30 : pda             : more flexible date/hour analysis
#   2010/12/13 : pda             : i18n
#   2010/12/25 : pda             : use cgi-dispatch
#

#
# Template pages used by this script
#

set conf(page)		metro.html

#
# Next actions
# 

set conf(nextmetro)	"metro"

#
# Script parameters
#

set conf(urlgraph)	"graph"

#
# Netmagis general library
#

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

# ::webapp::cgidebug ; exit

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

d cgi-register {} {
    {id		1 1}
    {jdebut	0 1}
    {hdebut	0 1}
    {jfin	0 1}
    {hfin	0 1}
} {
    global conf

    #
    # Initialization
    #

    set msgsta [topo-status $dbfd $tabuid(p_admin)]

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

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

    #
    # Check start time and date
    #

    set today [clock format [clock seconds] -format "%d/%m/%Y"]

    set start ""
    switch -regexp -- "$jdebut-$hdebut" {
	{^[0-9/]+-[0-9:]+$} {
	    # exact specification
	    set start [decode-date $jdebut $hdebut]
	}
	{^-[0-9:]+$} {
	    # today at x o'clock
	    set start [decode-date $today $hdebut]
	}
	{^[0-9/]+-$} {
	    # day x at 00:00
	    set start [decode-date $jdebut "00:00:00"]
	}
	{^-$} {
	    # 36 hours ago
	    set start "-1 day -12 hours"
	}
    }
    if {$start eq ""} then {
	d error [mc {Invalid date or time '%1$s/%2$s'} $jdebut $hdebut]
    }
    set start [clock scan $start]

    #
    # Check end time and date
    #

    set end ""
    switch -regexp -- "$jfin-$hfin" {
	{^[0-9/]+-[0-9:]+$} {
	    # exact specification
	    set end [decode-date $jfin $hfin]
	}
	{^-[0-9:]+$} {
	    # today at x o'clock
	    set end [decode-date $today $hfin]
	}
	{^[0-9/]+-$} {
	    # day x at midnight
	    set end [decode-date $jfin "23:59:59"]
	}
	{^-$} {
	    # just now
	    set end "now"
	}
    }
    if {$end eq ""} then {
	d error [mc {Invalid date or time '%1$s/%2$s'} $jfin $hfin]
    }
    set end [clock scan $end]

    #
    # Display id
    #

    set hid [::webapp::html-string $id]

    #
    # Prepare URL
    #

    d urlset "%URLMETRO%" $conf(nextmetro) {}
    d urlset "%URLGRAPH%" $conf(urlgraph) [list \
						[list "id" $id] \
						[list "debut" $start] \
						[list "fin" $end] \
					    ]

    #
    # End of script: output page and close database
    #

    d result $conf(page) [list \
			    [list %ADMIN%  "admin (future use)"] \
			    [list %MSGSTA% $msgsta] \
			    [list %TITLE%  $titre] \
			    [list %ID%     $hid] \
			    [list %JDEBUT% $jdebut] \
			    [list %HDEBUT% $hdebut] \
			    [list %JFIN%   $jfin] \
			    [list %HFIN%   $hfin] \
			]
}

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

d cgi-dispatch "topo" ""
