#!/bin/csh -f

set OSNAME=`uname -s|sed 's/[^a-zA-Z]//g'`

alias gosub 'set stack=(\!:3 $stack); set __args=(\!:2) ; goto \!:1'
alias return 'set __lab=$stack[1] ; set stack=($stack[2-]) ; goto $__lab'
alias matches 'echo \!:1 |egrep -i \!:2|wc -l'
set stack=()

if (! $?BACKUP_HOME) then
  set p=$0
  set pnam=$p
  set pnam=$pnam:t
  if ($pnam == $0) then
    foreach dir ($path)
      if (-x $dir/$pnam) then
	set p=$dir/$pnam
	break
      endif
    end
  endif
  if (`echo $p|cut -c1` != "/") then
    set p=`pwd`/$p
  endif

  set oldpath=$p

 restart_resolv:
  set newpath="$oldpath"_
  while ("$newpath" != "$oldpath")
    set newpath="$oldpath"

    set oldpath=`echo "$oldpath"|sed 's#/\./#/#g;s#/\.$##g;s#//*#/#g;s#^\./##g;s#^/\.\./#/#g'`

    if ("$oldpath" != '/') then
      set oldpath=`echo "$oldpath"|sed 's#/*$##g'`
    endif
  end

  if (`echo "$oldpath"|cut -c1` == "/") then
    set oldhead="/"`echo "$oldpath"|cut -c2-|sed 's#/.*$##g'`
    set oldtail=`echo "$oldpath"|cut -c2-|sed 's#^[^/]*/##'`
  else
    set oldhead=`echo "$oldpath"|sed 's#/.*$##g'`
    set oldtail=`echo "$oldpath"|sed 's#^[^/]*/##'`
  endif
  if ("$oldpath" == "$oldhead") set oldtail=""

  while (1)
    if ("$oldhead" == "") set oldhead="."
    if (! -f "$oldhead" && ! -d "$oldhead") then
      set newpath=""
      goto getout_1
    endif

    set linkchar=`ls -ld "$oldhead"|cut -c1`
    if ($linkchar == 'l') then
      set points_to=`ls -ld "$oldhead"|sed 's#^.*->[ 	]*##g'`

      if (`echo "$points_to"|cut -c1` == '/') then
        set newpath="$points_to"/"$oldtail"
      else
        set newpath=`dirname "$oldhead"`/"$points_to"/"$oldtail"
      endif

      set oldpath="$newpath"
      goto restart_resolv
    endif

    if ("$oldtail" == "") then
      set oldpath="$oldhead"
      break
    endif

    set oldhead="$oldhead"/`echo "$oldtail"|sed 's#/.*$##g'`
    if (`echo "$oldtail"|grep /|wc -l` < 1) then
      set oldtail=''
    else
      set oldtail=`echo "$oldtail"|sed 's#^[^/]*/##g'`
    endif
  end

  set newpath="$oldpath"_
  while ("$newpath" != "$oldpath")
    set newpath="$oldpath"

    set oldpath=`echo $oldpath|sed 's#[^/][^/]*/\.\./##g;s#[^/][^/]*/\.\.$##g;s#/\./#/#g;s#/\.$##g;s#//*#/#g;s#^\./##g;s#^/\.\./#/#g'`

    if ("$oldpath" != '/') then
      set oldpath=`echo "$oldpath"|sed 's#/*$##g'`
    endif
  end

 getout_1:

  set p="$newpath"
  set p=$p:h

  setenv BACKUP_HOME $p:h
endif


set path=($BACKUP_HOME/bin $path)
rehash

unsetenv AWK

# the solaris awk is doin' dawn f...... BS
foreach awk (nawk gawk awk)
  foreach dir ($path)
    if (-x $dir/$awk) then
      setenv AWK $dir/$awk
      break
    endif
  end
  if ($?AWK) break
end

if (! $?AWK) then
  echo 'No awk ? Is this really one of the wonderful UNIX-es ?'
  echo 'Sorry. I have to exit.'
  exit 1
endif

set found=0
set configfiles=($BACKUP_HOME/lib/backup.conf /etc/buserver.conf /etc/afbackup/server.conf "@serverlibdir@/@serverconf@")

foreach configfile ($configfiles)
  if (-r $configfile) then
    set found=1
    break
  endif
