# Copyright: 	(c) 1997,1998,1999 ICEM Systems.
# Author:    	Jari Spaete ICEM Systems Hannover jsp@icem.de
# 011299

proc stopMonitor { reason } {
global arrayMonitor

  set arrayMonitor(STOP) 1
  
  if { $reason == 0 } {
    if { ![ file exists $arrayMonitor(STOP_FILE) ] } {
      set f [ open $arrayMonitor(STOP_FILE) w ]
      close $f
    }
  } 

  set w .
  if { $reason == 0 } {
    catch { wm title $w "$arrayMonitor(WHAT) Monitor - Stopped !" }
  } elseif { $reason == 1 } {
    catch { wm title $w "$arrayMonitor(WHAT) - Finished !" }
  } 
  bind $w <Visibility> "raise $w"
  
  .monitor.buttons.1 configure -bg bisque -activebackground bisque -bd 0 \
                       -relief flat -text "Please\nwait" 

  .monitor.text delete 1.0 end
  if { $reason == 0 } {
    .monitor.text configure -fg red
    .monitor.text insert end "$arrayMonitor(WHAT) stopped by user."
  } elseif { $reason == 1 } {
    .monitor.text insert end "$arrayMonitor(WHAT) completed."
  }
      
  update
  
  set loop 0
  while { 1 } {
    if { $reason == 0 } {
      if { ![ info exists stopDelay ] } {
        if [ file exists $arrayMonitor(STOP_FILE) ] {
          set stopDelay 1
          incr loop
        } else {
          break
        }
      } else {
        after 1000
        unset stopDelay
        if { $loop == 6 } {
          break
        }
      }
    } elseif { $reason == 1 } {
      if { $loop == 3 } {
        break
      } else {
        after 1000
        incr loop
      }
    } 
  } 
  exitMonitor $w
}

proc exitMonitor { w } {
global arrayMonitor

  foreach name [ array names arrayMonitor ] {
    unset arrayMonitor($name)
  }
  unset arrayMonitor  

  catch { destroy $w }
  exit
}

proc mainMonitor { pidExtension what where } {
global arrayMonitor
  
  set screenWidth [ winfo screenwidth . ] 
  if { $screenWidth <= 1600 } {
    set winWidth 472
    set winHeight 87
    set buttonSize 60
    set barsSize 30
    set textSize 36
    set lineWidth 300
    set textFont -*-helvetica-bold-r-normal--12*
  } else {
    set winWidth 492
    set winHeight 107
    set buttonSize 80
    set barsSize 50
    set textSize 48
    set lineWidth 400
    set textFont -*-helvetica-bold-r-normal--14*
  }
  
  set arrayMonitor(OS) [ winfo server . ]
  set arrayMonitor(STOP) 0
  set arrayMonitor(START) 0
  set arrayMonitor(SLEEP_TIME) 1
  set arrayMonitor(WHAT) $what
  set arrayMonitor(MONITOR_FILE) "$where/.monitorSmoother_$pidExtension"
  set arrayMonitor(STOP_FILE) "$where/.stopSmoother_$pidExtension"
  set arrayMonitor(LINE_SIZE) [ expr $lineWidth / 100 ]
  set arrayMonitor(BARS_SIZE) $barsSize
  set arrayMonitor(LAST_OPEN) 0
  set arrayMonitor(LAST_MODI) 0
  set arrayMonitor(POS_LAST) 0
  set arrayMonitor(OLD_LINE) ""
  set arrayMonitor(TEXT) ""
  set arrayMonitor(STEPS) ""

  frame  .monitor
 
  frame  .monitor.buttons -width $buttonSize -height $buttonSize -bd 1 -relief raised \
  			  -bg bisque  
  button .monitor.buttons.1 -text "Stop" -bg red -activebackground red -relief raised \
                     	  -bd 5 -command "stopMonitor 0"
  place  .monitor.buttons.1 -relx 0.1 -rely 0.1 -relwidth 0.8 -relheight 0.8

  frame  .monitor.labels    -width $lineWidth -height 30 -bd 3 -relief raised -bg bisque  
  button .monitor.labels.1  -bg bisque -activebackground bisque -activeforeground darkgreen \
                    	    -fg darkgreen -relief flat -bd 0 -text "$arrayMonitor(WHAT) Progress"
  place  .monitor.labels.1  -relwidth 1 -relheight 1

  frame  .monitor.bars    -width $lineWidth -height $barsSize -bd 0 -relief flat -bg bisque 
  canvas .monitor.bars.1  -bg darkgreen -relief raised -bd 2
###  button .monitor.bars.1  -bg darkgreen -activebackground darkgreen -fg black -relief sunken -bd 2
  place  .monitor.bars.1  -height [ expr $barsSize - 5 ] -width 0
  
  frame  .monitor.bars.2  -bg black -bd 0 -relief flat
  place  .monitor.bars.2  -height 5 -width $lineWidth -rely 0.9 
  for { set i 0 } { $i < 11 } { incr i } {
    button .monitor.bars.2.$i -bg white -fg white -bd 0 -relief flat
    place  .monitor.bars.2.$i -height 5 -width 1 -rely 0.1 -relx 0.$i
  }
    
  text   .monitor.text -bd 3 -relief raised -width $textSize -height 1 -bg bisque \
  		       -font $textFont
  .monitor.text insert end "Please wait ..."

  pack   .monitor.text    -side top -anchor w -expand 1 -fill both
  pack   .monitor.labels  -side top -anchor w -ipadx 2
  pack   .monitor.bars    -side top -anchor w -ipadx 2
  pack   .monitor.buttons -side right -after .monitor.text -ipadx 3
  pack   .monitor
 
  set w .
  catch { wm title $w "$arrayMonitor(WHAT) Monitor" }
  catch { wm protocol $w WM_DELETE_WINDOW "exitMonitor $w" }  
  catch { wm resizable $w 0 0 }
  catch { wm geometry $w +100+100 }
  
  update
    
  while { $arrayMonitor(STOP) == 0 } {
    sleepATime $arrayMonitor(SLEEP_TIME)
    progressMonitor    
  }
}

