#!/bin/sh
#
# File:         install
#
# Author:       Ulli Horlacher (framstag@rus.uni-stuttgart.de)
#
# Contribs:	Stefan Zehl (sec@42.org)
#               Michael Neumayer (eumel@42.org)
#
# History:      
#
#  1995-09-18 Framstag		initial version (insserv)
#  1995-10-24 Framstag		changed SAFT-port
#  1995-11-25 Framstag		new version: install
#  1995-12-18 Framstag		better error checking
#  1995-12-21 Framstag		new default for nosendfile: 
# 				/usr/local/etc/
#  1996-03-24 Framstag		added utf7encode binary
#  1996-04-04 Framstag		better HP-UX support
#  1996-06-23 Framstag		added INLOG and OUTLOG
#  1996-09-13 Framstag		added $BINDIR to check_sendfile
#                       	better /etc/inetd.conf parsing
#  1997-02-24 Framstag		fixed MANDIR
#  1997-03-20 Framstag		added Amiga support
#  1997-06-18 Framstag		added check for new sendfile.cf
#  1997-09-30 Framstag		better (own) spool free-space check
#  1997-11-22 Framstag		added sfconf
#  1997-11-23 Framstag		moved NOSENDFILE to ALLOW and DENY files
#				added sfdconf
#  1998-01-05 Framstag		better sfdconf installation
#  1998-02-27 sec		added reinstall option
#  1998-03-01 sec		fixed "text file busy"
#  1998-03-06 Framstag		better man sections locations
#  1998-03-13 Eumel		fixed rm-logfile-bug
#  1998-04-17 sec		better man-section detection
#  1998-09-29 Framstag		added ewl
#  1999-01-28 Framstag		added symlinks: sf sm rf
#  2005-05-30 Framstag		added xinetd detection
#
# Shell script to install the server, the clients and the man-pages for the 
# sendfile service. It also configures /etc/inetd.conf, /etc/xinetd.d/sendfile,
# /etc/services, /etc/profile and /etc/csh.login
# This script is called by make.
#
# If you want to change the default directories look in file build.
# 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_dirs() {
  mkdir_recursive $D$BINDIR
  mkdir_recursive $D$MANDIR/man1
  mkdir_recursive $D$SERVERDIR
  mkdir_recursive $D$EGDIR
  mkdir_recursive $D$EGDIR
  mkdir $D/$MANDIR/man$manmisc $D/$MANDIR/man$manadmin 2>/dev/null
}

make_man() {
  sed "s:/usr/local/etc/sendfile.allow:$ALLOW:
       s:/usr/local/etc/sendfile.deny:$DENY:
       s:/usr/local/etc/sendfile.cf:$CONFIG:
       s:/var/spool/sendfile:$SPOOL:
      " doc/$1.$2 >$D$MANDIR/man$3/$1.$3
}

install_man() {
  make_man sendfile 1 1
  make_man sendmsg 1 1
  make_man receive 1 1
  make_man fetchfile 1 1
  make_man fetchfile 7 $manmisc
  make_man sendfiled 8 $manadmin
  cp doc/wlock.1 doc/utf7encode.1 $D$MANDIR/man1
  (cd $D$MANDIR/man1; ln -s utf7encode.1 utf7decode.1 2>/dev/null)
}

SYSTEM=$1
RESTART=false

if [ "$SYSTEM" = "" ]; then 
  echo "To invoke install, type: make install"
  exit
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 EGDIR/     {print "EGDIR="$2}
                /define CONFIG/    {print "CONFIG="$2}
                /define DENY/	   {print "DENY="$2}
                /define ALLOW/	   {print "ALLOW="$2}
                /define ALIASES/   {print "ALIASES="$2}
                /define INLOG/     {print "INLOG="$2}
                /define OUTLOG/    {print "OUTLOG="$2}
	       ' src/globals.h`

D=$DESTDIR

if [ "$BINDIR" = "" ]; then	BINDIR=/usr/local/bin; fi
if [ "$MANDIR" = "" ]; then	MANDIR=/usr/local/man; fi
if [ "$SERVERDIR" = "" ]; then	SERVERDIR=/usr/local/sbin; fi
if [ "$CONFIG" = "" ]; then	CONFIG=/usr/local/etc/sendfile.cf; fi
if [ "$DENY" = "" ]; then	DENY=/usr/local/etc/sendfile.deny; fi
if [ "$ALLOW" = "" ]; then	ALLOW=/usr/local/etc/sendfile.allow; fi
if [ "$ALIASES" = "" ]; then	ALIASES=/usr/local/etc/sendfile.aliases; 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
if [ "$INETDCONF" = "" ]; then	INETDCONF=/etc/inetd.conf; fi
if [ "$SERVICES" = "" ]; then	SERVICES=/etc/services; fi

