#! /bin/csh -f

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

unalias cd
unalias cp
unalias mv

set servicename="afbackup"

if (-r Install.cache) then
  set noglob
  source Install.cache
  unset noglob
endif

set varstosave=(servinstdir username userid groupid defdev servvarinstdir clntinstdir clntvarinstdir port use_des des_header des_include des_lib des_libdir orduserrestore runclientconfig runserverconfig servicename activate_mserver mservicename mport add_mservice use_zlib zlib_include zlib_libdir)

set exitst=0

set pathext=(/usr/bin /bin /usr/ccs/bin /usr/ccs/lib /opt/gnu/bin /usr/local/bin /usr/local/gnu/bin /opt/bin /opt/gnu)

foreach newdir ($pathext)
  if (! -d $newdir) continue

  set found=0
  foreach dir ($path)
    if ($dir == $newdir) then
      set found=1
      break
    endif
  end
  if (! $found) then
    set path=($path $newdir)
  endif
end

rehash

set stack=()
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'
alias issymlink 'ls -l \!:1 |grep '^l'|wc -l'
alias do_exit 'goto exitlabel'
alias find_program 'sh -c '"'"'if [ `echo \!:1|grep /|wc -l` -gt 0 ] ; then echo \!:1 ; else P=`basename \!:1` ; for d in `echo $PATH|tr : " "` ; do if [ -x $d/$P ] ; then echo $d/$P ; break ; fi ; done ; fi'"'"

#foreach p ($path)
#  if (-x $p/cc) then
#    setenv CC cc
#    break
#  endif
#  if (-x $p/gcc) then
#    setenv CC gcc
#    break
#  endif
#end

set CONF_INTERPR=""
if (`matches $OSNAME HPUX`) set CONF_INTERPR=ksh

set adminprefix=/etc

set tmpfile=core.tmp.$$
set tmpfile2=core.tmp2.$$

set saved_files=""

set modes=(servonly clntonly clntrem all)

gosub which_make "" know_make
know_make:
set MAKEOPTS="EXTRA_CFLAGS=-DORIG_DEFAULTS"
set MAKECMD="make"
if ($?GNU_MAKE) then
  set MAKEOPTS="$MAKEOPTS MAKECMD=$GNU_MAKE"
  set MAKECMD=$GNU_MAKE
endif

set configure_options=""

clear
echo " "
echo 'Welcome to the installation procedure for'
echo " "
echo "AF's backup system *"
echo '=================='
echo " "
echo ' '
echo '(* People accustomed to Microsoft-publications please'
echo '   insert: hyper-optimized, ultra-reliable, turbo-speed,'
echo '   multimedia, leading technology, highest performance ...)'
echo ' '
echo 'You can interrupt this program any time hitting Ctrl-C. With some'
echo 'of the following questions a default answer will be offered in'
echo "square brackets. If you don't want to change this or don't know"
echo "how to set the value of the parameter, just hit the Return key."
echo " "
echo "First you have to decide, what functionality you"
echo 'would like to have on this machine.'
echo " "
echo '(1) Backup server only'
echo '(2) Backup client only (also to start backups on other hosts)'
echo '(3) Backup client only with remote start option'
echo '(4) Backup server, client and client remote start option'
echo " "

set sel=0
while (! `matches $sel '^[1-4]$'`)
  echo -n 'Please enter your selection [1-4]: '
  set sel=$<
  if (! `matches $sel '^[1-4]$'`) then
    echo "This is no valid selection. Please try again."
    echo " "
  endif
end

set mode=$modes[$sel]

onintr exitlabel

switch ($sel)
  case 1:
		gosub server_only "" contlab
		breaksw
  case 2:
		gosub client_only "" contlab
		breaksw
  case 3:
		gosub remclient "" contlab
		breaksw
  case 4:
		gosub all "" contlab
		breaksw
endsw

contlab:
echo " "
echo 'All done. The input supplied during this installation procedure'
echo 'is saved in the file "Install.cache". This file can be copied'
echo 'somewhere else to have the input present as default for the next'
echo 'upgrade or installation on other architectures. The file will be'
echo 'erased typing "make distclean".'

if (`echo $saved_files | wc -w` > 0) then
  echo " "
  echo "The following files are safety copies of files, that"
  echo "are crucial for your system. If up to now everything"
  echo "worked fine, you can safely remove them:"
  echo " "
  echo $saved_files
  echo ' '
endif

exitlabel:

set noglob
/bin/rm -f Install.cache
touch Install.cache
foreach var ($varstosave)
  eval 'set exists=$?'"$var"
  if ($exists) then
    set valuestr=`eval echo '$'$var`
    echo set $var"=($valuestr)" >> Install.cache
  endif
end

exit $exitst

### SERVER ONLY ###

server_only:
echo " "
echo "Ok, we're gonna install only the server side on this"
echo -n "machine. "

gosub server_install "" end_server_only

end_server_only:
return

### CLIENT ONLY ###

client_only:
echo " "
echo "Ok, we're gonna install only the client side on this"
echo -n "machine. "

gosub client_install "" end_client_only

end_client_only:
return

### REMOTELY STARTED CLIENT ###

remclient:
echo " "
echo "Ok, we're gonna install the client with remote start"
echo -n "option. "

gosub server_install "" end_remclient
end_remclient:
return

### CLIENT, SERVER AND REMOTE START OPTION ###

all:
echo " "
echo "Ok, we will install the client, server and remote start"
echo -n "option. "

gosub server_install "" end_allinst
end_allinst:
return

################ SERVER INSTALLATION #################

server_install:

if (! $?servinstdir) then
  set servinstdir=/usr/backup
endif

echo "First we have to specify, where to install the"
echo "files and programs. Please enter a directory of"
echo 'your choice (you may supply the same directory'
echo 'like for the client side).'
ask_for_servinstdir:
echo 'Is '$servinstdir' ok ? If yes, enter yes.'
echo 'Otherwise, please enter an alternate directory.'

choose_servinstdir:
echo " "
set choice="blub"
while (! `matches $choice '^(ye?s?|no?|/.*)$'`)
  echo -n 'Your choice (Yes/No/path) [Yes]: '
  set choice=$<
  if ("$choice" == "") set choice="yes"
  if (! `matches $choice '^(ye?s?|no?|/.*)$'`) then
    echo " "
    echo "This is no valid choice. Please Try again."
  endif
