#! /bin/csh -f

unalias cd
unalias cp
unalias mv

set path=(/usr/bin /bin /usr/ccs/bin /usr/ccs/lib $path)
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'

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

setenv CC "$CC -DORIG_DEFAULTS"

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=""
set MAKECMD="make"
if ($?GNU_MAKE) then
  set MAKEOPTS="MAKECMD=$GNU_MAKE"
  set MAKECMD=$GNU_MAKE
endif

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 " "

if (! $?GNU_MAKE) then
  echo Warning: No GNU-make found. Maybe the Makefile has to be edited.
  echo " "
  /bin/rm -f Makefile
  ln -s Makefile.no-GNU-make Makefile
else
  /bin/rm -f Makefile
  ln -s Makefile.GNU-make Makefile
endif

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]

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.
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

exit 0

### 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_remclient
end_remclient:
return

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

server_install:
echo "First we have to run make. 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 make now ..."
echo " "
if ($mode == "all" || $mode == "clntrem") then
  $MAKECMD CC="$CC" $MAKEOPTS
  set s=$status
else
  $MAKECMD server CC="$CC" $MAKEOPTS
  set s=$status
endif

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

set servinstdir=/usr/backup

echo "Ok, this seems to have worked. Now we can install"
echo "the files and programs to a directory of your"
echo 'choice (you may supply the same directory as for'
echo '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

echo " "
echo "We're now going to install the files and programs."
echo " "
if ($mode == "all" || $mode == "clntrem") then
  $MAKECMD install install.rclient BASEDIR=$servinstdir $MAKEOPTS
  set s=$status
else
  $MAKECMD install.server BASEDIR=$servinstdir $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
    exit 1
  else
    echo ' '
    goto ask_for_servinstdir
  endif
endif

\rm -f $tmpfile

gosub add_backup_service "" serv_serv_added

serv_serv_added:

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

echo ' '
echo 'An entry for the backup service is necessary in the system'
echo "file $adminprefix/inetd.conf."
add_inetdline:
set inetdlinen=`grep '^[ 	]*afbackup' $adminprefix/inetd.conf|wc -l`
set inetdline=(`grep '^[ 	]*afbackup' $adminprefix/inetd.conf`)
if ($inetdlinen > 0) then
  echo "There is an entry for afbackup present in $adminprefix/inetd.conf."
  echo "It looks like this:"
  echo ' '
  grep '^[ 	]*afbackup' $adminprefix/inetd.conf
  echo ' '
  set def=No
  set i=($inetdline)
  if (! `matches $inetdline[6]"/" ^$servinstdir"/"`) then
    echo 'The directory path in this entry seems to be wrong (It'
    echo 'should be '$servinstdir'/... )'
    set def="Yes"
  endif
  if ($i[7] != "server") then
    echo 'The program name in this entry is wrong (it must be server).'
    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] != "nowait" || $i[2] != "stream") then
    echo 'The protocol in this entry is wrong (we are using TCP).'
    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
      grep -v '^[         ]*afbackup' $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 ' '
      grep '^[ 	]*afbackup' $adminprefix/inetd.conf
      echo " "
      echo "Please hit enter, when you are done with it."
      set a=$<
    endif
    goto add_inetdline
  else
    set line=(`grep '^[         ]*afbackup' $adminprefix/inetd.conf`)
    set username=$line[5]
  endif
else
  echo ' '
  echo "Should we add an entry to $adminprefix/inetd.conf for the afbackup"
  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 "afbackup	stream	tcp	nowait	$username	$servinstdir/server/bin/server server $servinstdir/server/lib/backup.conf" >> $adminprefix/inetd.conf
  endif
endif

echo ' '

