#!/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/
#		24 Mar 96   Framstag	added utf7encode binary
#		 4 Apr 96   Framstag	better HP-UX support
#		23 Jun 96   Framstag	added INLOG and OUTLOG
#		13 Sep 96   Framstag	added $BINDIR to check_sendfile
#                                       better /etc/inetd.conf parsing
#
# 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}
                /define INLOG/     {print "INLOG="$2}
                /define OUTLOG/    {print "OUTLOG="$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
if [ "$SPOOL" = "" ]; then	SPOOL=/var/spool/sendfile; fi
if [ "$INLOG" = "" ]; then	INLOG=$SPOOL/LOG/in; fi
if [ "$OUTLOG" = "" ]; then	OUTLOG=$SPOOL/LOG/out; 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 utf7encode $BINDIR/
( cd $BINDIR; ln -s utf7encode utf7decode 2>/dev/null)
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/ { sfd=$6;
                          if (index($7,"/sendfiled")>0) sfd=$7;
                          if (substr(sfd,1,1)=="?") sfd=substr(sfd,2);
                          print sfd;
                        }' /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/inetd.sec ]; then
  if [ "`grep '^saft' /etc/inetd.sec`" = "" ]; then
    echo >> /etc/inetd.sec
    echo "saft allow" >> /etc/inetd.sec
  fi
fi

if [ -f /etc/profile ]; then
  if [ "`grep check_sendfile /etc/profile`" = "" ]; then
    echo "adding check_sendfile to /etc/profile"
    echo >>/etc/profile
    echo $BINDIR/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 $BINDIR/check_sendfile >>/etc/csh.login
  fi
fi
				
if [ ! -d "$SPOOL/OUTGOING" ]; then
  echo "creating $SPOOL"
  mkdir_recursive $SPOOL/OUTGOING
  chmod 755  $SPOOL
  chmod 1777 $SPOOL/OUTGOING
fi
if [ ! -d `dirname "$INLOG"` ]; then 
  mkdir_recursive `dirname $INLOG`
  chmod 700 `dirname $INLOG`
fi
if [ ! -d `dirname "$OUTLOG"` ]; then 
  mkdir_recursive `dirname $OUTLOG`
  chmod 700 `dirname $OUTLOG`
fi
if [ ! -f "$INLOG" ]; then 
  echo "# use \"utf7decode $INLOG\" to view this file" > $INLOG
  echo >> $INLOG
  chmod 600 $INLOG
fi
if [ ! -f "$OUTLOG" ]; then 
  echo "# use \"utf7decode $OUTLOG\" to view this file" > $OUTLOG
  echo >> $OUTLOG
  chmod 600 $OUTLOG
fi

if df -k >/dev/null 2>&1; then 
  free=`df -k $SPOOL | awk '/\//{print int($4/1024)}'`
else
  free=`df $SPOOL | awk '/\//{print int($3/1024)}'`
fi
minfree=`awk '/minfree =/{print $3}' $CONFIG`
if [ "$free" -le "$minfree" ]; then
  cat <<EOD
		! WARNING ! 
You have not enough free disk space in $SPOOL !
Either give the spool directory more space or lower the value for the 
minfree option in $CONFIG
EOD
fi

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