end
if (`matches $choice '^no?$'`) then
  echo "Please enter the directory."
  goto choose_servinstdir
endif
if (`matches $choice '^/'`) then
  set servinstdir=$choice
endif

gosub choose_DES "" chosen_DES_servonly
chosen_DES_servonly:
echo " "

gosub choose_ZLIB "" chosen_ZLIB_servonly
chosen_ZLIB_servonly:
echo " "

echo " "
echo "Now we have to build the programs. Please stand by"
echo "and watch for error messages. If any occur, you have"
echo "a problem. Warnings can usually be ignored. If errors"
echo 'occur, drop a mail to the author (af@muc.de)'
echo 'with the fault message(s) of make or try to patch the'
echo "stuff yourself. If you succeed, please also drop a"
echo "mail to the author, so your changes can be added to the"
echo "distribution."
echo " "
echo "Running configure now ..."
echo " "
$CONF_INTERPR ./configure -prefix="$servinstdir" --without-prefixext \
		--with-rexecdir="$servinstdir"/server/rexec \
		$configure_options
set s=$status
echo " "
if ($s != 0) then
  echo "Damn. Something went wrong. Please contact the author."
  echo "Stopping here."
  set exitst=1
  do_exit
endif

echo "Running make now ..."
echo " "
if ($mode == "all" || $mode == "clntrem") then
  $MAKECMD $MAKEOPTS
  set s=$status
else
  $MAKECMD server $MAKEOPTS
  set s=$status
endif

echo " "
if ($s != 0) then
  echo "Damn. Something went wrong. Please contact the author."
  echo "Stopping here."
  set exitst=1
  do_exit
endif

echo " "
echo "We're now going to install the files and programs."
echo " "
if ($mode == "all" || $mode == "clntrem") then
  $MAKECMD install install.rclient $MAKEOPTS
  set s=$status
else
  $MAKECMD install.server $MAKEOPTS
  set s=$status
endif

echo " "
if ($s != 0) then
  echo "Oops. This failed. Maybe your directory choice is"
  echo "somewhat problematic. Choose one of:"
  set c=b
  while (! `matches $c '^[ar]'`)
    echo -n 'Abort, Retry (A/R) [R]: '
    set c=$<
    if ("$c" == "" ) set c=r
  end
  if ($c == "a" || $c == "A") then
    set exitst=1
    do_exit
  else
    echo ' '
    goto ask_for_servinstdir
  endif
endif

\rm -f $tmpfile

gosub add_backup_service "servicename port" serv_serv_added

serv_serv_added:

if (! $?username) then
  if ($mode == "all" || $mode == "clntrem") then
    set username=root
  else
    set username=backup
  endif
endif

echo ' '

gosub add_inetd_entry "$servicename nowait afserver" inst_mserver

inst_mserver:
echo ' '

echo 'Do you want to activate the multi stream server ?'
if (! $?activate_mserver) then
  set activate_mserver=Yes
endif
if (! $?mservicename) then
  set mservicename=afmbackup
endif
gosub yes_no $activate_mserver activate_mserver

activate_mserver:
set activate_mserver=$yesno
if ($yesno == "no") then
  goto sel_buserver_usrgrp
endif

echo ' '
echo 'The multi-stream server should use a different port than the'
echo 'normal server, so we now configure the appropriate entries'
echo 'for the system files.'

gosub add_backup_service "mservicename mport" serv_mserv_added

serv_mserv_added:

echo ' '
gosub add_inetd_entry "$mservicename wait afmserver" sel_buserver_usrgrp

sel_buserver_usrgrp:

echo ' '

gosub add_user_group "username userid groupid" chowntapedev

chowntapedev:

chown -R $username $servinstdir

if ($mode != "clntrem") then
  echo " "
  echo "Now we should change the owner of your tape device"
  echo "to user $username and give him exclusive access to it."
  echo "You may now enter a filename-pattern or no, if you"
  echo "want to skip this step. Here are the usual streamer"
  echo "device name patterns for some OS-es:"
  echo " "
  echo "Solaris:       /dev/rmt/[0-9]*"
  echo "AIX:           /dev/rmt[0-9]*"
  echo "IRIX:          /dev/rmt/tps[0-9]*"
  echo "HP-UX:         /dev/rmt/[0-9]*"
  echo "Linux:         /dev/st[0-9]* /dev/nst[0-9]*"
  echo "Digital UNIX:  /dev/rmt[0-9]* /dev/nrmt[0-9]*"
  echo "Free BSD:      /dev/rst[0-9]* /dev/nrst[0-9]*"

  if (! $?defdev) then
    switch (`uname -s`)
      case SunOS:
		set defdev="/dev/rmt/0*"
		breaksw
      case AIX:
		set defdev="/dev/rmt0*"
		breaksw
       case IRIX:
		set defdev="/dev/rmt/tps0d4*"
		breaksw
      case HP-UX:
		set defdev="/dev/rmt/0*"
		breaksw
      case Linux:
		set defdev="/dev/st0* /dev/nst0*"
		breaksw
      case OSF1:
		set defdev="/dev/rmt0* /dev/nrmt0*"
		breaksw
      case FreeBSD:
		set defdev="/dev/rst0* /dev/nrst0*"
		breaksw
      default:
		set defdev=""
    endsw
  endif

 seldev:
  echo " "
  echo -n "Your choice [$defdev]: "
  set c=""
  set c=$<
  if ("$c" == "") set c="$defdev"
  if (! `matches "$c" '^no?$'`) then
    chown $username $c
    set r=$status
    chmod 600 $c
    set r2=$status
    if ($r || $r2) then
      echo "Oops, this did not work. Please do it manually entering:"
      echo '(replace <devicenames> with your device-names pattern)'
      echo " "
      echo " chown $username <devicenames>"
      echo " chmod 600 <devicenames>"
      echo " "
      echo "Please hit return, when you are done with it or"
      echo "want to skip this step."
      set c=$<
    endif
  endif
  set defdev="$c"
endif


set PS1="ps -ef"
set PS2="ps -fp"

