# Library functions for the batch_queue library 
#
# File: batch_queue/lib/xgrid.tcl
#
# $Revision: 392681 $ on $Date: 2004-03-25 13:58:24 -0500 (Thu, 25 Mar 2004) $
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
# ------------------------------------------------------------------------------------

# Portions of this documentation ("XGrid usage: *) have been taken from
# the xgrid documentation and those portions are subject to the
# respective licensing.

# ------------------------------------------------------------------------------------

# xgrid returns its own job_token which we map to our own.

proc batch_queue_xgrid_get_job_ident { job_token } {
    global xgrid_job_identifiers    
    if ![info exists xgrid_job_identifiers($job_token)] { return -code error "" }
    return $xgrid_job_identifiers(job_token)
}

# xgrid returns its own job_token which we map to our own.

proc batch_queue_xgrid_set_job_ident { job_token xg_job_identifier } {
    global xgrid_job_identifiers
    if [info exists xgrid_job_identifiers($job_token)] {
	mess "XGrid job identifier \"$xg_job_identifier\" already exists for job_token job_token \"$job_token\" ...\n" red
	# return (not yet we don't)
    }
    set xgrid_job_identifiers($job_token) $xg_job_identifier
}

## Submit a job (XGrid synonym: xgrid -job submit)<br/>
## XGrid usage:
## <pre>
## xgrid -job submit [-si stdin] [-in indir] cmd [arg1 [...]]
## </pre>

proc batch_queue_xgrid_submit_job { job_token script args } {

    set logfile ""
    foreach {n v} $args { set [string range $n 1 end] $v }
    
    if [catch { eval exec xgrid -job submit "$script" } xg_job_identifier] {
	mess "Error in xgrid -job submit: $xg_job_identifier\n" red
	return [batch_queue_cmd_failed]
    }
    
    batch_queue_xgrid_set_job_ident $job_token $xg_job_identifier
    
    return [batch_queue_cmd_succeeded]
}

# ------------------------------------------------------------------------------------

## Show status of batch jobs (XGrid synonym: xgrid -job status)<br/>
## XGrid usage:
## <pre>
## xgrid -job status -id identifier
## </pre>

proc batch_queue_xgrid_job_status { job_token } {
    
    set xg_job_identifier [batch_queue_xgrid_get_job_ident $job_token]
    
    if [catch { eval exec xgrid -job status -id $xg_job_identifier } res] {
	mess "Error in xgrid -job status: $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return $res
}

# ------------------------------------------------------------------------------------

## Hold a batch job (XGrid synonym: )<br/>
## XGrid usage:
## <pre>
## 
## </pre> 

proc batch_queue_xgrid_hold_job {job_identifier {args ""}} {
    
    if [catch { eval exec xgrid -job stop -id $job_identifier } res] {
	mess "Error in xgrid -job stop: $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}

# ------------------------------------------------------------------------------------

## Delete a batch job (XGrid synonym: )<br/>
## XGrid usage:
## <pre>
## xgrid -job delete -id identifier
## </pre>

proc batch_queue_xgrid_delete_job { job_identifier {args ""} } {
    
    if [catch { eval exec xgrid -job delete -id $job_identifier } res] {
	mess "Error in xgrid -job delete: $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}

# ------------------------------------------------------------------------------------

## Alter a batch job (XGrid synonym: )<br/>
## XGrid usage:
## <pre>
## 
## </pre>

proc batch_queue_xgrid_alter_job { job_identifier args } {
    
    if [catch { eval exec xgrid $args $job_identifier } res] {
	mess "Error in xgrid-qalter: $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}


# ------------------------------------------------------------------------------------

## Send a message to a batch job (XGrid synonym: )<br/>
## XGrid usage:
## <pre>
##
## </pre>

proc batch_queue_xgrid_message {job_identifier message_string {args ""}} {
    
    if [catch { eval exec qmsg $args $message_string $job_identifier } res] {
	mess "Error in xgrid-qmsg: $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}


# ------------------------------------------------------------------------------------

## Move a batch job to another queue (XGrid synonym: )<br/>
## XGrid usage:
## <pre>
##
## </pre>

proc batch_queue_xgrid_move_job {job_identifier args} {    
    
    if [catch { eval exec qmove $args $message_string $job_identifier } res] {
	mess "Error in xgrid-qmove: $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}


# ------------------------------------------------------------------------------------

## Release held jobs (XGrid synonym: )<br/>
## XGrid usage:
## <pre>
##
## </pre>

proc batch_queue_xgrid_release_job {job_identifier {args ""}} {
        
    if [catch { eval exec qrls $args $message_string $job_identifier } res] {
	mess "Error in xgrid : $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}


# ------------------------------------------------------------------------------------

## Rerun a batch job (XGrid synonym: )<br/>
## XGrid usage:
## <pre>
##
## </pre>

proc batch_queue_xgrid_rerun_job {job_identifier} {
    
    if [catch { eval exec qrerun $job_identifier } res] {
	mess "Error in xgrid : $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}

# ------------------------------------------------------------------------------------

## Select a specific subset of jobs (XGrid synonym: )<br/>
## XGrid usage:
## <pre>
## 
## </pre>

proc batch_queue_xgrid_select_jobs {args} {
    
    if [catch { eval exec qselect $args } res] {
	mess "Error in xgrid : $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}


# ------------------------------------------------------------------------------------

## Send a signal to a batch job (XGrid synonym: )<br/>
## XGrid usage:
## <pre>
## 
## </pre>

proc batch_queue_xgrid_signal {job_identifier {args ""}} {
    
    if [catch { eval exec qsig $args } res] {
	mess "Error in xgrid : $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}

