#!/bin/csh

if(`printenv SW_BASE` == "") setenv SW_BASE /usr/local/softWoRx
if(`uname -s` == "Linux") then
	set TARG=`uname -p`
else
	set TARG=mips
endif

# determine active configuration
set CfgSuffix = ""
set ActiveConfig = `grep -sv "#" "$HOME"/.swrc | grep MICROSCOPE_CFG_ACTIVE`

if ($#ActiveConfig > 1) then
  if ($ActiveConfig[1] == "MICROSCOPE_CFG_ACTIVE") then
    if ($ActiveConfig[2] != "0") then
      set CfgSuffix = "_$ActiveConfig[2]"
    endif
  endif
else
  set ActiveConfig = `grep -sv "#" $SW_BASE/config/system.swrc | grep MICROSCOPE_CFG_ACTIVE`

  if ($#ActiveConfig > 1) then
    if ($ActiveConfig[1] == "MICROSCOPE_CFG_ACTIVE") then
      if ($ActiveConfig[2] != "0") then
	set CfgSuffix = "_$ActiveConfig[2]"
      endif
    endif
  endif
endif

set PrevConfig = "$SW_BASE"/config/Resolve3D.SYS"$CfgSuffix"

if (! (-e $PrevConfig)) then
  echo ""
  echo "Unable to find the active configuration file:"
  echo "$PrevConfig"
  echo ""
  exit(-1)
endif

echo ""
echo "Active Configuration File: $PrevConfig"
echo ""

set NEWNAME="$PrevConfig"_`date +"%m%d%y"`

if(`uname -s` == "Linux") then
    if(-e /usr/bin/gedit) then
        set WINEDITOR=gedit
    else if (-e /usr/bin/kwrite) then
        set WINEDITOR=kwrite
    endif
endif

if(!($?WINEDITOR)) setenv WINEDITOR "gedit"

if($WINEDITOR == "") then
    echo "No Appropriate Graphical Editor Found"
    return 1
endif

set COMMAND="cp $PrevConfig $NEWNAME ; $WINEDITOR $PrevConfig"

if(`whoami` != 'root') then
    echo ""
    echo "<--> To make changes, you will need to "
    echo "<--> provide superuser (root) password."
    echo ""
    echo "<--> To only view the configuration, press Enter,"
    echo "<------> then use the up and down arrow keys on the"
    echo "<------> keyboard to scroll through the file."
    echo "<------> Type q to quit."
    echo "<------> "
    echo "<------> "
    echo "root password: \c"
endif

su root -c "$COMMAND" 
if($status) then
    exec less $PrevConfig
else
	echo ""
	echo ""
    echo "                        You will need to re-start the Acquire Module for changes to take effect" 
endif
