proc search_files {args} {
  global gracer_search_path

  set res {}
  foreach path $gracer_search_path {
    foreach pattern $args {
      catch {set res [concat $res [exec find $path -name $pattern]]}
    }
  }
  return $res
}

proc get_title {filename} {
  set file [open $filename "r"]
  while {![eof $file]} {
    set line [gets $file]
    set token [lindex $line 0]
    if [string match $token "TITLE"] {
      close $file
      return [lindex $line 1]
    }
  }
  close $file
  error "title does not specified."
}

proc get_model {filename} {
  set file [open $filename "r"]
  while {![eof $file]} {
    set line [gets $file]
    set token [lindex $line 0]
    if [string match $token "MODEL"] {
      close $file
      return [lindex $line 1]
    }
  }
  close $file
  error "model does not specified."
}

# get latest time stamp.

proc get_time_stamp {args} {
  set stamp 0
  foreach arg $args {
    if [file exists $arg] {
      set file [open $arg "r"]
      while {![eof $file]} {
	set line [gets $file]
	set token [lindex $line 0]
	if [string match $token "TIMESTAMP"] {
	  set tmp [clock scan "[lindex $line 1]"]
	  if {$tmp > $stamp} {
	    set stamp $tmp
	  }
	  break
	}
      }
      close $file
    } elseif {![catch {set tmp [clock scan $arg]}]} {
      if {$tmp > $stamp} {
	set stamp $tmp
      }
    } else {
      error "can not open file or scan arg as time stamp."
    }
  }
  return $stamp
}

proc gr_get_info {arrayName filename args} {
  upvar $arrayName res 

  array set res {}

  set file [open $filename "r"]
  while {![eof $file]} {
    set line [gets $file]
    set token [lindex $line 0]
    if {[lsearch -exact $args $token] >= 0} {
      set res($token) [lrange $line 1 end]
    }
  }
}
