#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-tcpip
SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
explodepkg $CWD/_tcpip.tar.gz
# This is for the "blue underscore" effect, already patched in.
#( cd $PKG ; zcat $CWD/termcap.diff.gz | patch )
#( cd $PKG ; rm etc/termcap.orig )

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+===============+"
echo "| NetKit-A-0.10 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/NetKit-A-0.10.tar.gz
cd NetKit-A-0.10
zcat $CWD/NetKit-A-0.10.diff.gz | patch
make
cd ytalk-3.0.2
make -f Makefile.old
strip ytalk
cat ytalk > $PKG/usr/bin/ytalk
cat ytalk.1 | gzip -9c > $PKG/usr/man/man1/ytalk.1.gz
cd ../pidentd-2.6.1
strip in.identd
cat in.identd > $PKG/usr/sbin/in.identd
cat identd.8 | gzip -9c > $PKG/usr/man/man8/in.identd.8.gz
cd ../tcp_wrappers_7.2
strip tcpd
cat tcpd > $PKG/usr/sbin/tcpd
make clean
make REAL_DAEMON_DIR=/usr/sbin/real-daemon-dir linux
strip tcpd safe_finger tcpdchk tcpdmatch try-from
cat tcpd > $PKG/usr/sbin/real-daemon-dir/tcpd
cat safe_finger > $PKG/usr/sbin/real-daemon-dir/safe_finger
cat tcpdchk > $PKG/usr/sbin/real-daemon-dir/tcpdchk
cat tcpdmatch > $PKG/usr/sbin/real-daemon-dir/tcpdmatch
cat try-from > $PKG/usr/sbin/real-daemon-dir/try-from
cat hosts_access.3 | gzip -9c > $PKG/usr/man/man3/hosts_access.3.gz
cat hosts_access.5 | gzip -9c > $PKG/usr/man/man5/hosts_access.5.gz
cat hosts_options.5 | gzip -9c > $PKG/usr/man/man5/hosts_options.5.gz
cat tcpd.8 | gzip -9c > $PKG/usr/man/man8/tcpd.8.gz
cat tcpdchk.8 | gzip -9c > $PKG/usr/man/man8/tcpdchk.8.gz
cat tcpdmatch.8 | gzip -9c > $PKG/usr/man/man8/tcpdmatch.8.gz
cd ../traceroute-4.4BSD
strip traceroute
cat traceroute > $PKG/usr/bin/traceroute
cat traceroute.8 | gzip -9c > $PKG/usr/man/man8/traceroute.8.gz
cd ../net-tools-1.2.0
strip arp ifconfig ipfw plipconfig rarp route netstat slattach
cat arp > $PKG/sbin/arp
cat ifconfig > $PKG/sbin/ifconfig
cat ipfw > $PKG/sbin/ipfw
cat plipconfig > $PKG/sbin/plipconfig
cat rarp > $PKG/sbin/rarp
cat route > $PKG/sbin/route
# cat hostname > $PKG/bin/hostname # same binary already comes with util-linux
cat netstat > $PKG/bin/netstat
cat slattach > $PKG/usr/sbin/slattach
cd man/en_US.88591
for page in dnsdomainname.1 hostname.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cat resolver.5 | gzip -9c > $PKG/usr/man/man5/resolver.5.gz
for page in arp.8 ipfw.8 ifconfig.8 netstat.8 plipconfig.8 rarp.8 \
 route.8 slattach.8 ; do
  cat $page | gzip -9c > $PKG/usr/man/man8/$page.gz
done
cd ../../../netdate
strip netdate
cat netdate > $PKG/usr/sbin/netdate
cat netdate.8 | gzip -9c > $PKG/usr/man/man8/netdate.8.gz
cd ../nfs-server-2.2beta2
strip rpc.mountd rpc.nfsd showmount
cat rpc.mountd > $PKG/usr/sbin/rpc.mountd
cat rpc.nfsd > $PKG/usr/sbin/rpc.nfsd
cat showmount > $PKG/usr/sbin/showmount
cat exports.5 | gzip -9c > $PKG/usr/man/man5/exports.5.gz
for page in mountd.8 nfsd.8 showmount.8 ; do
  cat $page | gzip -9c > $PKG/usr/man/man8/$page.gz
done
cd ../bootp-2.4.3
strip bootpd bootpef bootpgw bootptest
for file in bootpd bootpef bootpgw bootptest ; do
  cat $file > $PKG/usr/sbin/$file
