#!/bin/sh
# Start Tcl from a compbio bin directory \
exec /cluster/bin/tcl/`uname -m`/bin/tclsh8.4 $0 ${1+"$@"}
package require Tclx

#
# Program to build the cvs-reports directory.
#

proc mkLock {} {
    set lockFile $::CVS_REPORTS_DIR/build.lock
    if {[file exists $lockFile]} {
        set locker [read_file $lockFile]
        puts stderr "Error: lock file exists: $lockFile"
        puts stderr "       $locker"
        exit 1
    }
    set lockTime [clock format [clock seconds] -format "%F %T"]
    set locker "[id user] [id host] $lockTime"
    write_file $lockFile $locker
    return $lockFile
}

# convert date/time string to sec
proc cnvDateTime {dateStr} {
    # Tcl clock wants dates in form 2003-01-30
    regsub -all / $dateStr - dateStr
    return [clock scan $dateStr]
}

proc buildReports {fromTag toTag fromTagDate toTagDate branchVersion} {
    set pid [fork]
    if {$pid == 0} {
        puts stderr [list $::CVS_REPORTS -verbose -no-update -from $fromTag -to $toTag -fromDate $fromTagDate -toDate $toTagDate -branchVersion $branchVersion $::CVS_WORK_DIR $::CVS_REPORTS_DIR]
        execl $::CVS_REPORTS [list -verbose -no-update -from $fromTag -to $toTag -fromDate $fromTagDate -toDate $toTagDate -branchVersion $branchVersion $::CVS_WORK_DIR $::CVS_REPORTS_DIR]
    } else {
        set status [wait $pid]
        if {[lrange $status 1 2] != "EXIT 0"} {
            error "Build of CVS pages failed"
        }
    }
}

if {$argc != 7} {
    puts stderr "wrong \# args: cvs-reports-delta fromTag toTag fromTagDate toTagDate whichReport branchVersion sandBox"
    puts stderr "   fromTag and toTag are in the form: branch or review "
    puts stderr "   fromTagDate and toTagDate are in the form: 2004-06-07 "
    puts stderr "   whichReport is either 'branch' or 'Review' (no quotes)"
    puts stderr "   branchVersion is v${BRANCHNN} e.g. v144"
    puts stderr "   sandBox is the location of the checked out source"
    exit 1
}


set fromTag [lindex $argv 0]
set   toTag [lindex $argv 1]
set fromTagDate [lindex $argv 2]
set   toTagDate [lindex $argv 3]
set  whichReport [lindex $argv 4]
set  branchVersion [lindex $argv 5]
set  sandBox [lindex $argv 6]

umask 0002

if {($whichReport != "branch") && ($whichReport != "review")} {
    puts stderr "whichReport should be brance or review: cvs-reports-delta fromTag toTag fromTagDate toTagDate whichReport branchVersion sandBox"
    puts stderr "   fromTag and toTag are in the form: branch or review "
    puts stderr "   fromTagDate and toTagDate are in the form: 2004-06-07 "
    puts stderr "   whichReport is either 'branch' or 'review' (no quotes)"
    puts stderr "   branchVersion is v${BRANCHNN} e.g. v144"
    puts stderr "   sandBox is the location of the checked out source"
    exit 1
}

# this is the base
set CVS_REPORTS_BASE /hive/groups/qa/cvs-reports-latest/
set CVS_REPORTS_DIR $CVS_REPORTS_BASE/$whichReport

# this is the cvs sandbox used (now shared by branch and review versions of cvs-reports)
# old set CVS_WORK_DIR $CVS_REPORTS_BASE/kent
# new method using -no-update assumes sandbox is already up to date
set CVS_WORK_DIR $sandBox

# this is the program name
set CVS_REPORTS ./cvs-reports-d

set lockFile [mkLock]
try_eval {
    buildReports $fromTag $toTag $fromTagDate $toTagDate $branchVersion
} {
    file delete $lockFile
    puts "Error: $errorResult"
    exit 1
}
file delete $lockFile

# Local Variables: **
# mode: tcl **
# End: **