end
if (! $found) then
  echo "No configuration file found. Exiting."
  exit 2
endif

alias getparam $AWK' '"'"'/^[ 	]*'"'"'\!:1'"'"'/{split($0,a,"'"'"'\!:1'"'"'[ 	]*"); printf "%s\n",a[2]}'"' $configfile"

set num_fix_params=24
set num_var_sp_idx=0

set comments=("")
set names=("")
set values=("")
set prompts=("")
set helps=("")
set patterns=("")

set i=-1
while ($i <= $num_fix_params)
  @ i++
  set comments=($comments $i)
  set names=($names $i)
  set values=($values $i)
  set prompts=($prompts $i)
  set helps=($helps $i)
  set patterns=($patterns $i)
end
set i=1
while ($i <= $#values)
  set values[$i]=""
  @ i++
end


set comments[1]='\DEVICE SPECIFICATION\\The device name (No-Rewind-Version \!)'
set comments[2]='\The blocksize of the device'
set comments[3]='\Flag, whether the device is a robot / cartridge handling system (0 or 1)'
set comments[4]='\Insert your number of cartridges, whether you have a robot or not'
set comments[5]='\The last cartridges of different cartridge sets, if requested.\Default is 1 set with cartridges ranging from 1 up to the\previous parameter. If e.g. 3 cartridge sets are requested:\1-3, 4-8, 9-10, supply here: 3 8 10\If not all cartridges should be used, the last number may be\smaller than the previous parameter'
set comments[6]='\The maximum number of bytes per file on tape'
set comments[7]='\Time to wait from the moment, a new cartridge has been inserted\ until a new action is attempted on the tape device'
set comments[8]='\Time to wait while the device is unavailable until a mail is\sent to the user in charge'
set comments[9]='\Time to wait while the device is unavailable until abort'
set comments[10]='\The interval in seconds for probing, if device is ready, when\inserting another cartridge is requested. 0 for no probing.'
set comments[11]='\\SHELL-COMMANDS FOR TAPE HANDLING\\In these commands %d is replaced by the device name,\%n by the number, if within the command the count starts with 1,\%m by the number, if the count starts with 0.\\Command to set the file # on tape'
set comments[12]='\Command to skip to the next %n-th file on tape'
set comments[13]='\If there is a command to set the cartridge directly, supply it here'
set comments[14]='\If the cartridges can only by changed successively, insert the\command to change them here. This is also the command, that unloads\ the tape, if you have no cartridge handler.'
set comments[15]='\The command to erase the tape. This is necessary on some systems.'
set comments[16]='\\CONFIGURATION FOR NON-EXISTING CARTRIDGE HANDLING SYSTEM\\The user to inform, if a cartridge should be changed manually'
set comments[17]='\\The mail-program to use.\\Insert %u for the position, where the username has to appear.'
set comments[18]='\\FILES TO SAVE THE STATE AND LOGGINGS\\Filename to save the actual tape position'
set comments[19]='\Filename for error loggings etc.'
set comments[20]='\Locking file to prevent several server starts'
set comments[21]='\The file with the authentication encryption key'
set comments[22]='\\CONFIGURATION FOR REMOTE_EXECUTION\\Directory, where remotely started programs must reside'
set comments[23]='\\SERVER-STARTUP AND SHUTDOWN ACTIONS\\shell-command to perform, when the server starts'
set comments[24]='\\shell-command to perform, when the server exits'

set names[1]='Backup-Device'
set names[2]='Tape-Blocksize'
set names[3]='Cartridge-Handler'
set names[4]='Number Of Cartridges'
set names[5]='Last Cartridges'
set names[6]='Max Bytes Per File'
set names[7]='Cart-Insert-Gracetime'
set names[8]='Device Unavail Send mail after min'
set names[9]='Device Unavail give up after min'
set names[10]='Device-Probe Interval'
set names[11]='SetFile-Command'
set names[12]='SkipFiles-Command'
set names[13]='Set-Cart-Command'
set names[14]='Change-Cart-Command'
set names[15]='Erase-Tape-Command'
set names[16]='User To Inform'
set names[17]='Mail-Program'
set names[18]='Tape-Pos-File'
set names[19]='Logging-file'
set names[20]='Lock-file'
set names[21]='Encryption-Key-File'
set names[22]='Program-Directory'
set names[23]='Init-Command'
set names[24]='Exit-Command'

set patterns[1]='[Bb]ackup[-_ 	]*[Dd]evi?c?e?:?'
set patterns[2]='[Tt]ape[-_ 	]*[Bb]lock[-_ 	]*[Ss]ize:?'
set patterns[3]='[Cc]artr?i?d?g?e?[-_ 	]*[Hh]andler:?'
set patterns[4]='[Nn]umb?e?r?[-_ 	]*[Oo]f[-_ 	]*[Cc]artr?i?d?g?e?s[-_ 	]*:?'
set patterns[5]='[Ll]ast[-_ 	]*[Cc]artr?i?d?g?e?s[-_ 	]*:?'
set patterns[6]='[Mm]axi?m?u?m?[-_ 	]*[Bb]ytes[-_ 	]*[Pp]er[-_ 	]*[Ff]ile:?'
set patterns[7]='[Cc]artr?i?d?g?e?[-_ 	]*[Ii]nse?r?t?[-_ 	]*[Gg]race[-_ 	]*[Tt]ime:?'
set patterns[8]='[Dd]evi?c?e?[-_ 	]*[Uu]n[Aa]vaila?b?l?e?[-_ 	]*[Ss]end[-_ 	]*[Mm]ail[-_ 	]*([Aa]fter[-_ 	]*)?[Mm]inu?t?e?s?:?'
set patterns[9]='[Dd]evi?c?e?[-_ 	]*[Uu]n[Aa]vaila?b?l?e?[-_ 	]*[Gg]ive[-_ 	]*[Uu]p[-_ 	]*([Aa]fter[-_ 	]*)?[Mm]inu?t?e?s?:?'
set patterns[10]='[Dd]evi?c?e?[-_ \t]*[Pp]rob(e|ing)[-_ \t]*[Ii]nterval:?'
set patterns[11]='[Ss]et[-_ 	]*[Ff]ile[-_ 	]*[Cc]o?mm?a?n?d:?'
set patterns[12]='[Ss]kip[-_ 	]*[Ff]iles?[-_ 	]*[Cc]o?mm?a?n?d:?'
set patterns[13]='[Ss]et[-_ 	]*[Cc]artr?i?d?g?e?[-_ 	]*[Cc]o?mm?a?n?d:?'
set patterns[14]='[Cc]hange[-_ 	]*[Cc]artr?i?d?g?e?[-_ 	]*[Cc]o?mm?a?n?d:?'
set patterns[15]='[Ee]rase[-_ 	]*[Tt]ape[-_ 	]*[Cc]o?mm?a?n?d:?'
set patterns[16]='[Uu]ser[-_ 	]*[Tt]o[-_ 	]*[Ii]nfor?m?:?'
set patterns[17]='[Mm]ail[-_ 	]*[Pp]rogram:?'
set patterns[18]='[Tt]ape[-_ 	]*[Pp]osi?t?i?o?n?[-_ 	]*[Ff]ile:?'
set patterns[19]='[Ll]ogg?i?n?g?[-_ 	]*[Ff]ile:?'
set patterns[20]='[Ll]ocki?n?g?[-_ 	]*[Ff]ile:?'
set patterns[21]='([Ee]n)?[Cc]rypti?o?n?[-_ 	]*[Kk]ey[-_ 	]*[Ff]ile:?'
set patterns[22]='[Pp]rogram[-_ 	][Dd]ire?c?t?o?r?y?:?'
set patterns[23]='[iI]niti?a?l?i?z?a?t?i?o?n?[-_ 	]*[Cc]o?mm?a?n?d:?'
set patterns[24]='[eE]xit[-_ 	]*[Cc]o?mm?a?n?d:?'

set prompts[1]='Streamer device (no-rewind):   '
set prompts[2]='Device Blocksize:              '
set prompts[3]='Cartridge handler:             '
set prompts[4]='Number of cartridges:          '
set prompts[5]='Last cartridges of sets:       '
set prompts[6]='Max. number of bytes per file: '
set prompts[7]='Cartridge change gracetime:    '
set prompts[8]='Device unav. send mail after:  '
set prompts[9]='Device unavail. abort after:   '
set prompts[10]='Device probing interval:       '
set prompts[11]='Set-file-command:              '
set prompts[12]='Skip-files-command:            '
set prompts[13]='Set-cartridge-command:         '
set prompts[14]='Change-cartridge-command:      '
set prompts[15]='Erase-tape-command:            '
set prompts[16]='User to inform:                '
set prompts[17]='Mail program:                  '
set prompts[18]='Tape-position savefile:        '
set prompts[19]='Logging-file:                  '
set prompts[20]='Lock-file:                     '
set prompts[21]='Encryption-Key-File:           '
set prompts[22]='Program directory:             '
set prompts[23]='Init command:                  '
set prompts[24]='Exit command:                  '

set helps[1]="\\
This is the device, the backup is written to. It can be any\\
tape device with the capability to distinguish several files on\\
the media. It is mandatory to supply the no-rewind device here.\\
Otherwise this package won't work properly. This may also be the\\
path of a directory. Then the backup is written to files in this\\
directory. Suitable device names for some OS-es:\\
AIX:          /dev/rmt0.1\\
Solaris:      /dev/rmt/0bn\\
IRIX:         /dev/rmt/tps0d4nr\\
HP-UX:        /dev/rmt/0hn\\
Linux:        /dev/nst0\\
Digital UNIX: /dev/nrmt0h\\
Free BSD:     /dev/nrst0"
set helps[2]="\\
The blocksize of the tape device. This value specifies, how many\\
bytes are written to tape or read from it with one system call.\\
Usually this value is at least 512 or a multiple of it.\\
It is not very important, whether the blocksize is set to 2048\\
or 1024. The main thing to keep in mind is that if there is a\\
minimum, it should be respected "'(e.g. 1024 on AIX), otherwise\\
media space is wasted.'
set helps[3]='\\
This value must be 1 or 0, what means, that you either have a\\
cartridge handling system (i.e. some kind of robot) (1) or\\
not (0). If you don'"'"'t have a robot, you may nonetheless maintain\\
a set of cartridges, that you have to manually number. The backup\\
server side will inform you via email or console output, whenever\\
another cartridge has to be inserted into the drive and what number\\
it requires it is.'
set helps[4]='\\
This number specifies, how many cartridges you are maintaining.\\
If you have a cartridge handling system (some kind of robot),\\
this must be the number of cartridges, your system is juggling.'
set helps[5]='\\
Several cartridge sets can be used. Here they may be specified\\
supplying the last cartridge of each set separated by whitespace.\\
If this parameter is not given, there is only the default set\\
number 1 with all available cartridges. Not all cartridges need\\
to be used by the sets, but only the last ones can be omitted.\\
E. g. if 3 cartridge sets are needed (1-3, 4-8, 9-10), you may\\
supply here: 3 8 10.'
set helps[6]='\\
The stream of data, that represents your backup, is divided into\\
pieces (files on tape). This is done to find the files faster\\
during a restore. This value determines, how large the pieces on\\
tape may be in bytes. Some good values for a few tape technologies:\\
  DAT:              30000000\\
  Exabyte:          50000000\\
  DLT:             100000000'
if (`matches $OSNAME HPUX`) then
set helps[7]=="\\
This is the time in seconds, the program waits after another\\
cartridge has been put into the drive. Normal devices need a\\
certain time span to mount the tape to get it ready for use.\\
Normally this value is not critical. Some tried values for a\\
few tape technologies:\\
  DAT:         30\\
  Exabyte:     70\\
  DLT:         70\\
(Shortened help cause of HPUX-csh-limitations)"
else
set helps[7]="\\
This is the time in seconds, the program waits after another\\
cartridge has been put into the drive. Normal devices need a\\
certain time span to mount the tape to get it ready for use.\\
Normally this value is not critical. If you estimate it too\\
low, the ioctl-system-call will wait until the device becomes\\
available. This time is sometimes longer than two minutes,\\
so if you want to proceed quickly after a cartridge\\
change, you may measure the maximum time, your system needs.\\
Some tried values for a few tape technologies:\\
  DAT:         30\\
  Exabyte:     70\\
  DLT:         70"
endif
set helps[8]='\\
If the streaming device is not accessible (i.e. an open or a\\
tape handling command fails) or another backup server process\\
is still running, the server process re-tries his attempts\\
regularly. If it fails longer than the time in minutes\\
supplied here, an e-mail is sent to the configured user in\\
charge (see: User To Inform). Supplying 0 means: never send mail.'
set helps[9]='\\
If the streaming device is not accessible (i.e. an open or a\\
tape handling command fails), the server process re-tries his\\
attempts regularly. If it fails longer than the time in minutes\\
supplied here, it exits silently leaving a warning in the log file.\\
Supplying 0 means: try forever, never exit.'
set helps[10]='\\
This is the interval in seconds, after that regularly the device\\
is probed to be ready for reading. Thus after having ejected a\\
cartridge it is automatically recognized, if a new cartridge has\\
been inserted. For other media (e.g. exchangeable disks) this may\\
not be suitable. Supply a 0 in these cases for no probing.'
set helps[11]='\\
This is the (shell-) command to run to position the tape to a\\
certain file. Usually this is something like a combination\\
of: mt -f <device> rewind  and  mt -f <device> fsf <number>.\\
If the command you are supplying here starts to count with\\
1 for the first file on tape, you should insert %n for the\\
<number>. If it starts with 0, replace <number> with %m. If\\
you don'"'"'t want to type the devicename again here, you may\\
write %d instead.'
set helps[12]='\\
This is the (shell-) command to run to skip over to a file\\
later on tape. Usually this is something like\\
mt -f <device> fsf <number>\\
Insert %n, where the number of files to skip over must be\\
supplied in the command, in the example instead of <number>,\\
and %d, where the device should appear (here: <device>).'
if (`matches $OSNAME HPUX`) then
set helps[13]='\\
This is the (shell-) command to run to put a certain\\
cartridge into the device (if you have a command to do\\
this). If the command you are supplying here starts\\
to count with 1 for the first cartridge, insert %n\\
in the appropriate place. If it starts with 0, replace\\
it with %m. You may write %d instead of the devicename.'
else
set helps[13]='\\
This is the (shell-) command to run to put a certain\\
cartridge into the device. If the command you are supplying\\
here starts to count with 1 for the first cartridge, you\\
should insert %n in the place, where the cartridge number\\
must appear. If it starts with 0, replace it with %m. If\\
you don'"'"'t want to type the devicename again here, you may\\
write %d instead. If you don'"'"'t have a command to perform\\
this task, don'"'"'t supply anything here (type a space\\
character and hit enter). In this case you must set your\\
cartridge handling system to sequential mode (automatically\\
putting the next cartridge in, when the actual one is ejected).'
endif
if (`matches $OSNAME HPUX`) then
set helps[14]='\\
The (shell-) command to run to eject a cartridge actually\\
placed inside the streamer device. This is normally\\
something like  mt -f <device> rewoffl  (but better consult\\
your man-pages). You have to supply this either if you have\\
no cartridge handling system (robot) or if you have no\\
command to set the cartridge directly by number.'
else
set helps[14]='\\
This is the (shell-) command to run to eject a cartridge\\
actually placed inside the streamer device. This is normally\\
something like  mt -f <device> rewoffl  (but better consult\\
your man-pages). You have to supply this either if you have\\
no cartridge handling system (robot) or if you have no\\
command to set the cartridge directly by number. In the latter\\
case this package tries to maintain the number of the actual\\
cartridge in a file and to (hopefully) keep it consistent\\
with the reality. In this case the cartridge handling system\\
must be configured to sequential mode (automatically putting\\
the next cartridge in, when the actual one is ejected).'
endif
set helps[15]='\\
The (shell-) command to run, if the tape must be erased.\\
(actually not needed).'
set helps[16]="\\
If you don't have a cartridge handling system "'(robot), a\\
human maintainer must put the appropriate cartridge into the\\
tape device. If you supply a mail program, an e-mail is sent\\
to the user given here, which informs him, that and which\\
cartridge (by number) must be put into the tape device.\\
If a timespan is configured, after that an automatic e-mail\\
should be sent due to an unaccessible tape device, it is\\
directed to this user.'
set helps[17]="\\
The mail program used to send messages to a human maintainer.\\
This is done, whenever another cartridge must be put into the\\
tape device and it can'"'t be done automatically (by a robot or\\
whatever). If you don'"'"'t want to type the username again here,\\
you can instead write %u . If you don'"'"'t want mails to be sent,\\
you may instead supply any other command, that reads the standard\\
input and does something reasonable with it, e.g. redirects it\\
to the console:  cat > /dev/console'
set helps[18]="\\
In this file some values are stored, e.g. the number of the\\
cartridge actually placed inside the streamer device."
set helps[19]="\\
Logging information concerning errors or other notable events\\
is redirected to this file."
set helps[20]="\\
To prevent the server program from being started several times\\
a lock file is created and this is it's name."
set helps[21]="\\
The file containing the encryption key for authenticating\\
the backup client to the server. This file must contain\\
at least 5 characters and must not have read permission for\\
group or world."
set helps[22]='\\
If you are using the remote start option for backing up\\
clients, this is the directory, where programs must reside,\\
that can be started remotely. No other programs can be\\
started remotely (for security reasons).'
set helps[23]='\\
Here you may supply a (shell-) command to be run, when the\\
backup server side wakes up, i.e. the server process starts.\\
A %p appearing in this command is replaced with the name\\
of the client, that connected the backup service.'
set helps[24]='\\
Here you may supply a (shell-) command to be run, when the\\
backup server side goes to sleep, i.e. the server process ends.\\
A %p appearing in this command is replaced with the name\\
of the client, that connected the backup service.'

