#!/bin/csh -f
#+
# pmfm -- set/show the amount of focus mode in the telescope primary
#
# Purpose:
#	With no argument, show the currently amount of primary mirror
#	focus mode.  With a argument, set the amount of primary mirror
#	focus mode to the specified number of nanometers.
#
# Usage:
#	pmfm [x]
# 
# Arguments:
#	x = amount of "focus mode" to introduce [nanometers]
# 
# Exit values:
#	 0 = normal completion
#	 1 = wrong number of arguments
#
# Example:
#	1) show the current amount of pmfm:
#		pmfm
#
#	2) apply 500 nm of primary mirror focus mode:
#		pmfm 500
# 
# 	3) make pmfm change in background:
#		pmfm 500 </dev/null &
#
#-
# Modification history:
#	Date unknown	RWG	Original version
#	2000-Jul-05	GDW	Added documentation
#	2000-Oct-15	GDW	Added rsh okoe crud to avoid problem with
#				access to ACS keywords from punaluu
#-----------------------------------------------------------------------

set cmd = `basename $0`

# check args...
if (${#argv} > 1) then
  echo "Usage: pmfm [x]"
  exit 1
endif
if (${#argv} == 1) then
    set want = $1
    shift
endif

set rcmd = "rsh k2server -l k2ruts"

# set/show the amount of pmfm...
if ( $?want ) then
    while (1)
	$rcmd modify -s acs pmfm = $want
	set buf = `$rcmd show -s acs -terse pmfm`
	set got = `calc "nint($buf)"`
	set want2 = `calc "nint($want)"`
	if ( "$got" == "$want2" ) break
	printf "[$cmd] WARNING: requested PMFM=$want but got PMFM=$got -- trying again!\n"
	sleep 1
    end
else
    $rcmd show -s acs -terse pmfm
endif
