#!/bin/sh

set kludge { ${1+"$@"}
shift
shift
exec ical -f $0 -nodisplay ${1+"$@"}
}
#
# This code was borrowed from ical_text.tcl by Sanjay Ghemawat 
#
proc WIZ_ical_no_tk_script {} {
    # Parse arguments (some argument parsing has already been
    # done by startup.tcl)
    global argv ical

    set showcount 1
    set print 0

    while {[llength $argv] != 0} {
	set arg [lindex $argv 0]
	set argv [lrange $argv 1 end]

	case $arg in {
	    "-print" {
		if {[llength $argv] < 1} ical_usage
		set spec [lindex $argv 0]
		set argv [lrange $argv 1 end]

		# Check on format of show spec
		set showcount $spec
		set print 1
	    }
	    "-show" {
		if {[llength $argv] < 1} ical_usage
		set spec [lindex $argv 0]
		set argv [lrange $argv 1 end]

		# Check on format of show spec
		if ![regexp {^\+([0-9]+)$} $spec junk days] ical_usage
		set showcount $days
		set print 0
	    }
	    "-list" {
		set showcount 1
		set print 0
	    }
	    default {ical_usage}
	}
    }

    # Get calendar
	calendar cal $ical(calendar)

	if $print {
	    # Generate postscript
	    puts stdout [pr_output $ical(startdate) $showcount]
	} else {
		# Generate listing
	    set lastdate ""
	    set sep ""

	    puts -nonewline stdout  "\"i\",\"SCRT\",\"TIM1\",\"TIM2\",\"ALRM\",\"ALON\",\"MEM1\",\"DMRK\",\"ZINK\",\"QMEM\""
	    puts -nonewline stdout "\r\n"
	    puts -nonewline stdout  "\"T\",\"å\",\"ϻ\",\"λ\",\"顼\",\"顼\",\"塼\",\"ޡ\",\"󥯥ǡ\""
	    puts -nonewline stdout "\r\n"

	    cal listing $ical(startdate) [expr $ical(startdate)+$showcount-1] i d {
		set split [date split $d]
		set day [format "%d%02d%02d"\
			[lindex $split 3]\
			[lindex $split 2]\
			[lindex $split 0]];
		if [catch {set start [format "%02d%02d"\
			[expr [$i starttime]/60]\
			[expr [$i starttime]%60]]}] {
		    set start "    "
		}
		if [catch {set end  [format "%02d%02d"\
			[expr ([$i starttime]+[$i length])/60]\
			[expr ([$i starttime]+[$i length])%60]]}] {
		    set end ""
		}

		set time1  [format "%s%s  " $day $start]
		if [ string compare $start "    " ] {
		    set time2 [format "%s%s  " $day $end]
		} else {
		    set  time2 ""
		}
		#	    puts stdout $time1
		#	    puts stdout $time2
		WIZ_out "D"    ; WIZ_sep
		WIZ_out " "    ; WIZ_sep
		WIZ_out $time1 ; WIZ_sep
		WIZ_out $time2 ; WIZ_sep
		#		    puts -nonewline stdout "$sep[WIZ_date2text $d]"
		set lastdate $d
		set sep "\n"
		if [catch {set Alarm [$i alarms]}] {
		    set Alarm {}
		}
		set str [$i text]

		regsub -all "\[ \t\n\]+\$" $str "" str

		regsub -all "\n" $str "\037" str

		#		puts -nonewline stdout [item2text $i]
		#	  puts stdout $str
		set alrm ""
		if [string compare $Alarm {}] {
		    set salarm [lsort -integer $Alarm]
		    
		    if { [lindex $salarm 0] > 0 } {
		       set alrm [expr [$i starttime] - [lindex $salarm 0]]
		    } elseif { [lindex $salarm 1] > 0 } {
		       set alrm [expr [$i starttime] - [lindex $salarm 1]]
                    } elseif { [lindex $salarm 2] > 0 } {
		       set alrm [expr [$i starttime] - [lindex $salarm 2]]
                    } elseif { [lindex $salarm 3] > 0 } {
		       set alrm [expr [$i starttime] - [lindex $salarm 3]]
		    } else {
		       puts stderr "Invalid Alarm Data"
		    }

       	            if [catch {set alrm [format "%s%02d%02d"\
		       $day\
		       [expr $alrm/60]\
		       [expr $alrm%60]]}] {
		         set atime ""
                     }

#		     puts -nonewline stdout "Alarm: "
		     #		    puts stdout $Alarm
		 }
		 WIZ_out $alrm  ; WIZ_sep
		 WIZ_out " "    ; WIZ_sep
		 WIZ_out $str   ; WIZ_sep
		 WIZ_out " "    ; WIZ_sep
		 WIZ_sep
		 puts -nonewline stdout  "\r\n"

	    }
	    puts -nonewline stdout "\032"
	    cal delete
	}
	exit 0
    }
    
proc WIZ_date2text {date} {
    global weekday_name month_name

    set split [date split $date]
    return [format "%d%02d%02d"\
            [lindex $split 3]\
	    [lindex $split 2]\
	    [lindex $split 0]]
}

proc WIZ_out {text} {
    if [ string compare $text "" ] {
        puts -nonewline stdout [format "\"%s\"" $text]
    } else {
        puts -nonewline stdout ""
    }
}

proc WIZ_sep {} {
    puts -nonewline stdout ","
}

WIZ_ical_no_tk_script