done
gzip -9c bootptab.5 > $PKG/usr/man/man5/bootptab.5.gz
for file in bootpd.8 bootpef.8 bootptest.8 ; do
  gzip -9c $file > $PKG/usr/man/man8/$file.gz
done
cd ../icmpinfo-1.10
strip icmpinfo
cat icmpinfo > $PKG/usr/sbin/icmpinfo
gzip -9c icmpinfo.man > $PKG/usr/man/man1/icmpinfo.1.gz

echo "+===============+"
echo "| NetKit-B-0.06 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/NetKit-B-0.06.tar.gz
cd NetKit-B-0.06
zcat $CWD/NetKit-B-0.06.diff.gz | patch
tar xzvf $CWD/arpa.tar.gz
make
cd biff
strip biff
cat biff > $PKG/usr/bin/biff
cat biff.1 | gzip -9c > $PKG/usr/man/man1/biff.1.gz
cd ../comsat
strip comsat
cat comsat > $PKG/usr/sbin/in.comsat
cat comsat.1 | gzip -9c > $PKG/usr/man/man8/in.comsat.8.gz
cd ../finger
strip finger
cat finger > $PKG/usr/bin/finger
cat finger.1 | gzip -9c > $PKG/usr/man/man1/finger.1.gz
cd ../fingerd
strip fingerd
cat fingerd > $PKG/usr/sbin/in.fingerd
cat fingerd.8 | gzip -9c > $PKG/usr/man/man8/in.fingerd.8.gz
cd ../ftp
strip ftp
cat ftp > $PKG/bin/ftp
cat ftp.1 | gzip -9c > $PKG/usr/man/man1/ftp.1.gz
cd ../lpr/lpc
cat lpc > $PKG/usr/sbin/lpc
cat lpc.8 | gzip -9c > $PKG/usr/man/man8/lpc.8.gz
cd ../filters
cat lpf > $PKG/usr/sbin/lpf
cd ../lpd
cat lpd > $PKG/usr/sbin/lpd
cat lpd.8 | gzip -9c > $PKG/usr/man/man8/lpd.8.gz
cd ../lpq
cat lpq > $PKG/usr/bin/lpq
cat lpq.1 | gzip -9c > $PKG/usr/man/man1/lpq.1.gz
cd ../lpr
cat lpr > $PKG/usr/bin/lpr
cat lpr.1 | gzip -9c > $PKG/usr/man/man1/lpr.1.gz
cd ../lprm
cat lprm > $PKG/usr/bin/lprm
cat lprm.1 | gzip -9c > $PKG/usr/man/man1/lprm.1.gz
cd ../lptest
cat lptest > $PKG/usr/sbin/lptest
cat lptest.1 | gzip -9c > $PKG/usr/man/man1/lptest.1.gz
cd ../pac
cat pac > $PKG/usr/sbin/pac
cat pac.8 | gzip -9c > $PKG/usr/man/man8/pac.8.gz
cd ..
cat printcap.5 | gzip -9c > $PKG/usr/man/man5/printcap.5.gz
cd ../inetd
strip inetd
cat inetd > $PKG/usr/sbin/inetd
cat inetd.8 | gzip -9c > $PKG/usr/man/man8/inetd.8.gz
cd ../ping
strip ping
cat ping > $PKG/bin/ping
cat ping.8 | gzip -9c > $PKG/usr/man/man8/ping.8.gz
cd ../portmap
strip portmap pmap_dump pmap_set
cat portmap > $PKG/usr/sbin/rpc.portmap
cat pmap_dump > $PKG/usr/sbin/pmap_dump
cat pmap_set > $PKG/usr/sbin/pmap_set
cat portmap.8 | gzip -9c > $PKG/usr/man/man8/portmap.8.gz
cd ../rcp
cat rcp > $PKG/usr/bin/rcp
cat rcp.1 | gzip -9c > $PKG/usr/man/man1/rcp.1.gz
cd ../rexecd
strip rexecd
cat rexecd > $PKG/usr/sbin/in.rexecd
cat rexecd.8 | gzip -9c > $PKG/usr/man/man8/rexecd.8.gz
cd ../rlogin
strip rlogin
cat rlogin > $PKG/usr/bin/rlogin
cat rlogin.1 | gzip -9c > $PKG/usr/man/man1/rlogin.1.gz
cd ../rlogind
strip rlogind
cat rlogind > $PKG/usr/sbin/in.rlogind
cat rlogind.8 | gzip -9c > $PKG/usr/man/man8/in.rlogind.8.gz
cd ../routed
strip routed
cat routed > $PKG/usr/sbin/routed
cat routed.8 | gzip -9c > $PKG/usr/man/man8/routed.8.gz
cd ../rpc.rusersd
strip rusersd
cat rusersd > $PKG/usr/sbin/rpc.rusersd
cat rpc.rusersd.8 | gzip -9c > $PKG/usr/man/man8/rpc.rusersd.8.gz
cd ../rpc.rwalld
strip rwalld
cat rwalld > $PKG/usr/sbin/rpc.rwalld
cat rpc.rwalld.8 | gzip -9c > $PKG/usr/man/man8/rpc.rwalld.8.gz
cd ../rpcgen.new
strip rpcgen
cat rpcgen > $PKG/usr/bin/rpcgen
cat rpcgen.1 | gzip -9c > $PKG/usr/man/man1/rpcgen.1.gz
cd ../rpcinfo
strip rpcinfo
cat rpcinfo > $PKG/usr/sbin/rpcinfo
cat rpcinfo.8 | gzip -9c > $PKG/usr/man/man8/rpcinfo.8.gz
cd ../rsh
strip rsh
cat rsh > $PKG/usr/bin/rsh
cat rsh.1 | gzip -9c > $PKG/usr/man/man1/rsh.1.gz
cd ../rshd
strip rshd
cat rshd > $PKG/usr/sbin/in.rshd
cat rshd.8 | gzip -9c > $PKG/usr/man/man8/in.rshd.8.gz
cd ../rusers
strip rusers
cat rusers > $PKG/usr/bin/rusers
cat rusers.1 | gzip -9c > $PKG/usr/man/man1/rusers.1.gz
cd ../rwall
strip rwall
cat rwall > $PKG/usr/bin/rwall
cat rwall.1 | gzip -9c > $PKG/usr/man/man1/rwall.1.gz
cd ../rwho
strip rwho
cat rwho > $PKG/usr/bin/rwho
cat rwho.1 | gzip -9c > $PKG/usr/man/man1/rwho.1.gz
cd ../sliplogin
strip sliplogin
cat sliplogin > $PKG/usr/sbin/sliplogin
cat sliplogin.8 | gzip -9c > $PKG/usr/man/man8/sliplogin.8.gz
cd ../talk
strip talk
cat talk > $PKG/usr/bin/talk
cat talk.1 | gzip -9c > $PKG/usr/man/man1/talk.1.gz
cd ../talkd
strip talkd
cat talkd > $PKG/usr/sbin/in.talkd
cat talkd.8 | gzip -9c > $PKG/usr/man/man8/in.talkd.8.gz
cd ../telnet
cat telnet > $PKG/bin/telnet
cat telnet.1 | gzip -9c > $PKG/usr/man/man1/telnet.1.gz
# This has a security hole...
#cd ../telnetd
#cat telnetd > $PKG/usr/sbin/in.telnetd
#cat telnetd.8 | gzip -9c > $PKG/usr/man/man8/in.telnetd.8.gz
cd ../tftp
strip tftp
cat tftp > $PKG/usr/bin/tftp
cat tftp.1 | gzip -9c > $PKG/usr/man/man1/tftp.1.gz
cd ../tftpd
strip tftpd
cat tftpd > $PKG/usr/sbin/in.tftpd
cat tftpd.8 | gzip -9c > $PKG/usr/man/man8/in.tftpd.8.gz
cd ../timed/timed
strip timed
cat timed > $PKG/usr/sbin/in.timed
cat timed.8 | gzip -9c > $PKG/usr/man/man8/timed.8.gz
cd ../timedc
strip timedc
cat timedc > $PKG/usr/sbin/timedc
cat timedc.8 | gzip -9c > $PKG/usr/man/man8/timedc.8.gz

