#!/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
#
# 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
# /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:/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

umask 022

echo checking for directories
mkdir_recursive $BINDIR
mkdir_recursive $MANDIR
mkdir_recursive $SERVERDIR

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

echo installing the sendfile-daemon in $SERVERDIR
cp sendfiled $SERVERDIR

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 "install-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 [ "`grep '^saft' /etc/inetd.conf`" = "" ]; 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