if (`grep '[ 	]*'"$username": $adminprefix/passwd |wc -l` < 1) then
  echo "No user entry found for $username 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
    set userid=2988
    while (`cut -f3 -d: $adminprefix/passwd|grep "^$userid"'$'|wc -l` > 0)
      @ userid++
    end
    echo " "
    echo "The default user-ID is $userid. 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 userid=$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 "^$userid"'$'|wc -l` > 0) then
      echo "Warning: A user entry with the user-ID $userid already exists"
      echo "     on the system. Using it nonetheless."
    endif

    set groupid=14
    while (`cut -f3 -d: $adminprefix/group|grep "^$groupid"'$'|wc -l` < 1)
      @ groupid--
    end
    echo " "
    echo "The default group-ID is $groupid. 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 groupid=$c
      if (`cut -f3 -d: $adminprefix/group|grep "^$groupid"'$'|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 "$username":x:"$userid":"$groupid":"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 "$username":x:"$userid":"$groupid":"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 "$username":x:"$userid":"$groupid":"Backup Server:$servinstdir/server:"
      echo " "
      echo "Please hit enter, when you are done with it."
      set a=$<
    endif
  endif
endif

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]*"

  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

 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
endif


set PS1="ps -ef"
set PS2="ps -fp"
set OSNAME=`uname -s|sed 's/[^a-zA-Z]//g'`

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

set servvarinstdir=/var/logs/backup
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
  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) [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 serv
    endif
  end
  if (`matches $c 'ye?s?'`) then
    $servinstdir/server/bin/serverconfig
  endif
endif

if ($mode == "all" || $mode == "clntrem") then
  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) [No]: '
  set c=blub
  while (! `matches $c '^(ye?s?|no?)$'`)
    set c=$<
    if ("$c" == "") set c=no
    while (! `matches $c '^(ye?s?|no?)$'`)
      echo ' '
      echo "Invalid choice. Please try again."
      goto ordusersclnt
    end
  end
  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
  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) [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 confsclnt
    endif
  end
  if (`matches $c 'ye?s?'`) then
    $servinstdir/client/bin/clientconfig
  endif
endif

return


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

client_install:
echo "First we have to run make. 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 make now ..."
echo " "
$MAKECMD client CC="$CC" $MAKEOPTS
set s=$status
echo " "
if ($s != 0) then
  echo "Damn. Something went wrong. Please contact the author."
  echo "Stopping here."
  exit 1
endif

set clntinstdir=/usr/backup

echo "Ok, this seems to have worked. Now we can install"
echo "the files and programs to a directory of your"
echo 'choice (you may supply the same directory as for'
echo '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

echo " "
echo "We're now going to install the files and programs."
echo " "
$MAKECMD install.client BASEDIR=$clntinstdir $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
    exit 1
  else
    echo ' '
    goto ask_for_clntinstdir
  endif
endif

gosub add_backup_service "" clnt_serv_added

clnt_serv_added:

set clntvarinstdir=/var/logs/backup
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

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) [No]: '
set c=blub
while (! `matches $c '^(ye?s?|no?)$'`)
  set c=$<
  if ("$c" == "") set c=no
  while (! `matches $c '^(ye?s?|no?)$'`)
    echo ' '
    echo "Invalid choice. Please try again."
    goto orduserclnt
  end
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
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) [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 confclnt
  endif
end
if (`matches $c 'ye?s?'`) then
  $clntinstdir/client/bin/clientconfig
endif

return


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

add_backup_service:

echo ' '
echo 'An entry for the backup service is necessary in the system'
echo "file $adminprefix/services."
add_service:
set servicesline=`grep '^[ 	]*afbackup' $adminprefix/services|wc -l`
if ($servicesline > 0) then
  echo "There is an entry for afbackup present in $adminprefix/services."
  echo "It looks like this:"
  echo ' '
  grep '^[ 	]*afbackup' $adminprefix/services
  echo ' '
  echo 'Should we remove this entry and create a new one ?'
  serventry:
  echo ' '
  echo -n 'Your choice (Yes/No) [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 serventry
    endif
  end
  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
      grep -v '^[ 	]*afbackup' $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 ' '
      grep '^[ 	]*afbackup' $adminprefix/services
      echo " "
      echo "Please hit enter, when you are done with it."
      set a=$<
    endif
    goto add_service
  endif    
else
  set port=2988
  while (`grep -i $port/tcp $adminprefix/services|grep -v afbackup|wc -l` > 0)
    @ port++
  end
  if ($port == 2988) then
    set hint=' (hex 0xbac)'
  else
    set hint=''
  endif
  echo ' '
  echo "Should we add an entry to $adminprefix/services for the afbackup"
  echo 'service ? Default port number is '$port$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.'
  echo 'If everything is ok, enter yes.'
  set c=blub
  while (! `matches $c '^(ye?s?|no?|[0-9][0-9]*)$'`)
    echo " "
    echo -n 'Your choice (Yes/No/port) [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 port=$c
  endif
  if (! `matches $c '^no?$'`) then
    echo "afbackup		$port/tcp" >> $adminprefix/services
  endif
endif

\rm -f $tmpfile

return


which_make:

gmake -v >& /dev/null
if (! $status) 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
