#!/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,
# /usr/local/etc/sendfile.cf 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() {
  if [ ! -d $1 ]; then
    mkdir `echo $1 | 
           awk -F/ '{ for (i=2; $i!=""; i++)
                      { for (j=2; j<=i; j++) printf("/"$j)
  		        printf(" ") } }'
          ` 2>/dev/null
  fi
}


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


SYSTEM=$1
RESTART=false

if [ "$SYSTEM" = "" ]; 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\" '/define BINDIR/    {print "BINDIR="$2} 
                /define MANDIR/    {print "MANDIR="$2} 
                /define SERVERDIR/ {print "SERVERDIR="$2}
                /define SPOOL/     {print "SPOOL="$2}
                /define CONFIG/    {print "CONFIG="$2}
                /define NOSENDFILE/{print "NOSENDFILE="$2}
	       ' config.h`

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 [ "$CONFIG" = "" ]; then	CONFIG=/usr/local/etc/sendfile.cf; fi
if [ "$NOSENDFILE" = "" ]; then	NOSENDFILE=/usr/local/etc/nosendfile; fi

umask 022

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

echo "installing the clients in $BINDIR"
cp sendfile sendmsg receive check_sendfile $BINDIR/
sed "s:/usr/local/etc/sendfile.cf:$CONFIG:
     s:/var/spool/sendfile:$SPOOL:" sf_cleanup >$BINDIR/sf_cleanup
chmod 755 $BINDIR/sf_cleanup

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 ! -h /etc/nosendfile ]; then 
if [ "`ls -l /etc/nosendfile 2>/dev/null | cut -c1`" = "-" ]; then 
  mv /etc/nosendfile $NOSENDFILE
  ln -s $NOSENDFILE /etc/nosendfile 
fi
if [ ! -f $NOSENDFILE ]; then 
  echo "installing the sendfile allow/deny file as $NOSENDFILE"
  cp nosendfile $NOSENDFILE
  chmod 644 $NOSENDFILE
fi

if [ ! -f $CONFIG ]; then 
  echo "installing the global sendfile config file as $CONFIG"
  cp sendfile.cf $CONFIG
  chmod 644 $CONFIG
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