@ var_param_idx=$num_fix_params + 2

echo -n "Reading actual settings "
set i=1
while ($i <= $num_fix_params)
  set values[$i]="`getparam '$patterns[$i]'`"
  echo -n .
  @ i++
end
gosub set_var_params "" cont1
cont1:
if ($num_var_params > 1) then
  set j=1
  while ($j <= $num_var_params)
    set parname="`echo '$patterns[$var_param_idx]'|sed 's/#num#/'$j/g`"
    set values[$i]="`getparam '$parname'`"
    @ j++
    @ i++
    echo -n .
  end
else
  set values[$first_var_param]="`getparam '$names[$first_var_param]'`"
  echo -n .
endif

echo " done"

menu:
gosub set_var_params "" cont2
cont2:
clear

set tmpfile=`tempfile || exit 1`
#/bin/rm -f $tmpfile
onintr cleantmp
#touch $tmpfile

set i=1
echo ' '
while ($i <= $num_fix_params)
  echo '   ['$i']	'"$prompts[$i]""$values[$i]" >> $tmpfile
  @ i++
end
set j=1
if ($num_var_params > 1) then
  while ($i <= $num_total_params)
    echo -n '   ['$i']	' >> $tmpfile
    set text="`echo '$prompts[$var_param_idx]' | sed 's/#num#/'$j/g`"
    echo "$text$values[$i]" >> $tmpfile
    @ i++
    @ j++
  end
