

set fpDebugPemTest ""
set pemTestDebug 0
proc Debug {msg} {
    global pemTestDebug fpDebugPemTest
    if {$pemTestDebug && ![string length $fpDebugPemTest]} {
        set fpDebugPemTest [open /tmp/pemTest.[pid].log w]
    }
    if {$pemTestDebug} {
	puts $fpDebugPemTest "$msg"
        flush $fpDebugPemTest
    }
}

proc APSPEMTestProc1Info {} {
    return "This procedure runs PEM test 1."
}

proc APSPEMTestProc2Info {} {
    return "This procedure runs PEM test 1."
}

proc APSPEMTestProc3Info {} {
    return "This procedure runs PEM test 1."
}

proc APSPEMTestProc123SeqInfo {} {
    return "This procedure runs PEM test 1, 2, and 3 in sequence."
}

proc APSPEMTestProc123ParInfo {} {
    return "This procedure runs PEM test 1, 2, and 3 in parallel."
}

proc APSPEMTestDummy {} {
    APSMpReturn ok
}

proc APSPEMTestProc1 {hangUp} {
    global status
    APSMpStep init
    APSMpStep Proc1Step1
    while {$hangUp} {
        after 500
        update
    }
    if {[catch {APSPEMTestDummy} results]} {
        APSMpReturn error "APSPEMTestProc1: $results"
    }
    APSMpStep Proc1Step2
    APSWaitWithUpdate -waitSeconds 3 -updateInterval 1
    APSMpStep Proc1Step3
    APSMpStep APSPEMTestProc4 -proc APSPEMTestProc4

    set id4 [APSMpParallel \
                  -procedure "APSPEMTestProc4 $hangUp" \
                  -mode Automatic]
    set id5 [APSMpParallel \
                 -procedure "APSPEMTestProc5 $hangUp" \
                -mode Automatic]
    APSMpAbort [list $id4 $id5] -onError 1
    set status "Joining 4 and 5: [clock format [clock sec]]"
    if {[catch {APSMpJoin $id4} results]} {
        Debug "Error from 4: $results"
	APSMpReturn error "APSPEMTestProc1: $results"
    }
    if {[catch {APSMpJoin $id5} results]} {
        Debug "Error from 5: $results"
	APSMpReturn error "APSPEMTestProc1: $results"
    }
    set status "Joined 4 and 5: [clock format [clock sec]]"

    APSMpReturn ok
}

proc APSPEMTestProc2 {hangUp} {
    set prefix "Click here for "
    APSMpStep init
    APSMpStep ${prefix}Proc2Step1
    while {$hangUp}  {
        after 500
        update
    }
    APSMpStep ${prefix}Proc2Step2
    APSWaitWithUpdate -waitSeconds 3 -updateInterval 1
    if {[catch {APSPEMTestDummy} results]} {
        APSMpReturn error "APSPEMTestProc2: $results"
    }
    APSMpStep ${prefix}Proc2Step3
    #APSMpReturn error "this is a test"
    APSMpReturn ok
}

proc APSPEMTestProc3 {hangUp} {
    set prefix "Click here for "
    APSMpStep init
    APSMpStep ${prefix}Proc3Step1 -desc "This will start Proc3Step1"
    APSPEMTestDummy
    while {$hangUp}  {
        after 500
        update
    }
    if {[catch {APSPEMTestDummy} results]} {
        APSMpReturn error "APSPEMTestProc3: $results"
    }
    APSMpStep ${prefix}Proc3Step2 -desc "This will start Proc3Step2"
    APSWaitWithUpdate -waitSeconds 5 -updateInterval 1
    APSMpStep ${prefix}Proc3Step3 -desc "This will start Proc3Step3"
    APSMpReturn ok
}

proc APSPEMTestProc4 {hangUp} {
    Debug "APSPEMTestProc4: [clock format [clock sec]]"
    APSMpStep init
    APSMpStep Proc4Step1
    if {[catch {APSPEMTestDummy} results]} {
        APSMpReturn error "APSPEMTestProc4: $results"
    }
    while {$hangUp}  {
        after 500
        update
    }
    if {[catch {APSPEMTestDummy} results]} {
        APSMpReturn error "APSPEMTestProc4: $results"
    }
    APSMpStep Proc4Step2
    APSWaitWithUpdate -waitSeconds 10
    APSMpStep Proc4Step3
    Debug "APSPEMTestProc4: about to return: [clock format [clock sec]]"
    APSMpReturn ok
}