proc progressMonitor {} {
global arrayMonitor

  set arrayMonitor(SLEEP_TIME) 1
  
  if [ file exists $arrayMonitor(MONITOR_FILE) ] {
    set arrayMonitor(LAST_MODI) [file mtime $arrayMonitor(MONITOR_FILE)]
    if { $arrayMonitor(LAST_MODI) <= $arrayMonitor(LAST_OPEN) } {
      update
      return
    }
    set f [open $arrayMonitor(MONITOR_FILE) r]
    set  arrayMonitor(LAST_OPEN) $arrayMonitor(LAST_MODI)
    seek $f $arrayMonitor(POS_LAST) start
    while { ![eof $f] } {
      set line [gets $f]
      set posFile [tell $f]
      if { $posFile > $arrayMonitor(POS_LAST) } {
        if [string match "@@@*" $line] {
          if [string match "@@@ 1: *" $line] {		;# Information Message
            .monitor.text configure -fg black
            regsub "@@@ 1: " "$line" "" line
            .monitor.text delete 1.0 end
            .monitor.text insert 1.$posFile "$line $arrayMonitor(STEPS)"
            set arrayMonitor(TEXT) $line
            if [info exists arrayMonitor(TARGET)] {
              setLabelButton $arrayMonitor(TARGET)
              unset arrayMonitor(TARGET)
            }
            set arrayMonitor(POS_LAST) $posFile
            if { $arrayMonitor(START) == 0 } {
              catch { wm title . "$arrayMonitor(WHAT) Monitor - Running !" }
              set arrayMonitor(START) 1
            }
            update
          } elseif [string match "@@@ 2: *" $line] {	;# Warning Message
	    .monitor.text configure -fg red
	    regsub "@@@ 2: " "$line" "" line
	    .monitor.text delete 1.0 end
            .monitor.text insert 1.$posFile $line
            set arrayMonitor(POS_LAST) $posFile
            update
          } elseif [string match "@@@ 11: *" $line] {	;# Smoothing Progress
            regsub "@@@ 11: " "$line" "" line
            set newLine [ format %.4s $line ]
            set diffLine [ format %.4s $arrayMonitor(OLD_LINE) ]
            if [ string match $newLine $diffLine ] {
              set arrayMonitor(SLEEP_TIME) 0
            } else {
              set arrayMonitor(SLEEP_TIME) 0
              setBarButton $newLine
              update
            }
            set arrayMonitor(OLD_LINE) $line
            set arrayMonitor(POS_LAST) $posFile
          } elseif [string match "@@@ 12: *" $line] {	;# Target determinant 
            regsub "@@@ 12: " "$line" "" line
            regsub "0000" "$line" "" line
            setLabelButton "Target determinant $line"
            set arrayMonitor(TARGET) "Target determinant $line"
            set arrayMonitor(POS_LAST) $posFile
            update
          } elseif [string match "@@@ 13: *" $line] {	;# Target orthogonality
            regsub "@@@ 13: " "$line" "" line
            regsub "0000" "$line" "" line
            setLabelButton "Target orthogonality $line"
            set arrayMonitor(TARGET) "Target orthogonality $line"
            set arrayMonitor(POS_LAST) $posFile
            update
          } elseif [string match "@@@ 20: *" $line] {	;# Smoothing steps
            regsub "@@@ 20: " "$line" "" line
            set arrayMonitor(STEPS) " (Step: $line)"
            .monitor.text delete 1.0 end
            .monitor.text insert 1.$posFile "$arrayMonitor(TEXT) $arrayMonitor(STEPS)"
            set arrayMonitor(POS_LAST) $posFile
            update
          } elseif [string match "@@@ 99:" $line] {	;# Smoothing completed !!!
            close $f
            update
            stopMonitor 1
          } 
        }
      }
    }
    close $f
  } else {
    update
  }
}

proc setBarButton { line } {
global arrayMonitor
 
  set barWidth [ expr $line * $arrayMonitor(LINE_SIZE) * 100 ]
  set textPercent [ expr $barWidth / $arrayMonitor(LINE_SIZE) ]
  set textPercent [ format %3.5s $textPercent ]  

  .monitor.bars.1 create rectangle 0 0 $barWidth [ expr $arrayMonitor(BARS_SIZE) - 5 ] \
  			-fill darkgreen -width 10 -outline darkgreen
  place .monitor.bars.1 -width $barWidth
  setLabelButton "$textPercent %"
}

proc setLabelButton { line } {
global arrayMonitor

  set messText "$arrayMonitor(WHAT) Progress : $line"
  .monitor.labels.1 configure -text $messText
}

proc sleepATime { sleepTime } {
global arrayMonitor env  
    
  if { ![ string match Windows* $arrayMonitor(OS) ] } {
    catch { exec /bin/sleep $sleepTime }
  } else {
    catch { exec $env(ICEM_ACN)/toolswin32/sleep.exe $sleepTime }
  } 
}

proc getRunTime {} {

  set exeProg "date"
  set dummy [exec $exeProg]
  puts $dummy
}

mainMonitor [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]

