#!/bin/sh

#makeconfig CC=cc
#make

prg=`basename $0`
arch=${1:-`ls -ld /client | awk -F/ '{print $NF}'`}
sfd=/sw/$arch/sendfile-2.1
sfold=/sw/$arch/sendfile-2.0
sfs=`pwd`

if [ -d $sfold ]; then
  cd $sfold
  echo . | /client/sbin/SWdeinstall -c /sw/clients/$arch
  cd $sfs
  rm -rf $sfold
fi

if [ -f /usr/man/man7/locale.7 -o -f /usr/man/man7/term.7 ]; then
  manmisc=7
else
  manmisc=5
fi

if [ -f /usr/man/man8/inetd.8 ]; then
  manadmin=8
else
  manadmin=1m
fi

rm -rf $sfd
mkdir $sfd $sfd/bin $sfd/etc $sfd/install $sfd/doc
mkdir $sfd/man $sfd/man/man1
mkdir $sfd/man/man$manmisc $sfd/man/man$manadmin
cd src
cp receive sendfile sendmsg utf7encode wlock ewl $sfd/bin/ || exit 1
cp sendfiled $sfd/install/ || exit 1
cd $sfd/bin
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
cd $sfs/etc
cp sfconf sfdconf sf_cleanup $sfd/bin/ || exit 1
cp sendfile.deny $sfd/install/ || exit 1
sed 's/msglog = off/msglog = on/
     s|:/usr/bin|:/usr/bin:/client/bin|' sendfile.cf > $sfd/etc/sendfile.cf || exit 1
awk '{if ($0 != "CONFIG") print $0; else system("cat '$sfd/etc/sendfile.cf'")}' \
    sfdconf > $sfd/bin/sfdconf || exit 1
sed "s:receive -l:/client/bin/receive -l:" check_sendfile >$sfd/bin/check_sendfile
if [ $arch = pmax_ul43 ]; then
  sed "s:/bin/sh:/bin/sh5:
       s:SW_INSTALL:$sfd/install:" sf_install >$sfd/bin/sf_install
else
  sed "s:SW_INSTALL:$sfd/install:" sf_install >$sfd/bin/sf_install
fi
chmod 775 $sfd/bin/*
cd ../doc
cp *.1 $sfd/man/man1/
(cd $sfd/man/man1; ln -s utf7encode.1 utf7decode.1)
cp fetchfile.7 $sfd/man/man$manmisc/fetchfile.$manmisc
cp sendfiled.8 $sfd/man/man$manadmin/sendfiled.$manadmin
cp LIESMICH.neu LIESMICH.auch doku.ps $sfd/doc/

cd $sfd
rm -f *~ */*~

if [ "$prg" = swupdate ]; then 
  echo . | /client/sbin/SWrelease 
  echo
  cd bin
  chmod 755 *
  exit
fi

echo . | /client/sbin/SWdeinstall -c /sw/clients/$arch 2>/dev/null
rm Links 2>/dev/null
/client/sbin/SWlink -c /sw/clients/$arch
/client/sbin/SWmklinks -c /sw/clients/$arch
/client/sbin/SWmkreadme
head -5 README >qq
mv qq README
cat <<EOD>>README
Source: ftp.rus.uni-stuttgart.de:/pub/unix/comm/misc/sendfile.tar.gz
Kurzbeschreibung: asynchroner File und Message Transfer
Short-Description: asynchronous file and message transfer
Provider: rus.uni-stuttgart.de
Compiled-By: Ulli Horlacher (framstag@rus.uni-stuttgart.de)
Support: framstag@rus.uni-stuttgart.de
Documentation: man-pages, /client/doc/sendfile-2.1
Copyright: GPL
Status: available
User-Interface: Text

EOD
echo . | /client/sbin/SWrelease
echo . | /client/sbin/SWinstall -c /sw/clients/$arch
echo
cd bin
chmod 755 *
