#!/bin/sh
#
# File:         install
#
# Author:       Ulli Horlacher (framstag@rus.uni-stuttgart.de)
#
# History:      18 Sep 95   Framstag	initial version (insserv)
#               24 Oct 95   Framstag	changed SAFT-port
#               25 Nov 95   Framstag	new version: install
#               18 Dec 95   Framstag	better error checking
#		21 Dec 95   Framstag	new default for nosendfile: 
# 					/usr/local/etc/
#
# Shell script to install the server, the clients and the man-pages for the 
# sendfile service. It also configures /etc/inetd.conf, /etc/services and
# /usr/local/etc/nosendfile . 
# This script is called by make.
#
# If you want to change the default directories look in config.h
# You should not edit this file.
#
# This file is covered by the GNU General Public License


mkdir_recursive() {
  echo $1 \
    | awk -F/ 'BEGIN { printf("mkdir ") }
               { for (i=2; $i!=""; i++) 
                 { for (j=2; j<=i; j++) printf("/"$j)
	           printf(" ")
	         }
	       }
               END { print"2>/dev/null" }' \
    | sh
}


make_man() {
  sed "s:/usr/local/etc/nosendfile:$NOSENDFILE:
       s:/var/spool/sendfile:$SPOOL:" $1 >$MANDIR/$1
}


if [ "$1" = "" ]; then 
  echo "To invoke install, type: make; make install"
  exit
fi

if [ "$LOGNAME" != root ]; then
  if [ "`whoami`" != root ]; then
    echo
    echo "You are not root! You probably run into problems now..."
    echo
  fi
fi

eval `awk -F\" '/BINDIR/    {print "BINDIR="$2} 
                /MANDIR/    {print "MANDIR="$2} 
                /SERVERDIR/ {print "SERVERDIR="$2}
                /SPOOL/     {print "SPOOL="$2}
                /NOSENDFILE/{print "NOSENDFILE="$2}
	       ' config.h`
SYSTEM=$1
RESTART=false

if [ "$BINDIR" = "" ]; then	BINDIR=/usr/local/bin; fi
if [ "$MANDIR" = "" ]; then	MANDIR=/usr/local/man/man1; fi
if [ "$SERVERDIR" = "" ]; then	SERVERDIR=/usr/local/sbin; fi
if [ "$NOSENDFILE" = "" ]; then	NOSENDFILE=/usr/local/etc; fi

umask 022

echo "checking for directories"
mkdir_recursive $BINDIR
mkdir_recursive $MANDIR
mkdir_recursive $SERVERDIR
mkdir_recursive `dirname $NOSENDFILE`

echo "installing the clients in $BINDIR"
chmod 755 sendfile sendmsg receive check_sendfile
cp sendfile sendmsg receive check_sendfile $BINDIR/

echo "installing the man-pages in $MANDIR"
make_man sendfile.1
make_man sendmsg.1
make_man receive.1

SENDFILED=`awk '/^saft/{if (index($7,"/sendfiled")>0) print $7; else print $6}'\
                /etc/inetd.conf`
if [ "$SENDFILED" != "$SERVERDIR/sendfiled" -a "$SENDFILED" != "" ]; then
  echo "WARNING: cannot install sendfiled in $SERVERDIR because in /etc/inetd.conf"
  echo "         there is $SENDFILED specified! Check it!"
else
  echo "installing the sendfile-daemon in $SERVERDIR"
  cp sendfiled $SERVERDIR
fi

if [ -f /etc/nosendfile -a ! -L /etc/nosendfile ]; then 
  mv /etc/nosendfile $NOSENDFILE
  ln -s $NOSENDFILE /etc/nosendfile 
fi
if [ ! -f $NOSENDFILE ]; then 
  echo "installing the sendfile blacklist as $NOSENDFILE"
  cp nosendfile $NOSENDFILE
  chmod 644 $NOSENDFILE
fi

if [ "$SYSTEM" = NEXT ]; then
  SERVICES="`nidump services . | grep '[	]487/tcp'`"
else
  SERVICES="`grep '[	]487/tcp' /etc/services`"
fi
if [ "$SERVICES" != "" ]; then
  case "$SERVICES" in 
    saft*)	;;
    *)		echo "ERROR: tcp-port 487 is already in use!"; exit 1;;
  esac
else
  if [ "$SYSTEM" = NEXT ]; then
    echo "configuring services"
    echo "saft	487/tcp		# simple asynchronous file transfer" | niload services .
  else
    echo "configuring /etc/services"
    echo "#" >>/etc/services
    echo "saft	487/tcp		# simple asynchronous file transfer" >>/etc/services
  fi
fi

if [ "$SENDFILED" = "" ]; then
  if [ -f /usr/sbin/tcpd ]; then 
    SFD="/usr/sbin/tcpd $SERVERDIR/sendfiled"
  else
    SFD="$SERVERDIR/sendfiled sendfiled"
  fi
  RESTART=true
  echo "configuring /etc/inetd.conf"
  echo "#" >>/etc/inetd.conf
  echo "# simple asynchronous file transfer" >>/etc/inetd.conf
  echo "saft	stream	tcp	nowait	root	$SFD" >>/etc/inetd.conf
fi

if [ -f /etc/profile ]; then
  if [ "`grep check_sendfile /etc/profile`" = "" ]; then
    echo "adding check_sendfile to /etc/profile"
    echo >>/etc/profile
    echo check_sendfile >>/etc/profile
  fi
fi
if [ -f /etc/csh.login ]; then
  if [ "`grep check_sendfile /etc/csh.login`" = "" ]; then
    echo "adding check_sendfile to /etc/csh.login"
    echo >>/etc/csh.login
    echo check_sendfile >>/etc/csh.login
  fi
fi

if [ ! -d "$SPOOL" ]; then
  echo "creating $SPOOL"
  mkdir_recursive $SPOOL
fi
chmod 755 $SPOOL

if [ "$RESTART" = true ]; then
  echo
  echo "please restart now your inetd ( or simply reboot :-) )"
  echo
fi