echo "+=============+"
echo "| fwhois-1.00 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/fwhois-1.00.tar.gz
cd fwhois-1.00
make
cat fwhois > $PKG/usr/bin/fwhois

echo "+====================+"
echo "| ipfwadm-2.0.tar.gz |"
echo "+====================+"
cd $TMP
tar xzvf $CWD/ipfwadm-2.0.tar.gz
cd ipfwadm-2.0
make
strip ipfwadm
cat ipfwadm > $PKG/sbin/ipfwadm
cat ipfwadm.8 | gzip -9c > $PKG/usr/man/man8/ipfwadm.8.gz
cat ipfw.4 | gzip -9c > $PKG/usr/man/man4/ipfw.4.gz

echo "+=================+"
echo "| popclient-3.0b4 |"
echo "+=================+"
cd $TMP
tar xzvf $CWD/popclient-3.0b4.tar.gz
cd popclient-3.0b4
configure --prefix=/usr
make CFLAGS=-O2
strip popclient
cat popclient > $PKG/usr/bin/popclient
cat popclient.man | gzip -9c > $PKG/usr/man/man1/popclient.1.gz

echo "+=============+"
echo "| ruptime-570 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/ruptime-570.tar.gz
cd ruptime
make
strip ruptime
cat ruptime > $PKG/usr/bin/ruptime
cat ruptime.1 | gzip -9c > $PKG/usr/man/man1/ruptime.1.gz