proc APSPEMTestProc5 {hangUp} {
    Debug "APSPEMTestProc5: [clock format [clock sec]]"
    APSMpStep init
    APSMpStep Proc5Step1
    if {[catch {APSPEMTestDummy} results]} {
        APSMpReturn error "APSPEMTestProc5: $results"
    }
    while {$hangUp}  {
        after 500
        update
    }
    if {[catch {APSPEMTestDummy} results]} {
        APSMpReturn error "APSPEMTestProc5: $results"
    }
    APSMpStep Proc5Step2
    APSWaitWithUpdate -waitSeconds 10
    APSMpStep Proc5Step3
    set id4 [APSMpParallel \
                 -procedure "APSPEMTestProc4 $hangUp" \
                -mode Semi-Automatic]
    APSMpAbort [list $id4] -onError 1
    set status "Joining 4: [clock format [clock sec]]"
    if {[catch {APSMpJoin $id4} results]} {
        Debug "Error from 4: $results"
	APSMpReturn error "APSPEMTestProc5: $results"
    }

    Debug "APSPEMTestProc5: about to return: [clock format [clock sec]]"
    APSMpReturn ok
}

proc APSPEMTestProc123Seq {} {
    set R1 1
    set R2 1
    set R3 1
    set myHang 0
    set myError 0

    APSMpStep init -promptDialog APSPEMTestProc123SeqInitDialog \
      -vars {R1 R2 R3 myHang myError}
    if {$R1} {
        APSMpStep Proc1 
        if {[catch {APSPEMTestProc1 $myHang} results]} {
            APSMpReturn error "APSPEMTestProc123Seq: $results"
        }
    }
    if {$R2} {
        APSMpStep Proc2
        if {[catch {APSPEMTestProc2 $myHang} results]} {
            APSMpReturn error "APSPEMTestProc123Seq: $results"
        }
    }
    if {$R3} {
        APSMpStep Proc3
        if {[catch {APSPEMTestProc3 $myHang} results]} {
            APSMpReturn error "APSPEMTestProc123Seq: $results"
        }
    }
    APSMpStep Done123Seq
    if {$myError} {
        APSMpReturn error "forced error"
    }
    APSMpReturn ok
}

proc APSPEMTestProc123Par {} {
    set R1 1
    set R2 1
    set R3 1
    set myHang 0
    set myError 0

    APSMpStep init -promptDialog APSPEMTestProc123ParInitDialog \
      -vars {R1 R2 R3 myHang myError}

    global hangUpInProcs
    # set hangUpInProcs $myHang

    set idList ""
    if {$R1} {
        APSMpStep Proc1
        set id1 [APSMpParallel \
                   -procedure "APSPEMTestProc1 $myHang" \
                   -mode Automatic]
        lappend idList $id1
    }
    if {$R2} {
        APSMpStep Proc2
        set id2 [APSMpParallel \
                   -procedure "APSPEMTestProc2 $myHang" \
                   -mode Automatic]
        lappend idList $id2
    }
    if {$R3} {
        APSMpStep Proc3
        set id3 [APSMpParallel \
                   -procedure "APSPEMTestProc3 $myHang" \
                   -mode Automatic]
        lappend idList $id3
    }
    if {[llength $idList]} {
        APSMpAbort $idList -onError 1
    }
    set status "IDs: [join $idList \n]"
    if {$R1} {
        if {[catch {APSMpJoin $id1} results]} {
	    APSMpReturn error "APSPEMTestProc123Par: $results"
	}
    }
    if {$R2} {
        if {[catch {APSMpJoin $id2} results]} {
	    APSMpReturn error "APSPEMTestProc123Par: $results"
	}
    }
    if {$R3} {
        if {[catch {APSMpJoin $id3} results]} {
	    APSMpReturn error "APSPEMTestProc123Par: $results"
	}
    }
    APSMpStep Done123Par
    if {$myError} {
        APSMpReturn error "forced error"
    }
    APSMpReturn ok
}

proc APSPEMTestProc123SeqInitDialog {frame} {
    global APSPEMTestProc123Seq
    APSRadioButtonFrame .r1 -parent $frame -label "Procedure 1: " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Seq(R1)
    APSRadioButtonFrame .r2 -parent $frame -label "Procedure 2: " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Seq(R2)
    APSRadioButtonFrame .r3 -parent $frame -label "Procedure 3: " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Seq(R3)
    APSRadioButtonFrame .r4 -parent $frame -label "Hang? " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Seq(myHang)
    APSRadioButtonFrame .r5 -parent $frame -label "Return error? " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Seq(myError)
}

proc APSPEMTestProc123ParInitDialog {frame} {
    global APSPEMTestProc123Par
    APSRadioButtonFrame .r1 -parent $frame -label "Procedure 1: " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Par(R1)
    APSRadioButtonFrame .r2 -parent $frame -label "Procedure 2: " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Par(R2)
    APSRadioButtonFrame .r3 -parent $frame -label "Procedure 3: " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Par(R3)
    APSRadioButtonFrame .r4 -parent $frame -label "Hang? " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Par(myHang)
    APSRadioButtonFrame .r5 -parent $frame -label "Return error? " \
      -buttonList {Yes No} -valueList {1 0} \
      -variable APSPEMTestProc123Par(myError)
}