else
  if ($num_var_params > 0) then
    echo '   ['$first_var_param']	'"$prompts[$first_var_param]""$values[$first_var_param]" >> $tmpfile
  endif
endif

(echo " " ;\
echo 'Please enter the number of the parameter you would like to' ;\
echo 'modify. Then, if you want to clear the entry, hit the Space- and' ;\
echo 'the Return-key. To get help enter: help, and the number of the' ;\
echo 'parameter you need help on. To exit and write out the changes' ;\
echo 'enter: ok. To exit without making any changes, enter: exit.' ) >> $tmpfile
more $tmpfile

/bin/rm -f $tmpfile
onintr

read_choice:
set c=blub
while (! `matches "$c" '^([1-9][0-9]*|help *[1-9][0-9]*|exit|quit|ok)$'`)
  echo " "
  echo -n "Your choice: "
  set c="$<"
  if (! `matches "$c" '^([1-9][0-9]*|help *[1-9][0-9]*|exit|quit|ok)$'`) then
    echo " "
    echo "Invalid choice. Please try again."
    goto read_choice
  endif
  if (`matches "$c" '^[1-9][0-9]*'`) then
    if ($c > $num_total_params || $c < 1) then
      echo " "
      echo "Invalid choice. Please try again."
      goto read_choice
    endif
  endif