if (`matches $OSNAME linux` || `matches $OSNAME FreeBSD`) then
  set PS1="ps -uxa"
  set PS2="ps"
endif
if (`matches $OSNAME sunos`) then
  set REV=`uname -r|sed 's/[.].*$//g'`
  if ($REV < 5) then
    set PS1="ps -uxa"
    set PS2="ps"
  endif
endif

echo ' '

set inetdline=("`$PS1|grep -v grep|grep inetd`")
set running_inetds=`$PS1|grep -v grep|grep inetd|wc -l`
if ($running_inetds == 0) then
  echo "Sorry, i'm unable to figure out, what the process ID of the"
  echo 'inetd is like. Would you do me a favour and enter it here ?'
  askforinetdpid:
  echo ' '
  echo -n 'Process ID: '
  set pid=""
  set pid=$<
  if ("$pid" == "" || ! `matches $pid '^[0-9][0-9]*$'`) then
    echo ' '
    echo "Invalid input. Please try again."
    goto askforinetdpid
  endif
  $PS2 $pid >& /dev/null
  if ($status) then
    echo ' '
    echo "This is not the pid of the inetd. Please try again."
    goto askforinetdpid
  endif
  set inetdline=("`$PS2 $pid|grep -v grep|grep inetd`")
endif
if ($running_inetds > 1) then
  echo "Sorry, I found several processes looking like inetd."
  echo "Would you please give me a hint and tell me the real"
  echo 'process ID ? You may evaluate the following lines:'
  echo ' '
  $PS1|grep -v grep|grep inetd
  askfipid2:
  echo ' '
  echo -n 'Process ID: '
  set pid=""
  set pid=$<
  if ("$pid" == "" || ! `matches $pid '^[0-9][0-9]*$'`) then
    echo ' '
    echo "Invalid input. Please try again."
    goto askfipid2
  endif
  $PS2 $pid >& /dev/null
  if ($status) then
    echo ' '
    echo "This is not the pid of the inetd. Please try again."
    goto askfipid2
  endif
  set inetdline=("`$PS2 $pid|grep -v grep|grep inetd`")
endif
set inetdline=(`echo "$inetdline"|sed 's/[?*\[]/_/g;s/\]/_/g'`)
if ($running_inetds == 1) then
  set pid=$inetdline[2]
endif

echo ' '
echo "The process ID of the inetd seems to be $pid, the"
echo "appropriate entry in the process list is:"
echo ' '
echo "$inetdline"
asksendhup:
echo ' '
echo "To activate the backup service we can now send a HANGUP signal"
echo -n 'to the inetd. Do you want to do this (Yes/No) [Yes] ? '
set c=blub
while (! `matches $c '^(ye?s?|no?)$'`)
  set c=$<
  if ("$c" == "") set c=Yes
  if (! `matches $c '^(ye?s?|no?)$'`) then
    echo ' '
    echo "Invalid choice. Please try again."
    goto asksendhup
  endif
end
if (`matches $c '^ye?s?$'`) then
  sh -c "kill -1 $pid"
endif

if (! $?servvarinstdir) then
  set servvarinstdir=/var/logs/backup
endif
echo " "
echo "You might now want to move the variable part of the"
if ($mode == "all" || $mode == "clntrem") then
  echo -n "backup service"
else
  echo -n "server side"
endif
echo " installation to the /var-directory. The"
echo "default directory is $servvarinstdir. If you want to do"
echo "this and this directory is ok, just enter yes. If"
echo 'the directory is not ok, enter the desired one (you'
echo 'may supply the same as for the client side). If you'
echo "want to skip this step, just enter no."
mvservvar:
echo ' '
echo -n 'Your choice (Yes/No/path) [No]: '
set c=blub
while (! `matches $c '^(ye?s?|no?|/.*)$'`)
  set c=$<
  if ("$c" == "") set c=no
  if (! `matches $c '^(ye?s?|no?|/.*)$'`) then
    echo ' '
    echo "Invalid choice. Please try again."
    goto mvservvar
  endif
end
if (`matches $c '^/'`) then
  set servvarinstdir=$c
endif
if (! `matches $c '^no?$'`) then
  mkdir -p $servvarinstdir
  set curwd=`pwd`
  cd $servinstdir/server
  if (`issymlink var`) then
    echo "Warning: The directory seems to be moved already."
    echo "         Please check, where $servinstdir/server/var"
    echo "         points to. Not moving anything."
    goto eosvmove
  endif
  tar cf - var | (cd $servvarinstdir; tar xf -)
  mv $servvarinstdir/var $servvarinstdir/server
  \rm -rf var
  ln -s $servvarinstdir/server var
  eosvmove:

  if ($mode == "all" || $mode == "clntrem") then
    cd $servinstdir/client
    if (`issymlink var`) then
      echo "Warning: The directory seems to be moved already."
      echo "         Please check, where $servinstdir/client/var"
      echo "         points to. Not moving anything."
      goto eoclsvmove
    endif
    tar cf - var | (cd $servvarinstdir; tar xf -)
    mv $servvarinstdir/var $servvarinstdir/client
    \rm -rf var
    ln -s $servvarinstdir/client var
    eoclsvmove:
  endif

  cd $curwd
endif

if ($mode == "all" || $mode == "servonly") then
  if (! $?runserverconfig) then
    set runserverconfig=Yes
  endif
  echo ' '
  echo "Now we may run the program $servinstdir/server/bin/serverconfig,"
  echo "so you can configure the server side of the backup system. Would"
  echo 'you like to do this now ?'
  confserv:
  echo ' '
  echo -n "Your choice (Yes/No) [$runserverconfig]: "
  set c=blub
  while (! `matches $c '^(ye?s?|no?)$'`)
    set c=$<
    if ("$c" == "") set c=$runserverconfig
    if (! `matches $c '^(ye?s?|no?)$'`) then
      echo ' '
      echo "Invalid choice. Please try again."
      goto serv
    endif
  end
  set runserverconfig="$c"
  if (`matches $c 'ye?s?'`) then
    $servinstdir/server/bin/serverconfig
  endif
endif

