#!/bin/sh  
# \
exec oagwish "$0" "$@"


set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

set args $argv
set pvaName ""
set value ""
set mode add
set inputFile ""
set usage "pvaput -pvaName <name> -value <value> -mode <add|absolute|multiply|set> -inputFile <filename>\n
-pvaName     required, provide the PVA channel name.\n
-value       value to set, either value or input file has to be provided. \n
-mode        optional, default: add -- set in delta mode: absolute -- set absolute value; \n
-inputFile   provide the value for each PVs contained in the PVA channel, must have \n
             ControlName, and Value column, Weight column is optional. \n
             if Weight column is provided, the acutal set value is Value*Weight.\n"

APSParseArguments {pvaName value mode inputFile}

if ![string length $argv] {
    puts $usage
    exit 0
}

if ![string length $pvaName] {
    puts stderr "Error1 (pvaput): pvaName not provided"
    exit 1
}
if {![string length $value] && ![string length $inputFile]} {
    puts stderr "Error2 (pvaput): either value or inputFile must provided!"
    exit 1
}

if [catch {exec pvinfo $pvaName} result] {
    puts stderr "Error3 (pvaput) pvinfo $pvaName: $result!"
    exit 1
}

if [string length $value] {
    if [string length $mode] {
	exec daq-pv-group set --group-channel $pvaName --value $value --mode $mode
    } else {
	exec daq-pv-group set --group-channel $pvaName --value $value
    }
} elseif [string length $inputFile] {
    if ![file exist $inputFile] {
	puts stderr "Error4 (pvaput): $inputFile does not exist!"
	exit 1
    }
    if [string length $mode] {
	exec daq-pv-group set  --group-channel $pvaName --input-sdds-file $inputFile --mode $mode
    } else {
	exec daq-pv-group set --group-channel $pvaName --input-sdds-file $inputFile
    }
} else {
    puts stderr "Error5 (pvaput): either value or inputFile has to be provided!"
    exit 1
}

exit 0
