#!/usr/local/bin/wish4.0 -f
#
#  'conf-reports' -- CBB General Reporting Front End
#
#  Written by Curtis Olson.  Started November 17, 1994.
#
#  Copyright (C) 1994, 1995, 1996  Curtis L. Olson  - curt@sledge.mn.org
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: conf-reports,v 1.4 1996/02/11 03:40:54 curt Exp $
# (Log is kept at end of this file)


#------------------------------------------------------------------------------
# Set defaults 
#------------------------------------------------------------------------------

set lib_path [pwd]
set cur_file ""
set tmp_file ""
set rep_start ""
set rep_end ""
set rep_dest "screen"
set rep_temp ""
set rep_type 1

if { [file exists "~/.cbbrc.tcl"] } {
    source "~/.cbbrc.tcl"
}


source "$lib_path/bindings.tk"
source "$lib_path/common.tk"


#------------------------------------------------------------------------------
# Process arguments
#------------------------------------------------------------------------------

set i 0
while { $i < $argc } {
    set arg [lindex $argv $i]

    if { "$arg" == "-a" } {
        set i [expr $i + 1]
	set cur_file [lindex $argv $i]
	puts "Current File is $cur_file"
    }

    if { "$arg" == "-graph" } {
	set rep_type 2
    }

    if { "$arg" == "-report" } {
        set rep_type 1
    }

    if { "$arg" == "-t" } {
        set i [expr $i + 1]
	set tmp_file [lindex $argv $i]
	puts "Temp File is $tmp_file"
    }

    set i [expr $i + 1]
}


#------------------------------------------------------------------------------
# Select report starting & ending dates
#------------------------------------------------------------------------------

