#!/usr/local/bin/tclsh


 ############################################################
 # PUSH.CGI  V1              WRITEN BY PIERRE-MIKAEL LEGRIS #
 #                                      IN TCL SCRIPT       #
 # Create animations using multipart/x-mixed-replace        #
 # The animation is based on the access time of files       #
 # This program is designed to be used with a camera that   #
 # Would be able to store pictures in a folder              #
 # The program parse this folder and give 3 choices         #
 # 1 - View a sequence a determinate from a date to another #
 # 2 - View images when they arrive in the folder           #
 # 3 - View a determinate picture                           #
 #                                                          #
 # push.cgi MUST be used with putpic.cgi                    #
 #                                                          #
 ############################################################

## Set the path where push.cgi can find the pictures
## Must end with "/"
set path "Images/"

## Set the type of picture to display : jpg , gif 
set type_of_pict "jpg"


######## Nothing Need to be changed below this line #########
set pict_path "$path*.$type_of_pict"

set date_list ""

### The procedure that parse picts by dates ###
proc tri_picts {} {
    global date_list picta pict_path
    set picts [glob $pict_path]
    set picta(0) "0"

    foreach pict $picts {
	set pict_date [file mtime $pict]
	set date_list "$date_list $pict_date"
	
	set same_date [array get picta "$pict_date"]
	if {$same_date != "" } {
	    set picta($pict_date) "$picta($pict_date) $pict"
	} else {
	    set picta($pict_date) "$pict"
	}
    }
    set date_list [lsort -integer $date_list]
}
### End of tri_picts ###

### Proc which create the default web page ###
proc page_default {} {
    global date_list picta
    puts "See an animation <br>"
    
    set previous_date 0
    puts "<form action=\"push.cgi\" method=post>"
    puts "Start: <select name=\"debut\">"
    foreach date_value $date_list {
	if {$date_value != $previous_date} {
	    set ofile [array get picta "$date_value"]
	    puts "<option>[clock format [lindex $ofile 0]]"
	}
	set previous_date $date_value
    }
    puts "</select>"
    
    set rev_date_list [lsort -integer -decreasing $date_list]
    
    set previous_date 0
    puts "End: <select name=\"fin\">"
    foreach date_value $rev_date_list {
	if {$date_value != $previous_date} {
	    set ofile [array get picta "$date_value"]
	    puts "<option>[clock format [lindex $ofile 0]]"
	}
	set previous_date $date_value
    }
    puts "</select><br>"
    
    puts "Frequency: <select name=\"sleep\">"
    for {set i 0} {$i < 11} {incr i} {
	puts "<option>$i"
    }
    puts "</select>seconds<input type=submit value=\"OK\"></form><br><hr><br>"

    
    puts "Live<br>"
    
    puts "<form action=\"push.cgi\" method=post>"
    puts "Checking frequency"
    puts "<select name=\"sleepm\"> minutes"
    for {set i 0} {$i < 10} {incr i} {
	puts "<option>$i"
    }
    puts "</select>minutes"

    puts "<select name=\"sleeps\">"
    for {set i 10} {$i < 60} {incr i 10} {
	puts "<option>$i"
    }
    puts "</select>seconds<br><input type=submit value=\"OK\"></form><br><hr><br>"

    puts "Voir une image precise"
    
    set previous_date 0
    foreach date_value $rev_date_list {
	if {$date_value != $previous_date} {
	    set ofile [array get picta "$date_value"]
	    foreach pfile [lindex $ofile 1] {
		puts "<br>[clock format [lindex $ofile 0]]"
		puts "<a href=\"$pfile\">$pfile</a>"
	    }
	}
	set previous_date $date_value
    }

}
### End page_default

### Parse the date sent by the POST method ###
proc convert_time {time} {
    set s1 [split $time "+"]
    set s3 [split [lindex $s1 3] {}]
    proc tcut { pos s3} {
	return "[lindex $s3 $pos][lindex $s3 [incr pos]]"
    }

    set h_m_s "[tcut 0 $s3]:[tcut 5 $s3]:[tcut 10 $s3]"
    return [clock scan "[lrange $s1 0 2] $h_m_s [lrange $s1 4 5]" ]
    
}
### End of convert_time

### Main part of the program ###


#### Web page header
puts "Content-type: text/html\n\n<html><head>"
puts "<title>PUSH.CGI</TITLE>"
puts "<META Name=\"Author\" Content=\"Perki\">"
puts "</head><body bgcolor=\"#FFFFFF\">"

# parse file by date
tri_picts

if {$env(REQUEST_METHOD) == "GET"} {
    puts "<center><H1>PUSH</H1></center><br>"
    page_default
} else {
    set query [read stdin $env(CONTENT_LENGTH)]
    set s_query [split $query "&="]

    if {[lindex [split $query "="] 0] == "debut"} {
	set d_deb [convert_time [lindex $s_query 1]]
	set d_fin [convert_time [lindex $s_query 3]]
	set ssleep [lindex $s_query end]
	if {$d_deb > $d_fin} {
	    puts "The starting date must be before the Ending date"
	} else {
	    puts "<center>SEQUENCE</center><BR>"
	    puts "From:[clock format $d_deb]<BR>"
	    puts "To:[clock format $d_fin]<BR>"
	    puts "<center>"
	    puts "<img src=\"putpic.cgi?$path+$type_of_pict+seq+$ssleep+$d_deb+$d_fin\">"
	    puts "</center>"
	}
    } else {
	set ssleep [expr {[lindex $s_query 1] * 60 + [lindex $s_query 3]}]
	puts "<center>LIVE</center><BR>"
	puts "Checking for images every $ssleep seconds"
	puts "<center>"
	puts "<img src=\"putpic.cgi?$path+$type_of_pict+live+$ssleep\">"   
	puts "</center>"
    }
    puts "</center><a href=\"push.cgi\">back</a>"
}

puts "</body></html>"