echo "+=============+"
echo "| wu-ftpd-2.4 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/wu-ftpd-2.4.tar.gz
cd wu-ftpd-2.4
zcat $CWD/wu-ftpd-2.4.diff.gz | patch
build lnx
cd bin
strip *
cat ckconfig > $PKG/usr/sbin/ckconfig
cat ftpcount > $PKG/usr/bin/ftpcount
cat ftpd > $PKG/usr/sbin/wu.ftpd
cat ftpshut > $PKG/usr/bin/ftpshut
cat ftpwho > $PKG/usr/bin/ftpwho
cd ../doc
for page in ftpcount.1 ftpwho.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cat realpath.3 | gzip -9c > $PKG/usr/man/man3/realpath.3.gz
for page in ftpaccess.5 xferlog.5 ftpconversions.5 ftphosts.5 ; do
  cat $page | gzip -9c > $PKG/usr/man/man5/$page.gz
done
for page in ftpd.8 ftpshut.8 ; do
  cat $page | gzip -9c > $PKG/usr/man/man8/$page.gz
done

echo "+================+"
echo "| yp-clients-2.1 |"
echo "+================+"
cd $TMP
tar xzvf $CWD/yp-clients-2.1.tar.gz
cd yp-clients-2.1
make
strip ypbind/ypbind domainname/domainname ypcat/ypcat ypmatch/ypmatch \
  yppasswd/yppasswd yppoll/yppoll ypset/ypset ypwhich/ypwhich
cat ypbind/ypbind > $PKG/usr/sbin/ypbind
cat domainname/domainname > $PKG/bin/domainname-yp
cat ypcat/ypcat > $PKG/usr/bin/ypcat
cat ypmatch/ypmatch > $PKG/usr/bin/ypmatch
cat yppasswd/yppasswd > $PKG/usr/bin/yppasswd
cat yppoll/yppoll > $PKG/usr/bin/yppoll
cat ypset/ypset > $PKG/usr/bin/ypset
cat ypwhich/ypwhich > $PKG/usr/bin/ypwhich

echo "+=============+"
echo "| ypserv-0.16 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/ypserv-0.16.tar.gz
cd ypserv-0.16
configure
make
strip dbload dbcat dbnext ypserv yppush makedbm ypxfr
cat ypserv > $PKG/usr/sbin/ypserv
cat yppush > $PKG/usr/sbin/yppush
cat ypMakefile > $PKG/var/yp/Makefile
cat ypMakefile.makedbm > $PKG/var/yp/Makefile.master
cat dbload > $PKG/usr/lib/yp/dbload
cat makedbm > $PKG/usr/lib/yp/makedbm
cat mknetid > $PKG/usr/lib/yp/mknetid
cat ypinit > $PKG/usr/lib/yp/ypinit
cat ypxfr > $PKG/usr/lib/yp/ypxfr

echo "+==============+"
echo "| telnetsnoopd |"
echo "+==============+"
cd $TMP
tar xzvf $CWD/telnetsnoopd.tar.gz
cd telnetsnoopd
tar xzvf $CWD/arpa.tar.gz # Old header file still needed to compile...
make
strip telnetsnoopd/telnetd
cat telnetsnoopd/telnetd > $PKG/usr/sbin/in.telnetsnoopd