end

if (`matches "$c" '^[1-9]'`) then
  echo ' '
  if ($c > $num_fix_params) then
    @ i=$num_total_params - $num_fix_params
    if ($num_var_params > 1) then
      echo "$prompts[$var_param_idx]"|sed 's/#num#/'"$c"'/g'
    else
      if ($num_var_params > 0) then
        echo "$prompts[$first_var_param]"
      endif
    endif
  else
    echo "$prompts[$c]"
  endif
  echo -n "Please enter new value: "
  set i="$<"
  if ("$i" != "") then
    set values[$c]="$i"
  endif
  goto menu
endif

if (`matches "$c" quit` || `matches "$c" exit`) exit 0

if (`matches "$c" ok`) then
  \rm -f $configfile
  touch $configfile
  set i=1
  while ($i <= $num_total_params)
    if ($i <= $num_fix_params) then
      echo "$comments[$i]" | awk '{ a=$0; while((i=index(a,"\\"))>0){printf "# %s\n",substr(a,1,i-1);a=substr(a,i+1)} printf "# %s\n",a}' >> $configfile
      if (`echo "$values[$i]"|wc -w` == 0) then
	echo "#$names[$i]":"		$values[$i]" >> $configfile
      else
	echo "$names[$i]":"		$values[$i]" >> $configfile
      endif
    else
      @ j=$i - $num_fix_params
      if ($num_var_params > 1) then
        echo "$comments[$var_param_idx]" | sed 's/#num#/'$j/g | awk '{ a=$0; while((i=index(a,"\\"))>0){printf "# %s\n",substr(a,1,i-1);a=substr(a,i+1)} printf "# %s\n",a}' >> $configfile
      else
	if ($num_var_params > 0) then
          echo "$comments[$first_var_param]" | awk '{ a=$0; while((i=index(a,"\\"))>0){printf "# %s\n",substr(a,1,i-1);a=substr(a,i+1)} printf "# %s\n",a}' >> $configfile
	endif
      endif
      if (`echo "$values[$i]"|wc -w` == 0) then
	echo "#$names[$var_param_idx]":"		$values[$i]" | \
				sed 's/#num#/'"$j"'/g' >> $configfile
      else
	echo "$names[$var_param_idx]":"		$values[$i]" | \
				sed 's/#num#/'"$j"'/g' >> $configfile
      endif
    endif
    @ i++
  end
  exit 0