if ($mode != "clntrem" && $mode != "servonly") then
  if (! $?orduserrestore) then
    set orduserrestore=No
  endif
  echo " "
  echo "Do you want ordinary users to be able to restore their"
  echo "own files and/or directories without administrator help ?"
  ordusersclnt:
  echo ' '
  echo -n "Your choice (Yes/No) [$orduserrestore]: "
  set c=blub
  while (! `matches $c '^(ye?s?|no?)$'`)
    set c=$<
    if ("$c" == "") set c=$orduserrestore
    while (! `matches $c '^(ye?s?|no?)$'`)
      echo ' '
      echo "Invalid choice. Please try again."
      goto ordusersclnt
    end
  end
  set orduserrestore=$c
  if (`matches $c 'ye?s?'`) then
    /bin/rm -f $servinstdir/client/bin/afrestore \
		$servinstdir/client/bin/afbackout \
		$servinstdir/client/bin/backout \
		$servinstdir/client/bin/restore
    cp $servinstdir/client/bin/full_backup \
		$servinstdir/client/bin/afrestore
    ln $servinstdir/client/bin/afrestore $servinstdir/client/bin/afbackout
    ln -s afbackout $servinstdir/client/bin/backout
    ln -s afrestore $servinstdir/client/bin/restore
    chmod 4755 $servinstdir/client/bin/afrestore
    chmod 755 $servinstdir/client/bin/xafrestore $servinstdir/client/bin/__packpats
  endif
endif

if ($mode == "all" || $mode == "clntrem") then
  if (! $?runclientconfig) then
    set runclientconfig=Yes
  endif
  echo ' '
  echo "Now we may run the program $servinstdir/client/bin/clientconfig,"
  echo "so you can configure the client side of the backup system. Would"
  echo 'you like to do this now ?'
  confsclnt:
  echo ' '
  echo -n "Your choice (Yes/No) [$runclientconfig]: "
  set c=blub
  while (! `matches $c '^(ye?s?|no?)$'`)
    set c=$<
    if ("$c" == "") set c=$runclientconfig
    if (! `matches $c '^(ye?s?|no?)$'`) then
      echo ' '
      echo "Invalid choice. Please try again."
      goto confsclnt
    endif
  end
  set runclientconfig="$c"
  if (`matches $c 'ye?s?'`) then
    $servinstdir/client/bin/clientconfig
  endif
endif

return


################## CLIENT INSTALLATION ##################

client_install:
if (! $?clntinstdir) then
  set clntinstdir=/usr/backup
endif

echo "First we have to specify, where to install the"
echo "files and programs. Please enter a directory of"
echo 'your choice (you may supply the same directory'
echo 'like for the server side).'
ask_for_clntinstdir:
echo 'Is '$clntinstdir' ok ? If yes, enter yes.'
echo 'Otherwise, please enter an alternate directory.'

choose_clntinstdir:
echo " "
set choice="blub"
while (! `matches $choice '^(ye?s?|no?|/.*)$'`)
  echo -n 'Your choice (Yes/No/path) [Yes]: '
  set choice=$<
  if ("$choice" == "") set choice="yes"
  if (! `matches $choice '^(ye?s?|no?|/.*)$'`) then
    echo " "
    echo "This is no valid choice. Please Try again."
  endif
end
if (`matches $choice '^no?$'`) then
  echo "Please enter the directory."
  goto choose_clntinstdir
endif
if (`matches $choice '^/'`) then
  set clntinstdir=$choice
endif

gosub choose_DES "" chosen_DES_clnt
chosen_DES_clnt:
echo " "

gosub choose_ZLIB "" chosen_ZLIB_clnt
chosen_ZLIB_clnt:
echo " "

echo " "
echo "Now we have to build the programs. Please stand by"
echo "and watch for error messages. If any occur, you have"
echo "a problem. Warnings can usually be ignored. If errors"
echo "occur, drop a mail to the author"
echo '(af@muc.de)'
echo 'with the fault message(s) of make or try to patch the'
echo "stuff yourself. If you succeed, please also drop a"
echo "mail to the author, so your changes ca be added to the"
echo "distribution."
echo " "
echo "Running configure now ..."
echo " "
$CONF_INTERPR ./configure -prefix="$clntinstdir" --without-prefixext \
		--with-rexecdir="$clntinstdir"/server/rexec \
		$configure_options
set s=$status
echo " "
if ($s != 0) then
  echo "Damn. Something went wrong. Please contact the author."
  echo "Stopping here."
  set exitst=1
  do_exit
endif

echo "Running make now ..."
echo " "
$MAKECMD client $MAKEOPTS
set s=$status
echo " "
if ($s != 0) then
  echo "Damn. Something went wrong. Please contact the author."
  echo "Stopping here."
  set exitst=1
  do_exit
endif

echo " "
echo "We're now going to install the files and programs."
echo " "
$MAKECMD install.client $MAKEOPTS
set s=$status

echo " "
if ($s != 0) then
  echo "Oops. This failed. Maybe your directory choice is"
  echo "somewhat problematic. Choose one of:"
  set c=b
  while (! `matches $c '^[ar]'`)
    echo -n 'Abort, Retry (A/R) [R]: '
    set c=$<
    if ("$c" == "" ) set c=r
  end
  if ($c == "a" || $c == "A") then
    return
  else
    echo ' '
    goto ask_for_clntinstdir
  endif
endif

gosub add_backup_service "servicename port" clnt_add_mservice

clnt_add_mservice:

echo ' '
echo 'Do you want to add an entry to '"$adminprefix/services"
echo 'for the multi stream server ?'

if (! $?add_mservice) then
  set add_mservice=Yes
endif
if (! $?mservicename) then
  set mservicename=afmbackup
endif

gosub yes_no $add_mservice clnt_mservice_added
clnt_mservice_added:
set add_mservice=$yesno
if ($yesno == "no") then
  goto clnt_chvardir
endif

gosub add_backup_service "mservicename mport" clnt_chvardir

clnt_chvardir:

if (! $?clntvarinstdir) then
  set clntvarinstdir=/var/logs/backup
