#!/usr/bin/tclsh
#----------------------------------------------------------
# A script for testing if the transfer of data files 
# from instrument computers to AFS works
#
# Mark Koennecke, December 2015
#----------------------------------------------------------

set instlist [list amor boa dmc eiger focus hrpt mars \
    morpheus narziss orion poldi rita2 sans sans2 tasp zebra]


proc execCommand {command} {
     puts stdout "Doing $command"
     set status [catch {eval exec $command} msg]
     if {$status != 0} {
     	puts stdout "ERROR: $msg "
     }
}
#-----------------------------------------------------------
proc testTransfer {inst} {
    set year [clock format [clock seconds] -format "%Y"] 
    set tmpfile /tmp/gaga.dat
    execCommand "ssh ${inst}@${inst} cat data/${year}/DataNumber > $tmpfile"
    set f [open $tmpfile r]
    set num [string trim [gets $f]]
    set hun [expr $num /1000]
    set filename [format "/afs/psi.ch/project/sinqdata/%s/%s/%03d/%s%4dn%06d.*" \
		      $year $inst $hun $inst $year $num]
    set status [catch {glob $filename} l]
    if {$status == 0 && [llength $l] >= 1} {
	puts stdout "$inst is good"
    } else {
	puts stdout "File $filename not fond, problem at $inst"
    }
}

if {[llength $argv] < 1} {
   puts stdout "usage:\n\ttesttransfer instname| all"
   exit 1
}

set inst [lindex $argv]

if {[string compare $inst all] == 0} {
   foreach inst $instlist {
   	   testTransfer $inst
   }
} else {
  testTransfer $inst
}

puts stdout "Done"

