
# Dummy routines which allow Mp procedures to execute without
# the Procedure Execution Manager (PEM)

proc APSMpReturnHelp {} {
    return "Usage: APSMpReturn code \[results\]\nwhere code may be ok or error\n\nThis procedure should be called in place of usual tcl return when creating\nmachine procedures. Note that a code of error will generate a catchable\nerror in the calling procedure."
}

proc APSMpReturn {code {results ""}} {
    global apsNetworkDomain apsScriptUser apsScriptHost

    if {[info level] == 2} {
	# Before returning to top level calling context, synchronize
	#   with all outstanding parallel processes first.
	global apsMpParallelProcesses
	if {[info exists apsMpParallelProcesses]} {
	    foreach id $apsMpParallelProcesses {
		catch {APSMpJoin $id}
	    }
	}
	if {(![string compare $apsNetworkDomain "accel.ntw0rk"]) || (![string compare $apsNetworkDomain "aps4.anl.gov"])} {
	    set procName [lindex [info level [expr [info level] - 1]] 0]
	    if {![string compare $code "ok"]} {
		set action Finish
	    } else {
		set action Error
	    }
#	    set logCmd "logMessage -sourceId=mpExecAudit -tag=mpName $procName -tag=invokingHost \"$apsScriptHost\" -tag=executionHost \"$apsScriptHost\" -tag=user $apsScriptUser -tag=action $action"
	    set logCmd "logMessage -sourceId=scriptAction -tag=User $apsScriptUser -tag=Host \"$apsScriptHost\" -tag=Procedure $procName -tag=Action $action -tag=Parameters ? -tag=Status ? -tag=Script ? -tag=Stack ?"
	    catch {eval exec $logCmd}
	}    
    }
	
    if {![string compare $code "ok"]} {
	return -code return $results
    } else {
	return -code error $results
    }
}

proc APSMpStepHelp {} {
    return "Usage: APSMpStep stepName\n\[-vars <varList>\]\n\[-promptDialog <procName>\] where <procName> is of form\n\t  proc <procName> \{frame\} \{\}\n\nDefines a step within a machine procedure. When executed from PEM,\neach step name is displayed as it is executed. When a procedure is\nexecuted in Automatic mode or outside of the PEM, steps are ignored.\nSee document for details on use of -vars and -promptDialog options."
}

proc APSMpStep {stepName args} {
    global apsNetworkDomain apsScriptUser apsScriptHost

    if {(![string compare $apsNetworkDomain "accel.ntw0rk"]) || (![string compare $apsNetworkDomain "aps4.anl.gov"])} {
	set procName [lindex [info level [expr [info level] - 1]] 0]	
#	set logCmd "logMessage -sourceId=mpExecAudit -tag=mpName $procName -tag=invokingHost \"$apsScriptHost\" -tag=executionHost \"$apsScriptHost\" -tag=user $apsScriptUser -tag=action Start"
	set logCmd "logMessage -sourceId=scriptAction -tag=User $apsScriptUser -tag=Host \"$apsScriptHost\" -tag=Procedure $procName -tag=Action Start -tag=Parameters ? -tag=Status ? -tag=Script ? -tag=Stack ?"
	catch {eval exec $logCmd}
    }
    return
}