endif
echo " "
echo "You might now want to move the variable part of the"
echo "client side installation to the /var-directory. The"
echo "default directory is $clntvarinstdir. If you want to do"
echo "this and this directory is ok, just enter yes. If"
echo 'the directory is not ok, enter the desired one (you'
echo 'may supply the same as for the server side). If you'
echo "want to skip this step, just enter no."
mvclntvar:
echo ' '
echo -n 'Your choice (Yes/No/path) [No]: '
set c=blub
while (! `matches $c '^(ye?s?|no?|/.*)$'`)
  set c=$<
  if ("$c" == "") set c=no
  if (! `matches $c '^(ye?s?|no?|/.*)$'`) then
    echo ' '
    echo "Invalid choice. Please try again."
    goto mvclntvar
  endif
end
if (`matches $c '^/'`) then
  set clntvarinstdir=$c
endif
if (! `matches $c '^no?$'`) then
  mkdir -p $clntvarinstdir
  set curwd=`pwd`
  cd $clntinstdir/client
  if (`issymlink var`) then
    echo " "
    echo "Warning: The directory seems to be moved already."
    echo "         Please check, where $clntinstdir/client/var"
    echo "         points to. Not moving anything."
    goto eoclmove
  endif
  tar cf - var | (cd $clntvarinstdir; tar xf -)
  mv $clntvarinstdir/var $clntvarinstdir/client
  \rm -rf var
  ln -s $clntvarinstdir/client var
  eoclmove:
  cd $curwd
endif

if (! $?orduserrestore) then
  set orduserrestore=No
endif
echo " "
echo "Do you want ordinary users to be able to restore their"
echo "own files and/or directories without administrator help ?"
orduserclnt:
echo ' '
echo -n "Your choice (Yes/No) [$orduserrestore]: "
set c=blub
while (! `matches $c '^(ye?s?|no?)$'`)
  set c=$<
  if ("$c" == "") set c=$orduserrestore
  while (! `matches $c '^(ye?s?|no?)$'`)
    echo ' '
    echo "Invalid choice. Please try again."
    goto orduserclnt
  end
  set orduserrestore=$c
end
if (`matches $c 'ye?s?'`) then
  /bin/rm -f $clntinstdir/client/bin/afrestore \
		$clntinstdir/client/bin/afbackout \
		$clntinstdir/client/bin/backout \
		$clntinstdir/client/bin/restore
  cp $clntinstdir/client/bin/full_backup \
		$clntinstdir/client/bin/afrestore
  ln $clntinstdir/client/bin/afrestore $clntinstdir/client/bin/afbackout
  ln -s afbackout $clntinstdir/client/bin/backout
  ln -s afrestore $clntinstdir/client/bin/restore
  chmod 4755 $clntinstdir/client/bin/afrestore
  chmod 755 $clntinstdir/client/bin/xafrestore $clntinstdir/client/bin/__packpats
endif

if (! $?runclientconfig) then
  set runclientconfig=Yes
endif
echo ' '
echo "Now we may run the program $clntinstdir/client/bin/clientconfig,"
echo "so you can configure the client side of the backup system. Would"
echo 'you like to do this now ?'
confclnt:
echo ' '
echo -n "Your choice (Yes/No) [$runclientconfig]: "
set c=blub
while (! `matches $c '^(ye?s?|no?)$'`)
  set c=$<
  if ("$c" == "") set c=$runclientconfig
  if (! `matches $c '^(ye?s?|no?)$'`) then
    echo ' '
    echo "Invalid choice. Please try again."
    goto confclnt
  endif
end
set runclientconfig="$c"
if (`matches $c 'ye?s?'`) then
  $clntinstdir/client/bin/clientconfig
endif

return

####### add user and group entry to /etc/passwd and /etc/group

add_user_group:

set usrnamvar=`echo $__args|awk '{print $1}'`
set usridvar=`echo $__args|awk '{print $2}'`
set grpidvar=`echo $__args|awk '{print $3}'`
eval set usrnam='$'$usrnamvar
eval set usridexists='$?'"$usridvar"
if ($usridexists) then
  eval set usrid='$'"$usridvar"
else
  unset usrid
endif
eval set grpidexists='$?'"$grpidvar"
if ($grpidexists) then
  eval set grpid='$'"$grpidvar"
else
  unset grpid
endif

