#!/bin/tcsh -f
# inclass.csh -- set up an indirect classing environment
# Eric Mandel 2/22/96
#

unalias rm

# initialize SAORD variables
if ( $?SAORD_ROOT == 0 ) then
  setenv SAORD_ROOT /scisoft/saord
endif
if ( -e ${SAORD_ROOT}/xpa1 ) then
  setenv XPA_VERSION 1
else
  setenv XPA_VERSION 2
endif
if ( $?SAORD_BIN == 0 ) then
  setenv SAORD_BIN ${SAORD_ROOT}/bin.`${SAORD_ROOT}/getarch`
endif
if ( $?SAORD_TMPDIR == 0 ) then
  setenv SAORD_TMPDIR /tmp
endif
setenv SAORD_VERSION "`cat ${SAORD_ROOT}/VERSION`"
# end of SAORD initialization

set assist = `${SAORD_BIN}/xpaaccess ASSIST`
set inclass = ""
set activate = ""
set n = 1
set TMP_FILE = /tmp/inclass.ls

while ("$n" <= "$#argv")
    if ( "$argv[$n]" == "-echo" ) then
	set echo
    else if ( "$argv[$n]" == "NONE" ) then
	set inclass = "NONE"
	break;
    else if ( "$argv[$n]" == "off" ) then
	set inclass = $argv[$n]
	break
    else if ( "$argv[$n]" == "OFF" ) then
	set inclass = $argv[$n]
	break
    else if ( "$argv[$n]" == "on" ) then
	set inclass = $argv[$n]
	break
    else if ( "$argv[$n]" == "ON" ) then
	set inclass = $argv[$n]
	break
    else
	set pfile = $argv[$n]
	rm -f ${TMP_FILE}
	(${SAORD_BIN}/pget $pfile inclass_type >${TMP_FILE} )>& /dev/null
	set type = "`cat ${TMP_FILE}`"
	rm -f ${TMP_FILE}
	if ( "$type" != "" ) then
	    set inclass = "${inclass} $type $pfile"
	    set activate = "${activate} $pfile"
	else
	    rm -f ${TMP_FILE}
	    (${SAORD_BIN}/pget $pfile mode >${TMP_FILE} )>& /dev/null
	    set mode = "`cat ${TMP_FILE}`"
	    rm -f ${TMP_FILE}
	    if ( "$mode" != "" ) then
		echo "Invalid inclass parameter file: $pfile"
	    else
		set inclass = "${inclass} PINCLASSDB $pfile"
	    endif
	endif
    endif	
    @ n++
end

if ( "$assist" == "yes" ) then
  if ( "$inclass" != "" ) then
	echo "inclass $inclass" | ${SAORD_BIN}/xpaset ASSIST
  endif
  if ( "$activate" != "" ) then
	set name = ${pfile:t}
	set name = ${name:r}
	echo "add $name ParamEditor '$name -- Common Data Set' 'unix NOPKG $name pfile'" | ${SAORD_BIN}/xpaset ASSIST
	echo "activate $name" | ${SAORD_BIN}/xpaset ASSIST
  endif
endif
