#/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

set read_count 0
set last_read_count 0
set semvar 1
set retry_count 0

proc Debug { s m } {
  global windows stuff

  if { $stuff(debug) == 0 } {
    return
  }

  set t [clock seconds]
  set date [clock format $t -format "%Y-%m-%d"]
  set utc [clock format $t -format "%H:%M"]
  set d "$date $utc"

  puts "$d $s $m"
}

proc Restart_Serial { } {
  Serial_Close
  Serial_Open
}


proc Read_SERIAL { } {
  global stuff
global read_count 
  
  if [eof $stuff(fid)] {
    Debug "Read_SERIAL" "End of file detected"
    Restart_Serial
  }

#  Debug "Read_SERIAL" "Reading"
#  if { [ catch { gets $stuff(fid) bug } ] } {
#    Debug "Read_SERIAL" "Read error"
#    Restart_Serial
#  } else {
#	incr read_count
#      puts  $stuff(spoolfid) $bug
#      puts  $bug
#
#    }

  if { [ catch { set bug [ read $stuff(fid) 1]  } ] } {
    Debug "Read_SERIAL" "Read error"
    Restart_Serial
  } else {
	incr read_count
		if { "" < $bug } {
      puts -nonewline  $stuff(spoolfid) $bug
      puts -nonewline stdout  $bug
		}

    }

}
proc Serial_Open { } {
  global stuff

  Debug "Serial_Open" "Opening Serial Port"
  if { [ catch { set stuff(fid) [ open $stuff(serport) ] } ] } {
    Debug "Serial_Open" "Cannot open $stuff(serport)."
  } else {
    fconfigure $stuff(fid) -mode $stuff(sermode)
    fconfigure $stuff(fid) -buffering line
    fconfigure $stuff(fid) -blocking 0
    fileevent $stuff(fid) readable { Read_SERIAL }
  }
}
proc Spool_file_Open { } {
  global stuff

  set t [clock seconds]
  set date [clock format $t -format "%Y-%m-%d"]
  set utc [clock format $t -format "%H%M%S"]
 set stuff(spool_name) "/tmp/$date$utc"

  Debug "Spool_file_Open" "Opening Spool File"
  if { [ catch { set stuff(spoolfid) [ open $stuff(spool_name) w ] } ] } {
    Debug "Spool_file_Open" "Cannot open $stuff(spool_name)"
  } 
}

proc Spool_file_to_printer { } {

global stuff
  Debug "Spool_file_to_printer" "Opening Spool File"
  if { [ catch { set spoolfid [ open $stuff(spool_name) r ] } ] } {
    Debug "Spool_file_to_printer" "Cannot open $stuff(spool_name)"
	}

  if { [ catch { set lprfid [ open $stuff(lpr_name) w ] } ] } {
    Debug "Spool_file_to_printer" "Cannot open $stuff(lpr_name)"
	}

while { 1 >  [eof $spoolfid] } {
  if { 1 > [ catch { gets $spoolfid bug } ] } {
      puts  $lprfid $bug
    }
  }

close $spoolfid
close $lprfid

}
proc Spool_file_Close { } {
  global stuff

 Debug "Spool_file_Close" "Closing $stuff(spool_name)"

  if { [ info exists stuff(spoolfid) ] } {
    close $stuff(spoolfid)
    unset stuff(spoolfid)
	Spool_file_to_printer
  }
  Spool_file_Open
}
proc Serial_Close { } {
  global stuff

  if { [ info exists stuff(fid) ] } {
    close $stuff(fid)
    unset stuff(fid)
  }
}
proc Init { } {
  global stuff tcl_platform

  set stuff(debug) 1

  switch -exact -- $tcl_platform(os) {
    "Linux" {
      set stuff(serport) "/dev/ttyS1"
    }
    "Darwin" {
      set stuff(serport) "/dev/ttyS1"
    }
    default {
      set stuff(serport) "COM4:"
		set stuff(spool_name) "./spoolfile"
		set stuff(lpr_name) "LPT1:"
    }
  }

  set stuff(sermode) "9600,n,8,1"
  set stuff(spoolfid) ""

}
proc check_data { } {
global read_count last_read_count semvar retry_count

puts "check_data"

if { 0 < $read_count && $read_count == $last_read_count } {
	incr retry_count
	if { 4 < $retry_count } {
		Spool_file_Close
		set read_count 0
		set retry_count 0
		}
	}
set last_read_count $read_count
set semvar 1
}


Init

if { [ file readable "serial.ini" ] } {
  source "serial.ini"
}

Spool_file_Open
set xyz 0
Serial_Open

set semvar 1
while { 1 < 2 } {
	after 1000 check_data
	set semvar 0
	vwait semvar
}


Serial_Close