if (`grep '[ 	]*'"$usrnam": $adminprefix/passwd |wc -l` < 1) then
  echo "No user entry found for $usrnam in $adminprefix/passwd."
  askadduser:
  echo ' '
  echo -n 'Should we add an entry (Yes/No) [Yes] ? '
  set c=blub
  while (! `matches $c '^(ye?s?|no?)$'`)
    set c=$<
    if ("$c" == "") set c=Yes
    if (! `matches $c '^(ye?s?|no?)$'`) then
      echo ' '
      echo "Invalid choice. Please try again."
      goto askadduser
    endif
  end
  if (`matches $c '^ye?s?$'`) then
    if (! $?usrid) then
      set usrid=2988
    endif
    while (`cut -f3 -d: $adminprefix/passwd|grep "^$usrid"'$'|wc -l` > 0)
      @ usrid++
    end
    echo " "
    echo "The default user-ID is $usrid. If this is ok, just"
    echo "enter yes. If not, enter the desired ID."
    set c=blub
    askforuserid:
    while (! `matches $c '^(ye?s?|no?|[0-9][0-9]*)$'`)
      echo " "
      echo -n 'Your choice (Yes/No/userid) [Yes]: '
      set c=$<
      if ("$c" == "") set c='Yes'
      if (! `matches $c '^(ye?s?|no?|[0-9][0-9]*)$'`) then
        echo ' '
        echo 'This is no valid choice. Please try again.'
      endif
    end
    if (`matches $c '^[0-9][0-9]*$'`) then
      set usrid=$c
    else
      if (`matches $c '^n'`) then
        echo "Please enter the desired user-ID."
        goto askforuserid
      endif
    endif
    if (`cut -f3 -d: $adminprefix/passwd|grep "^$usrid"'$'|wc -l` > 0) then
      echo "Warning: A user entry with the user-ID $usrid already exists"
      echo "     on the system. Using it nonetheless."
    endif

    if (! $?grpid) then
      set grpid=14
    endif
    while (`cut -f3 -d: $adminprefix/group|grep "^$grpid"'$'|wc -l` < 1)
      @ grpid--
      if ($grpid == 0) then
        break
      endif
    end
    while (`cut -f3 -d: $adminprefix/group|grep "^$grpid"'$'|wc -l` < 1)
      @ grpid++
    end
    echo " "
    echo "The default group-ID is $grpid. If this is ok, just"
    echo "enter yes. If not, enter the desired ID."
    set c=blub
    askforgroupid:
    while (! `matches $c '^(ye?s?|no?|[0-9][0-9]*)$'`)
      echo " "
      echo -n 'Your choice (Yes/No/groupid) [Yes]: '
      set c=$<
      if ("$c" == "") set c='Yes'
      if (! `matches $c '^(ye?s?|no?|[0-9][0-9]*)$'`) then
        echo ' '
        echo 'This is no valid choice. Please try again.'
      endif
    end
    if (`matches $c '^[0-9][0-9]*$'`) then
      set grpid=$c
      if (`cut -f3 -d: $adminprefix/group|grep "^$grpid"'$'|wc -l` < 1) then
	echo "Warning: A group entry with this group-ID does not exist"
	echo "    in the system. Using it nonetheless."
      endif
    else
      if (`matches $c '^n'`) then
        echo "Please enter the desired group-ID."
        goto askforgroupid
      endif
    endif

    set panic=0
    cp $adminprefix/passwd $adminprefix/passwd.$$
    set saved_files=($saved_files $adminprefix/passwd.$$)

    if (! -e $adminprefix/passwd.$$) then
      echo "Panic: cannot make safety copy of $adminprefix/passwd."
      echo "I'll not go on to add the user. You have to do it yourself."
      set panic=1
    else
      if (`grep '^+' $adminprefix/passwd|wc -l` > 0) then
        \rm -f $tmpfile $tmpfile2
        echo "/^+/ i\" >! $tmpfile
        echo "$usrnam":x:"$usrid":"$grpid":"Backup Server:$servinstdir/server:" \
  		>> $tmpfile
        sed -f $tmpfile $adminprefix/passwd >! $tmpfile2
        if ($status) then
  	  echo "Error: adding the user to the system file failed."
  	  set panic=1
        else
  	  cp $tmpfile2 $adminprefix/passwd
          if ($status) then
  	    echo "Error: adding the user to the system file failed."
  	    set panic=1
	  endif
        endif
      else
        echo "$usrnam":x:"$usrid":"$grpid":"Backup Server:$servinstdir/server:" \
		>> $adminprefix/passwd
      endif
    endif

    if ($panic) then
      echo "The following line must be added to $adminprefix/passwd"
      echo '(before the line starting with a +, if present):'
      echo ' '
      echo "$usrnam":x:"$usrid":"$grpid":"Backup Server:$servinstdir/server:"
      echo " "
      echo "Please hit enter, when you are done with it."
      set a=$<
    endif
  endif
endif

eval set "$usrnamvar"=$usrnam
if ($?usrid) then
  eval set "$usridvar"=$usrid
endif
if ($?grpid) then
  eval set "$grpidvar"=$grpid
endif

return


####### add afbackup entry to /etc/services

add_backup_service:

set servnamvar=`echo $__args|awk '{print $1}'`
set portnumvar=`echo $__args|awk '{print $2}'`
set portnumexists=`eval echo '$?'$portnumvar`
eval set servnam='$'"$servnamvar"
if ($portnumexists) then
  eval set portnum='$'"$portnumvar"
else
  unset portnum
endif
echo ' '
echo 'An entry for the backup service is necessary in the system'
echo "file $adminprefix/services."
add_service:
set servicesline=`egrep '^[ 	]*'"$servnam"'([ 	].*)?$' $adminprefix/services`
set serviceslinen=`echo $servicesline|wc -w`
if ($serviceslinen > 0) then
  echo "There exists an entry for this service in $adminprefix/services."
  echo "It looks like this:"
  echo ' '
  echo $servicesline
  echo ' '
  echo 'Should we remove this entry and create a new one, or add another ?'
  serventry:
  echo ' '
  echo -n 'Your choice (Yes/No/Add) [No]: '
  set c=blub
  while (! `matches $c '^(ye?s?|no?|ad?d?)$'`)
    set c=$<
    if ("$c" == "") set c=no
    if (! `matches $c '^(ye?s?|no?|ad?d?)$'`) then
      echo ' '
      echo "Invalid choice. Please try again."
      goto serventry
    endif
  end
  if (`matches $c '^ad?d?'`) then
   newservname:
    echo " "
    echo -n "Enter a name for the new service: "
    set c=@
    set c=$<
    if (! `matches $c '^[a-zA-Z0-9_-]*$'`) then
      echo " "
      echo "A service name should contain only A-Z, a-z, 0-9, _ or -"
      echo "Please try again."
      goto newservname
    endif
    set servnam="$c"
    goto add_service
  endif
  if (`matches $c '^ye?s?$'`) then
    set panic=0
    cp $adminprefix/services $adminprefix/services.$$
    set saved_files=($saved_files $adminprefix/services.$$)

    if (! -e $adminprefix/services.$$) then
      echo "Panic: cannot make safety copy of $adminprefix/services."
      echo "I'll not go on to add the entry. You have to do it yourself."
      set panic=1
    else
      egrep -v '^[ 	]*'"$servnam"'([ 	].*)?$' $adminprefix/services >! $tmpfile
      if (! -e $tmpfile) then
	echo "Error: removing entry from the system file failed."
	set panic=1
      else
        cp $tmpfile $adminprefix/services
	if ($status) then
	  echo "Error: removing entry from the system file failed."
	  set panic=1
	endif
      endif
    endif
    if ($panic) then
      echo "The following line must be removed from $adminprefix/services"
      echo ' '
      egrep '^[ 	]*'"$servnam"'([ 	].*)?$'
      echo " "
      echo "Please hit enter, when you are done with it."
      set a=$<
    endif
    goto add_service
  endif    
  if (`matches $c '^no?$'`) then
    set portnum=`echo $servicesline|awk '{print $2}'|cut -d/ -f1|awk '{print $1}'`
  endif