endif

if (`matches "$c" help`) then
  set num=`echo "$c"|cut -c5-`
  if ($num <= $num_fix_params) then
    echo $helps[$num] | awk '{ a=$0; while((i=index(a,"\\"))>0){printf "%s\n",substr(a,1,i-1);a=substr(a,i+1)} printf "%s\n",a}'
  else
    if ($num_var_params > 1) then
      @ j=$num - $num_fix_params
      echo $helps[$var_param_idx] | sed 's/#num#/'$j/g | awk '{ a=$0; while((i=index(a,"\\"))>0){printf "%s\n",substr(a,1,i-1);a=substr(a,i+1)} printf "%s\n",a}'
    else
      if ($num_var_params > 0) then
        echo $helps[$first_var_param] | awk '{ a=$0; while((i=index(a,"\\"))>0){printf "%s\n",substr(a,1,i-1);a=substr(a,i+1)} printf "%s\n",a}' | sed 's/#num#/'"$c"'/g'
      endif
    endif
  endif
  echo " "
  echo "Hit Return, when done."
  set d=$<
endif

goto menu


set_var_params:

if ($num_var_sp_idx > 0) then
  set num_var_params=$values[$num_var_sp_idx]
  if ("$num_var_params" == "") then
    set num_var_params=1
  endif
else
  set num_var_params=0
endif

@ num_total_params=$num_fix_params + $num_var_params
@ first_var_param=$num_fix_params + 1
if ($num_var_params > 1) then
  set max_num_vals=$#values
  if ($#values < $num_total_params) then
    set max_num_vals=$num_total_params
  endif
  set dummy=""
  set _i=1
  while ($_i <= $max_num_vals)
    set dummy=(1 $dummy)
    @ _i++
  end
  set dummy2=($dummy)
  set _i=1
  while ($_i <= $max_num_vals)
    set dummy[$_i]=""
    @ _i++
  end
  set _i=1
  while ($_i <= $#values)
    set dummy[$_i]="$values[$_i]"
    @ _i++
  end
  set values=($dummy2)
  set _i=1
  while ($_i <= $max_num_vals)
    set values[$_i]="$dummy[$_i]"
    @ _i++
  end
endif

return

cleantmp:
/bin/rm -f $tmpfile
onintr
exit 2