if [ -f /etc/xinetd.conf -a -d /etc/xinetd.d ]; then
  INETDCONF=/etc/xinetd.d/sendfile
fi  

CONFIG=$PKG_SYSCONFDIR/sendfile.cf
DENY=$PKG_SYSCONFDIR/sendfile.deny

case "$SYSTEM" in
     *) manmisc=7; manadmin=8;;
esac

if [ "$2"  = "reinstall" ] ; then
	umask 022
	cd src || exit 1
	make_dirs
	cp sendfile sendmsg receive utf7encode fetchfile wlock $D$BINDIR/
	[ -x $D$SERVERDIR/sendfiled ] && mv $D$SERVERDIR/sendfiled $D$SERVERDIR/sendfiled.old && rm -f $D$SERVERDIR/sendfiled.old
	cp sendfiled $D$SERVERDIR
	cd ../etc
	cp sfconf sfdconf $D$BINDIR/
	cd ..
	install_man
	echo Done.
	exit 0
fi

umask 022

echo "checking for directories"
make_dirs

echo "installing the clients in $BINDIR"
cd etc || exit 1
sed "s:/usr/local/etc/sendfile.cf:$CONFIG:
     s:/var/spool/sendfile:$SPOOL:" sf_cleanup \
     > $D$BINDIR/sf_cleanup
sed "s:SPOOL=/var/spool/sendfile:SPOOL=$SPOOL:
     s:INLOG=/var/spool/sendfile/LOG/in:INLOG=$INLOG:
     s:OUTLOG=/var/spool/sendfile/LOG/in:OUTLOG=$OUTLOG:
     s:CONFIG=/usr/local/etc/sendfile.cf:CONFIG=$CONFIG:
     s:ALIASES=/usr/local/etc/sendfile.aliases:ALIASES=$ALIASES:
     s:DENY=/usr/local/etc/sendfile.deny:DENY=$DENY:
     s:ALLOW=/usr/local/etc/sendfile.allow:ALLOW=$ALLOW:
    " sfdconf \
    | awk '{if ($0 != "CONFIG") print $0; else system("cat sendfile.cf")}' \
    > $D$BINDIR/sfdconf
sed "s:/var/spool/sendfile:$SPOOL:" sfconf >$D$BINDIR/sfconf
sed "s:receive :$BINDIR/receive :" check_sendfile >$D$BINDIR/check_sendfile
cd ../src || exit 1
cp sendfile sendmsg receive utf7encode fetchfile wlock $D$BINDIR/
cd ..
(cd $D$BINDIR
 ln -s utf7encode utf7decode 2>/dev/null
 ln -s sendfile sf 2>/dev/null
 ln -s sendmsg  sm 2>/dev/null
 ln -s receive  rf 2>/dev/null
 chmod 755 sfconf sfdconf <sf_cleanup check_sendfile \
           sendfile sendmsg receive fetchfile utf7encode)

echo "installing the man-pages in $MANDIR/*"
install_man

echo "installing the sendfile-daemon in $SERVERDIR"
[ -x $D$SERVERDIR/sendfiled ] && mv $D$SERVERDIR/sendfiled $D$SERVERDIR/sendfiled.old && rm -f $D$SERVERDIR/sendfiled.old
cp src/sendfiled $D$SERVERDIR  || exit 1

NOSENDFILE=$D`dirname $DENY 2>/dev/null`/nosendfile
if [ -f $NOSENDFILE ]; then 
  if grep ^allow-only $D$NOSENDFILE >/dev/null; then 
    echo "Information: $D$NOSENDFILE is now $ALLOW (autoconverted)"
    grep -v ^allow-only $D$NOSENDFILE > $D$ALLOW
  else  
    echo "Information: $NOSENDFILE is now $DENY (autoconverted)"
    mv $NOSENDFILE $D$DENY
  fi
fi
if [ ! -f $D$EGDIR/sendfile.allow -a ! -f $D$EGDIR/sendfile.deny ]; then 
  echo "installing the sendfile deny file into $D/$EGDIR"
  cp etc/sendfile.deny $D$EGDIR
  chmod 644 $D$EGDIR/sendfile.deny
fi

if [ ! -f $D$EGDIR ]; then 
  echo "installing the global sendfile config file as $D$EGDIR/sendfile.cf"
  cp etc/sendfile.cf $D$EGDIR
  chmod 644 $D$EGDIR/sendfile.cf
else 
  if [ "`diff etc/sendfile.cf $D$EGDIR`" ]; then
    echo "Warning: `pwd`/etc/sendfile.cf differs from $D$EGDIR !"
    echo "Please take a look what has been changed!"
  fi
fi

			
echo
echo "You may want to add the following line to your system bootup script:"
echo "    $SERVERDIR/sendfiled -Q"
echo "This will start an outgoing spooling sendfile daemon on boot time"
echo "which processes any old files in the outgoing spool."
echo