else
  if (! $?portnum) then
    set portnum=2988
  endif
  while (`grep -i $portnum/tcp $adminprefix/services|grep -v $servnam|wc -l` > 0)
    @ portnum++
  end
 portserviceselection:
  if ($portnum == 2988) then
    set hint=' (hex 0xbac)'
  else
    set hint=''
  endif
  echo ' '
  echo "Should we add an entry to $adminprefix/services for this"
  echo 'service ? Default port number is '$portnum$hint'. If'
  echo 'you do not want to add an entry, just enter no. If you'
  echo 'would like to use a different port number, enter it. If'
  echo 'you want a service name different from '"$servnam"','
  echo 'enter it (all lowercase letters, numbers or -, please).'
  echo 'If everything is ok, enter yes.'
  set c="_-_"
  while (! `matches $c '^(ye?s?|no?|[0-9][0-9]*|[a-z][a-z0-9-]*)$'`)
    echo " "
    echo -n 'Your choice (Yes/No/port/servicename) [Yes]: '
    set c=$<
    if ($c == "") set c='Yes'
    if (! `matches $c '^(ye?s?|no?|[0-9][0-9]*|[a-z][a-z0-9-]*)$'`) then
      echo ' '
      echo 'This is no valid choice. Please try again.'
    endif
  end
  if (`matches $c '^[0-9][0-9]*$'`) then
    set portnum=$c
    echo " "
    echo "Ok, port number changed to $c."
    if (`grep '^[ 	]*[a-z][a-z0-9-]*[ 	][ 	]*'"$c/"'[tT][Cc][Pp]' $adminprefix/services|wc -l` > 0) then
      echo "Warning: Port number $c is already used in $adminprefix/services."
    endif
    goto portserviceselection
  endif
  if (`matches $c '^[a-z][a-z0-9-]*$'` && ! `matches $c '^(ye?s?|no?)$'`) then
    set servnam="$c"
    echo " "
    echo "Ok, service name changed to $c."
    if (`grep '^[ 	]*'"$c"'[ 	][ 	]*[1-9][0-9]*/[tT][Cc][Pp]' $adminprefix/services|wc -l` > 0) then
      echo "Error: Service name $c is already used in $adminprefix/services."
      goto add_service
    endif
    goto portserviceselection
  endif
  if (! `matches $c '^no?$'`) then
    echo "$servnam		$portnum/tcp" >> $adminprefix/services
  endif
endif
eval set "$servnamvar"=$servnam
eval set "$portnumvar"=$portnum

\rm -f $tmpfile

return

################### add entry to /etc/inetd.conf file

add_inetd_entry:

set servnam=`echo $__args|awk '{print $1}'`
set waitforexit=`echo $__args|awk '{print $2}'`
set prognam=`echo $__args|awk '{print $3}'`

echo 'An entry for this service is necessary in the system'
echo "file $adminprefix/inetd.conf."
add_inetdline:
set inetdline=(`egrep '^[ 	]*'"$servnam"'([ 	].*)?$' $adminprefix/inetd.conf`)
set inetdlinen=`echo $inetdline|wc -w`
if ($inetdlinen > 0) then
  echo "There exists an entry in $adminprefix/inetd.conf."
  echo "It looks like this:"
  echo ' '
  echo "$inetdline"
  echo ' '
  set def=No
  set i=($inetdline)
  if (! `matches $i[6]"/" ^$servinstdir"/"`) then
    echo 'The directory path in this entry seems to be wrong (It'
    echo 'should be '$servinstdir'/... )'
    set def="Yes"
  endif
  if (! `matches $i[6] /$prognam'$'` || ! `matches $i[7] /$prognam'$'`) then
    echo "The program name in this entry is wrong (it must be $prognam)."
    set def="Yes"
  endif
  if (! `matches $i[7]"/" ^$servinstdir"/"`) then
    echo 'The program path in this entry is wrong (it must be '$servinstdir'/$prognam).'
    set def="Yes"
  endif
  if ($i[5] != $username) then
    echo 'The username in this entry is wrong (should be '$username').'
    set def="Yes"
  endif
  if ($i[3] != "tcp" || $i[4] != "$waitforexit" || $i[2] != "stream") then
    echo 'The protocol in this entry is wrong (we are allowing'
    if ("$waitforexit" == "nowait") then
      echo 'several server processes).'
    else
      echo 'only one server process)'
    endif
    set def="Yes"
  endif
  if ($def != "No") then
    echo " "
  endif
  echo 'Should we remove this entry and create a new one ?'
  inetdentry:
  echo ' '
  echo -n 'Your choice (Yes/No) ['$def']: '
  set c=blub
  while (! `matches $c '^(ye?s?|no?)$'`)
    set c=$<
    if ("$c" == "") set c=$def
    if (! `matches $c '^(ye?s?|no?)$'`) then
      echo ' '
      echo "Invalid choice. Please try again."
      goto inetdentry
    endif
  end
  if (`matches $c '^ye?s?$'`) then
    set panic=0
    cp $adminprefix/inetd.conf $adminprefix/inetd.conf.$$
    set saved_files=($saved_files $adminprefix/inetd.conf.$$)

    if (! -e $adminprefix/inetd.conf.$$) then
      echo "Panic: cannot make safety copy of $adminprefix/inetd.conf."
      echo "I'll not go on to add the entry. You have to do it yourself."
      set panic=1
    else
      egrep -v '^[ 	]*'"$servnam"'([ 	].*)?$' $adminprefix/inetd.conf >! $tmpfile
      if (! -e $tmpfile) then
	echo "Error: removing entry from the system file failed."
	set panic=1
      else
        cp $tmpfile $adminprefix/inetd.conf
	if ($status) then
	  echo "Error: removing entry from the system file failed."
	  set panic=1
	endif
      endif
    endif
    if ($panic) then
      echo "The following line must be removed from $adminprefix/inetd.conf"
      echo ' '
      echo "$inetdline"
      echo " "
      echo "Please hit enter, when you are done with it."
      set a=$<
    endif
    goto add_inetdline
  else
    set line=(`egrep '^[ 	]*'"$servnam"'([ 	].*)?$' $adminprefix/inetd.conf`)
    set username=$line[5]
  endif