echo "+===============+"
echo "| ttysnoop-0.11 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/ttysnoop-0.11.tar.gz
cd ttysnoop-0.11
make
strip ttysnoop ttysnoops
cat ttysnoops > $PKG/bin/ttysnoops
cat ttysnoop > $PKG/usr/sbin/ttysnoop
cat ttysnoop.8 | gzip -9c > $PKG/usr/man/man8/ttysnoop.8.gz

echo "+===============+"
echo "| linux_pcnfsd2 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/linux_pcnfsd2.tar.gz
cd pcnfsd_linux2
make -f Makefile.linux
cd linux
strip clnt.pcnfsd rpc.pcnfsd
cat clnt.pcnfsd > $PKG/usr/sbin/clnt.pcnfsd
cat rpc.pcnfsd > $PKG/usr/sbin/rpc.pcnfsd

echo "+=======+"
echo "| rwhod |"
echo "+=======+"
cd $TMP
tar xzvf $CWD/rwhod-519.tar.gz
cd rwhod
make
strip rwhod
cat rwhod > $PKG/usr/sbin/rwhod
cat rwhod.8 | gzip -9c > $PKG/usr/man/man8/rwhod.8.gz

echo "+========+"
echo "| bwnfsd |"
echo "+========+"
cd $TMP
tar xzvf $CWD/bwnfsd.tar.gz
cd bwnfsd
make linux
strip bwnfsd
cat bwnfsd > $PKG/usr/sbin/rpc.bwnfsd

echo "+===============+"
echo "| nntp-1.5.11tp |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/nntp-1.5.11tp-linux.tar.gz
cd nntp.1.5.11t
zcat $CWD/nntp-1.5.11tp-linux.diff.gz | patch -p1
make server
cd server
strip nntpd
cat nntpd > $PKG/usr/sbin/in.nntpd
cat $CWD/nntpd.8 | gzip -9c > $PKG/usr/man/man8/in.nntpd.8.gz
cd ..
make client
cd inews
strip inews
cat inews > $PKG/usr/bin/inews-nntp

echo "+==============+"
echo "| pop3d-1.00.4 |"
echo "+==============+"
cd $TMP
tar xzvf $CWD/pop3d-1.00.4.tar.gz
cd pop3d
make
strip pop3d
cat pop3d > $PKG/usr/sbin/in.pop3d
gzip -9c pop3d.man > $PKG/usr/man/man8/in.pop3d.8.gz

echo "+===========+"
echo "| ncftp 183 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/ncftp183.tar.gz
cd ncftp183/readline-2.0
configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cd ..
make
strip ncftp
cat ncftp > $PKG/usr/bin/ncftp
cat ncftp.1 | gzip -9c > $PKG/usr/man/man1/ncftp.1.gz

echo "+============================================+"
echo "| Telnetd with LD_ variable security patches |"
echo "+============================================+"
cd $TMP
tar xzvf $CWD/libtelnet.tar.gz
tar xzvf $CWD/telnetd.tar.gz
( cd libtelnet ; make )
cd telnetd 
make
strip telnetd
cat telnetd > $PKG/usr/sbin/in.telnetd
cat telnetd.8 | gzip -9c > $PKG/usr/man/man8/telnetd.8.gz
cat in.telnetd.8 | gzip -9c > $PKG/usr/man/man8/in.telnetd.8.gz
cat issue.net.5 | gzip -9c > $PKG/usr/man/man5/issue.net.5.gz

# Build the package:
cd $PKG
tar czvf $TMP/tcpip.tgz .

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/NetKit-A-0.10
  rm -rf $TMP/NetKit-B-0.06
  rm -rf $TMP/bwnfsd
  rm -rf $TMP/fwhois-1.00
  rm -rf $TMP/libtelnet
  rm -rf $TMP/ipfwadm-2.0
  rm -rf $TMP/ncftp183
  rm -rf $TMP/nntp.1.5.11t
  rm -rf $TMP/pcnfsd_linux2
  rm -rf $TMP/pop3d
  rm -rf $TMP/popclient-3.0b4
  rm -rf $TMP/ruptime
  rm -rf $TMP/rwhod
  rm -rf $TMP/telnetd
  rm -rf $TMP/telnetsnoopd
  rm -rf $TMP/ttysnoop-0.11
  rm -rf $TMP/wu-ftpd-2.4
  rm -rf $TMP/yp-clients-2.1
  rm -rf $TMP/ypserv-0.16
  rm -rf $PKG
fi