proc rep_configure { rep_type } {
    global lib_path cur_file tmp_file rep_name rep_start rep_end rep_dest 
    global rep_file max_files files names
    global dialog_font button_font

    option add *font $dialog_font

    if { $rep_type == 1 } {
	set name "Report"
    } else {
	set name "Graph"
    }

    if { "$rep_dest" != "screen" } {
        if { "$rep_dest" == "file" } {
	    set rep_dest "screen"
        }
    } 

    wm title . "$name Configuration ..."
    wm iconname . "$name Configuration ..."
    frame .f -borderwidth 2 -relief raised
    pack .f -side top -fill both -expand 1

    frame .f.l 
    frame .f.r 
    pack .f.l .f.r -side left -fill both -expand 1

    frame .f.l.rep -borderwidth 2 -relief groove
    frame .f.l.file -borderwidth 2 -relief groove
    pack .f.l.rep .f.l.file -side top -fill both -expand 1 -padx 4 -pady 4

    label .f.l.rep.label -text "Select a $name"
    pack .f.l.rep.label -side top -fill x -expand 1

    set i 0
    if { $rep_type == 1 } {
        set filehandle [open "$lib_path/reports/reports.conf" r]
    } else {
        set filehandle [open "$lib_path/graphs/graphs.conf" r]
    }
	
    while { [gets $filehandle line] >= 0 } {
        if { "[string range $line 0 0]" == "-" } {
	    # ignore this line
        } elseif { "[string range $line 0 0]" == "#" } {
            # ignore this line
        } elseif { [string length $line] < 3  } {
            # ignore this line
        } else {
            set pieces [split $line "\t"]
            set bname [lindex $pieces 0]
            set prog [lindex $pieces 1]
            radiobutton .f.l.rep.rep$i -text $bname -relief flat \
		-variable rep_name -value "$prog"
	    pack .f.l.rep.rep$i -side top -anchor w 

	    if { "$rep_name" == "" } {
		set rep_name "$prog"
	    }
	    set i [expr $i + 1]
        }
    }
    close $filehandle

    label .f.l.file.label -text "Select One or More Files"
    pack .f.l.file.label -side top -fill x -expand 1

    set i 0
    # cd [file dirname $cur_file]
    foreach file [lsort [glob -nocomplain [file dirname $cur_file]/*.cbb]] {
	set max_files $i
	checkbutton .f.l.file.file$i -text [file tail $file] -relief flat \
		-variable files($i) 

        set files($i) 0
	set names($i) $file
	if { "$file" == "$cur_file" } {
	    set files($i) 1
	    if { "$tmp_file" != "" } {
	        set names($i) $tmp_file
	    } 
	}

        pack .f.l.file.file$i -side top -anchor w

	set i [expr $i + 1]
    }

    frame .f.r.conf -borderwidth 2 -relief groove
    frame .f.r.but -borderwidth 2 -relief groove
    pack .f.r.conf -side top -fill x -expand 1 -padx 4 -pady 4 -anchor n
    pack .f.r.but -side top -fill both -expand 1 -padx 4 -pady 4

    label .f.r.conf.label2 -text "Enter Starting and Ending dates"
    pack .f.r.conf.label2 -side top -fill x -expand 1

    frame .f.r.conf.s -borderwidth 2
    frame .f.r.conf.e -borderwidth 2
    pack .f.r.conf.s .f.r.conf.e -side top -fill x -expand 1

    if { $rep_type == 1 } {
        frame .f.r.conf.os -borderwidth 2
        frame .f.r.conf.of -borderwidth 2
        frame .f.r.conf.oe -borderwidth 2

        pack .f.r.conf.os .f.r.conf.of .f.r.conf.oe \
	        -side top -fill x -expand 1
    }

    label .f.r.conf.s.label -text "Starting Date"
    entry .f.r.conf.s.entry -textvariable rep_start -relief sunken
    pack .f.r.conf.s.label -side left
    pack .f.r.conf.s.entry -side left -fill x -expand 1

    label .f.r.conf.e.label -text "Ending Date"
    entry .f.r.conf.e.entry -textvariable rep_end -relief sunken
    pack .f.r.conf.e.label -side left
    pack .f.r.conf.e.entry -side left -fill x -expand 1


    if { $rep_type == 1 } {
        radiobutton .f.r.conf.os.screen -text "Send to Screen" -anchor w \
	        -font $button_font -variable rep_dest -value "screen" \
	        -relief flat -command { \
		    if { "$rep_file" != "" } { \
		        set rep_temp $rep_file; \
		        set rep_file ""; \
		        update \
		    } \
	        }
        radiobutton .f.r.conf.of.file -text "Save to File or Pipe" -anchor w \
	        -font $button_font -variable rep_dest -value "file" \
	        -relief flat -command {
		    if { "$rep_file" == "" } { \
		        if { "$rep_temp" != "" } { \
			    set rep_file $rep_temp; \
			    update \
		        } \
		    } \
	        } 
        label .f.r.conf.oe.label -text "File Name"
        entry .f.r.conf.oe.file -textvariable rep_file -relief sunken

        pack .f.r.conf.os.screen -side left -fill x -expand 1
        pack .f.r.conf.of.file -side left -fill x -expand 1
        pack .f.r.conf.oe.label -side left
        pack .f.r.conf.oe.file -side left -fill x -expand 1

    }

    button .f.r.but.gen -text "Generate $name ..." -font $button_font \
	    -command { gen_report $rep_type }
    button .f.r.but.dismiss -text "Dismiss" -font $button_font \
    	    -command { 
                if { "$tmp_file" != "" } { exec rm $tmp_file }
		destroy . 
	    }
    pack .f.r.but.gen .f.r.but.dismiss -side left \
	    -fill x -expand 1 -padx 8 -pady 8

    focus .f.r.conf.s.entry
}


#------------------------------------------------------------------------------
# Call the current report with the current start/end dates
#------------------------------------------------------------------------------

proc gen_report { rep_type } {
    global rep_handle rep_start rep_end rep_dest rep_file max_files
    global files names tmp_file nice_start nice_end lib_path rep_name

    # build file list
    set i 0
    while { $i <= $max_files } {
	if { $files($i) } {
	    append file_list "$names($i) "
	}
	set i [expr $i + 1]
    }

    puts $file_list

    if { "$rep_start" != "" } {
	set nice_start "-from $rep_start"
    } else {
	set nice_start ""
    }

    if { "$rep_end" != "" } {
	set nice_end "-to $rep_end"
    } else {
	set nice_end ""
    }

    puts "From $nice_start To $nice_end"

    if { $rep_type == 1 } {
        puts "open |$lib_path/reports/$rep_name $nice_start $nice_end $file_list r+"
        set rep_handle [open "|$lib_path/reports/$rep_name $nice_start \
			      $nice_end $file_list" r]
    } else {
        puts "open |$lib_path/graphs/$rep_name $nice_start $nice_end $file_list r+"
        set rep_handle [open "|$lib_path/graphs/$rep_name $nice_start \
			      $nice_end $file_list" r+]
	set rep_dest "graph"
    }

    if { "$rep_dest" == "screen" } {
	display_report

        close $rep_handle
    } elseif { "$rep_dest" == "file" } {
	set rep_out [open "$rep_file" w]
        while { [gets $rep_handle line] >= 0 } {
	    puts $rep_out $line
        }
        close $rep_out
	ok_mesg "Report written to ``$rep_file''"
	tkwait window .ok

        close $rep_handle
    } elseif { "$rep_dest" == "graph" } {
	display_report
	tkwait window .repwin

	puts "Closing graph"
	puts $rep_handle ""

        close $rep_handle
    }
}


proc display_report {} {
    global rep_handle rep_name rep_start rep_end nice_start nice_end
    global dialog_font fixed_font button_font tk_version

    option add *font $dialog_font
    
    toplevel .repwin

    wm title .repwin "$rep_name: $rep_start - $rep_end"
    wm iconname .repwin "Fix Me"
    frame .repwin.frame -borderwidth 2 -relief raised
    frame .repwin.frame.l -borderwidth 2
    frame .repwin.frame.d -borderwidth 2
    pack .repwin.frame .repwin.frame.l .repwin.frame.d -side top \
	-fill both -expand 1

    if { $tk_version < 4.0 } {
        listbox .repwin.frame.l.list -geometry 80x25 -relief sunken \
	     -font $fixed_font -yscrollcommand ".repwin.frame.l.scroll set"
    } else {
        listbox .repwin.frame.l.list -width 80 -height 25 -relief sunken \
	     -font $fixed_font -yscrollcommand ".repwin.frame.l.scroll set"
    }
    pack .repwin.frame.l.list -side left -fill both -expand 1

    scrollbar .repwin.frame.l.scroll -command ".repwin.frame.l.list yview" \
	-relief flat
    pack .repwin.frame.l.scroll -side left -fill y -expand 1

    button .repwin.frame.d.dismiss -text "Dismiss" -font $button_font \
	-command { destroy .repwin }
    pack .repwin.frame.d.dismiss -side top -fill x -expand 1 -padx 8 -pady 4

    while { [gets $rep_handle line] >= 0 } {
	if { "$line" == "done-graph" } {
	    break
	}
	.repwin.frame.l.list insert end $line
    }
}


rep_configure $rep_type


# ----------------------------------------------------------------------------
# $Log: conf-reports,v $
# Revision 1.4  1996/02/11  03:40:54  curt
# Better tk4.0 support
#
# Revision 1.3  1996/01/21  02:19:22  curt
# Updated copyright date
#
# Revision 1.2  1995/08/03  22:57:15  curt
# Added support for tk4.0
#
# Revision 1.1  1995/07/20  02:45:21  curt
# Added Files: extern.conf conf-reports bindings.tk common.tk
#
# Revision 1.10  1995/07/14  11:48:47  curt
# Fixed a small typo
#
# Revision 1.9  1995/07/09  03:22:58  curt
# Updated some of the visuals ...
#
# Revision 1.8  1995/07/05  01:14:55  curt
# Worked on handling reports & graphs.
#
# Revision 1.7  1995/07/04  23:07:37  curt
# Made changes to go along with the new report organization
#
# Revision 1.6  1995/06/07  02:46:11  curt
# Modified Files:  Todo categories.tk cbb file.tk reports.tk
#   Redo font handling.  Default fonts can now be overridden in ~/.cbbrc file
#
# Revision 1.5  1995/04/24  02:56:05  curt
# Changed "proc foo 0 { bar }" to "proc foo {} { bar }"
#
# Revision 1.4  1995/01/23  03:23:18  curt
# Preparing for more flexible report printing.
#
# Revision 1.3  1994/12/20  02:44:20  curt
# Added a "by category summary report"
#
# Revision 1.2  1994/11/29  16:43:32  clolson
# Changed date entry font.
#
# Revision 1.1  1994/11/28  18:50:17  clolson
# Began integration of reporting functions.
#