else
  echo ' '
  echo "Should we add an entry to $adminprefix/inetd.conf for this"
  echo 'service ? Default username is '$username'. If you do not'
  echo 'want to add an entry, just enter no. If you would like'
  echo 'to run the server under a different username, enter it.'
  echo 'If everything is ok, enter yes.'
  askaddinetd:
  echo ' '
  echo -n "Your choice (Yes/No/username) [Yes]: "
  set c=""
  set c=$<
  if ("$c" == "") set c=Yes
  if (! `matches $c '^(ye?s?|no?)$'`) then
    set username=$c
  endif
  if (! `matches $c '^no?$'`) then
    echo "$servnam	stream	tcp	$waitforexit	$username	$servinstdir/server/bin/$prognam $servinstdir/server/bin/$prognam $servinstdir/server/lib/backup.conf" >> $adminprefix/inetd.conf
  endif
endif

return

#########################
which_make:
set gmake=`find_program gmake`
if (`matches "$gmake" "^/"`) then
  setenv GNU_MAKE gmake
  return
endif

make -v >& /dev/null
if ($status) then
  unsetenv GNU_MAKE
  return
endif

if (`make -v|grep GNU|wc -l` > 0) then
  setenv GNU_MAKE make
  return
endif

setenv GNU_MAKE gmake

return

###########################################################################

choose_DES:

echo " "
echo "Will we use 128 Bit DES encryption for authenticating the"
echo "client to the server ? (Eric Young's DES library (currently"
echo "version 3.23) is required and by default expected in the"
echo "directory ../libdes. The defaults can be modified in the"
echo "following section, when answering yes) If no, the old style"
echo "encryption is performed."

if (! $?use_des) then
  set use_des=No
endif

gosub yes_no $use_des DES_yesno
DES_yesno:

set use_des=$yesno
if ($yesno == "no") then
  return
endif

set configure_options="$configure_options --with-des"

if (! $?des_header) then
  set des_header=des.h
endif

echo " "
echo "Please enter the name of the DES-library's header file"
gosub mult_choice "$des_header [^ ]*" DES_have_header
DES_have_header:

set des_header="$the_choice"

if (! $?des_include) then
  set des_include=../libdes
endif

echo " "
echo "Please enter the directory, where "$des_header" can be found"

gosub mult_choice "$des_include [^ ]*" DES_have_include
DES_have_include:

set des_include="$the_choice"

if (! -e $des_include/$des_header) then
  echo " "
  echo "Warning: File '$des_include/$des_header' does not exist."
endif

set configure_options="$configure_options --with-des-include=$des_include"
set configure_options="$configure_options --with-des-header=$des_header"

if (! $?des_lib) then
  set des_lib=-ldes
endif

echo " "
echo "Please enter the specifier of the DES-library for linking"
gosub mult_choice "$des_lib [^ ]*" DES_have_lib
DES_have_lib:

set des_lib="$the_choice"
if (`matches "$des_lib" '^lib'`) then
  set des_lib=`echo $des_lib|sed 's#^lib#-l#g;s#[.].*##g'`
endif
set deslibfile=`echo $des_lib|sed 's#^-l#lib#g'`'.*'

if (! $?des_libdir) then
  if ($?des_include) then
    set des_libdir=`echo $des_include|sed 's#/include$#/lib#g'`
  else
    set des_libdir=../libdes
  endif
endif

echo " "
echo "Please enter the directory, where the DES library can be found"

gosub mult_choice "$des_libdir [^ ]*" DES_have_libdir
DES_have_libdir:

set des_libdir="$the_choice"

ls $des_libdir/$deslibfile >& /dev/null
if ($status) then
  echo " "
  echo "Warning: File '$des_libdir/$deslibfile' does not exist."
else
  set deslibfile=`basename $des_libdir/$deslibfile`
endif

set configure_options="$configure_options --with-des-libdir=$des_libdir"
set configure_options="$configure_options --with-des-ldflag=$des_lib"

return

###########################################################################

choose_ZLIB:

echo " "
echo "Will we use the libz compression library to have"
echo "builtin compression available ?"

if (! $?use_zlib) then
  set use_zlib=No
endif

gosub yes_no $use_zlib ZLIB_yesno
ZLIB_yesno:

set use_zlib=$yesno
if ($yesno == "no") then
  return
endif

set configure_options="$configure_options --with-zlib"

if (! $?zlib_include) then
  set zlib_include=/usr/local/include
endif

echo " "
echo "Please enter the directory, where zlib.h can be found"

gosub mult_choice "$zlib_include [^ ]*" ZLIB_have_include
ZLIB_have_include:

set zlib_include="$the_choice"

if (! -e $zlib_include/zlib.h) then
  echo " "
  echo "Warning: File '$zlib_include/zlib.h' does not exist."
endif

set configure_options="$configure_options --with-zlib-include=$zlib_include"

if (! $?zlib_libdir) then
  if ($?zlib_include) then
    set zlib_libdir=`echo $zlib_include|sed 's#/include$#/lib#g'`
  else
    set zlib_libdir=/usr/local/lib
  endif
endif

echo " "
echo "Please enter the directory, where the libz library can be found"

gosub mult_choice "$zlib_libdir [^ ]*" ZLIB_have_libdir
ZLIB_have_libdir:

set zlib_libdir="$the_choice"

ls $zlib_libdir/libz.* >& /dev/null
if ($status) then
  echo " "
  echo "Warning: File '$zlib_libdir/libz.*' does not exist."
endif

set configure_options="$configure_options --with-zlib-libdir=$zlib_libdir"

return

###########################################################################

yes_no:

set yesno=blub
echo " "
echo -n 'Your choice (Yes/No) ['$__args']: '
set yesno=$<
if ($yesno == "") set yesno=$__args
if (! `matches $yesno '^(ye?s?|no?)$'`) then
  echo ' '
  echo 'This is no valid choice. Please try again.'
  goto yes_no
endif

if (`matches $yesno '^ye?s?$'`) then
  set yesno="yes"
else
  set yesno="no"
endif

return

###########################################################################

mult_choice:

set noglob
set defaultsel=`echo "$__args"|awk '{print $1}'`
set validch=`echo "$__args"|awk '{printf "%s",$2; for(i=3;i<=NF;i++) printf " %s", $i}'`

echo " "
set the_choice=blub
echo -n 'Your choice ['$defaultsel']: '
set the_choice="$<"
if ("$the_choice" == "") set the_choice=$defaultsel
if (! `matches "$the_choice" '^'"$validch"'$'`) then
  echo ' '
  echo 'This is no valid choice. Please try again.'
  goto mult_choice
endif
unset noglob

return
