# Library functions for the batch_queue library 
#
# File: batch_queue/lib/pbs.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
# ------------------------------------------------------------------------------------

# Portions of this documentation ("PBS usage: *) have been taken from
# the OpenPBS documentation and those portions are subject to the
# respective licensing.
#
# Cf. http://www.openpbs.org/UserArea/commands.html
#     http://www.openpbs.org/UserArea/glossary.html

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

## Alter a batch job (PBS synonym: qalter)<br/>
## PBS usage:
## <pre>
## qalter [-a date_time]	[-A account_string] [-c	interval] [-e
## 	  path]	[-h hold_list] [-j join] [-k keep] [-l resource_list]
## 	  [-m mail_options] [-M	user_list] [-N name] [-o path] [-p
## 	  priority] [-r	c] [-S path] [-u user_list] [-W
## 	  additional_attributes] job_identifier...
## </pre>

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


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

## Delete a batch job (PBS synonym: qdel)<br/>
## PBS usage:
## <pre>
## qdel [-W delay] job_identifier ...
## </pre>

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


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

## Hold a batch job (PBS synonym: qhold)<br/>
## PBS usage:
## <pre>
## qhold [-h hold_list] job_identifier ...
## </pre> 

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


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

## Send a message to a batch job (PBS synonym: qmsg)<br/>
## PBS usage:
## <pre>
## qmsg [-E] [-O] message_string	job_identifier ...
## </pre>

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


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

## Move a batch job to another queue (PBS synonym: qmove)<br/>
## PBS usage:
## <pre>
## qmove destination job_identifier ...
## </pre>

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


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

## Release held jobs (PBS synonym: qrls)<br/>
## PBS usage:
## <pre>
## qrls [-h hold_list] job_identifier ...
## </pre>

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


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

## Rerun a batch job (PBS synonym: qrerun)<br/>
## PBS usage:
## <pre>
## qrerun job_identifier	...
## </pre>

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

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

## Select a specific subset of jobs (PBS synonym: qselect)<br/>
## PBS usage:
## qselect [-a [op]date_time] [-A account_string] [-c
## 	  [op]interval]	[-h hold_list] [-l resource_list] [-N name]
## 	  [-p [op]priority] [-q	destination] [-r rerun]	[-s states]
## 	  [-u user_list]
## </pre>

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


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

## Send a signal to a batch job (PBS synonym: qsig)<br/>
## PBS usage:
## <pre>
## qsig [-s signal] job_identifier ...
## </pre>

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


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

## Show status of batch jobs (PBS synonym: qstat)<br/>
## PBS usage:
## <pre>
## qstat [-f][-W site_specific] [job_identifier... |
## 	  destination...]
## qstat	-Q [-f][-W site_specific] [destination...]
## qstat	-B [-f][-W site_specific] [server_name...]
## </pre>

proc batch_queue_pbs_status {args} {
    
    if [catch { eval exec qstat $args } res] {
	mess "Error in pbs-qstat: $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return $res
}


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

## Submit a job (PBS synonym: qsub)<br/>
## PBS usage:
## <pre>
## qsub [-a date_time] [-A account_string] [-c interval] [-C
## 	  directive_prefix] [-e	path] [-h] [-I]	[-j join] [-k keep]
## 	  [-l resource_list] [-m mail_options] [-M user_list] [-N
## 	  name]	[-o path] [-p priority]	[-q destination] [-r c]	[-S
## 	  path_list] [-u user_list] [-v	variable_list] [-V] [-W
## 	  additional_attributes] [-z] [script]
## </pre>

proc batch_queue_pbs_submit_job { job_token script args } {
    
    set priority  1
    set logfile   ""
    
    foreach {n v} $args {
	set [string range $n 1 end] $v
    }
    
    set cmd "qsub -q regular -p $priority -N $job_token \
             -e $logfile.stderr -o $logfile.stdout $script"
    
    mess "DEBUG: $cmd\n" red
    
    if [catch { eval exec $cmd } res] {
	mess "Error in pbs-qsub: $res\n" red
	return [batch_queue_cmd_failed]
    }
    
    return [batch_queue_cmd_succeeded]
}
