#!/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
( 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 "| net-tools-1.1.79 |"
echo "+==================+"
cd $TMP
tar xzvf $CWD/net-tools-1.1.79.tar.gz
cd net-tools
zcat $CWD/net-tools-1.1.79.diff.gz | patch
touch config.h
make
cat arp > $PKG/sbin/arp
cat ifconfig > $PKG/sbin/ifconfig
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
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 ifconfig.8 netstat.8 plipconfig.8 rarp.8 route.8 slattach.8 ; do
  cat $page | gzip -9c > $PKG/usr/man/man8/$page.gz
done

echo "+===============+"
echo "| NetKit-B-0.05 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/NetKit-B-0.05.tar.gz
cd NetKit-B-0.05
tar xzvf $CWD/arpa.tar.gz # old <arpa/telnet.h> needed by the source
zcat $CWD/NetKit-B-0.05.diff.gz | patch
make
cd finger
cat finger > $PKG/usr/bin/finger
cat finger.1 | gzip -9c > $PKG/usr/man/man1/finger.1.gz
cd ../fingerd
cat fingerd > $PKG/usr/sbin/in.fingerd
cat fingerd.8 | gzip -9c > $PKG/usr/man/man8/in.fingerd.8.gz
cd ../ftp
cat ftp > $PKG/bin/ftp
cat ftp.1 | gzip -9c > $PKG/usr/man/man1/ftp.1.gz
cd ../inetd
cat inetd > $PKG/usr/sbin/inetd
cat inetd.8 | gzip -9c > $PKG/usr/man/man8/inetd.8.gz
cd ../ping
cat ping > $PKG/bin/ping
cat ping.8 | gzip -9c > $PKG/usr/man/man8/ping.8.gz
cd ../portmap
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
cat rexecd > $PKG/usr/sbin/in.rexecd
cat rexecd.8 | gzip -9c > $PKG/usr/man/man8/rexecd.8.gz
cd ../rlogin
cat rlogin > $PKG/usr/bin/rlogin
cat rlogin.1 | gzip -9c > $PKG/usr/man/man1/rlogin.1.gz
cd ../rlogind
cat rlogind > $PKG/usr/sbin/in.rlogind
cat rlogind.8 | gzip -9c > $PKG/usr/man/man8/in.rlogind.8.gz
cd ../routed
cat routed > $PKG/usr/sbin/routed
cat routed.8 | gzip -9c > $PKG/usr/man/man8/routed.8.gz
cd ../rpc.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
cat rwalld > $PKG/usr/sbin/rpc.rwalld
cat rpc.rwalld.8 | gzip -9c > $PKG/usr/man/man8/rpc.rwalld.8.gz
cd ../rpcgen.new
cat rpcgen > $PKG/usr/bin/rpcgen
cat rpcgen.1 | gzip -9c > $PKG/usr/man/man1/rpcgen.1.gz
cd ../rpcinfo
cat rpcinfo > $PKG/usr/sbin/rpcinfo
cat rpcinfo.8 | gzip -9c > $PKG/usr/man/man8/rpcinfo.8.gz
cd ../rsh
cat rsh > $PKG/usr/bin/rsh
cat rsh.1 | gzip -9c > $PKG/usr/man/man1/rsh.1.gz
cd ../rshd
cat rshd > $PKG/usr/sbin/in.rshd
cat rshd.8 | gzip -9c > $PKG/usr/man/man8/in.rshd.8.gz
cd ../rusers
cat rusers > $PKG/usr/bin/rusers
cat rusers.1 | gzip -9c > $PKG/usr/man/man1/rusers.1.gz
cd ../rwall
cat rwall > $PKG/usr/bin/rwall
cat rwall.1 | gzip -9c > $PKG/usr/man/man1/rwall.1.gz
cd ../rwho
cat rwho > $PKG/usr/bin/rwho
cat rwho.1 | gzip -9c > $PKG/usr/man/man1/rwho.1.gz
cd ../sliplogin
cat sliplogin > $PKG/usr/sbin/sliplogin
cat sliplogin.8 | gzip -9c > $PKG/usr/man/man8/sliplogin.8.gz
cd ../talk
cat talk > $PKG/usr/bin/talk
cat talk.1 | gzip -9c > $PKG/usr/man/man1/talk.1.gz
cd ../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
cd ../telnetd
cat telnetd > $PKG/usr/sbin/in.telnetd
cat telnetd.8 | gzip -9c > $PKG/usr/man/man8/in.telnetd.8.gz
cd ../tftp
cat tftp > $PKG/usr/bin/tftp
cat tftp.1 | gzip -9c > $PKG/usr/man/man1/tftp.1.gz
cd ../tftpd
cat tftpd > $PKG/usr/sbin/in.tftpd
cat tftpd.8 | gzip -9c > $PKG/usr/man/man8/in.tftpd.8.gz
cd ../timed/timed
cat timed > $PKG/usr/sbin/in.timed
cat timed.8 | gzip -9c > $PKG/usr/man/man8/timed.8.gz
cd ../timedc
cat timedc > $PKG/usr/sbin/timedc
cat timedc.8 | gzip -9c > $PKG/usr/man/8/timedc.8.gz

echo "+===========+"
echo "| portmap_3 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/portmap_3.tar.gz
cd portmap_3
make
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

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 "| popclient-2.21 |"
echo "+================+"
cd $TMP
tar xzvf $CWD/popclient-2.21.tar.gz
cd pop
make
strip popclient
cat popclient > $PKG/usr/bin/popclient
cat popclient.1L | 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
cat ruptime > $PKG/usr/bin/ruptime
cat ruptime.1 | gzip -9c > $PKG/usr/man/man1/ruptime.1.gz

echo "+===============+"
echo "| NetKit-A-0.06 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/NetKit-A-0.06.tar.gz
cd NetKit-A-0.06
make
cd ytalk-3.0.1
cp Makefile.old Makefile
make CFLAGS=-O2 LDFLAGS=-s
cat ytalk > $PKG/usr/bin/ytalk
cat ytalk.1 | gzip -9c > $PKG/usr/man/man1/ytalk.1.gz
cd ../pidentd-2.2
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_wrapper-6.3
strip tcpd safe_finger try try-from
cat tcpd > $PKG/usr/sbin/tcpd
make clean
make REAL_DAEMON_DIR=/usr/sbin/real-daemon-dir linux
strip tcpd safe_finger try try-from
cat tcpd > $PKG/usr/sbin/real-daemon-dir/tcpd
cat safe_finger > $PKG/usr/sbin/real-daemon-dir/safe_finger
cat try > $PKG/usr/sbin/real-daemon-dir/try
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
cd ../traceroute-4.4BSD
cc -s -o traceroute traceroute.o
cat traceroute > $PKG/usr/bin/traceroute
cat traceroute.8 | gzip -9c > $PKG/usr/man/man8/traceroute.8.gz

echo "+================+"
echo "| nfs-server-2.1 |"
echo "+================+"
cd $TMP
tar xzvf $CWD/nfs-server-2.1.tar.gz
cd nfs-server-2.1
zcat $CWD/nfs-server-2.1.diff.gz | patch
./configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
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

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-1.6 |"
echo "+================+"
cd $TMP
tar xzvf $CWD/yp-clients-1.6.tar.gz
cd yp-clients-1.6
make
strip ypbind domainname/domainname ypcat/ypcat ypmatch/ypmatch \
  yppasswd/yppasswd yppoll/yppoll ypset/ypset ypwhich/ypwhich
cat 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.13.1 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/ypserv-0.13.1.tar.gz
cd ypserv-0.13.1
make linux
strip dbload dbcat dbnext ypserv
cat ypserv > $PKG/usr/sbin/ypserv
cat ypMakefile > $PKG/var/yp/Makefile
cat dbload > $PKG/usr/lib/yp/dbload
cat mknetid > $PKG/usr/lib/yp/mknetid

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
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 "| netdate-1.16 |"
echo "+==============+"
cd $TMP
tar xzvf $CWD/netdate-1.16.tar.gz
cd netdate
make
strip netdate
cat netdate > $PKG/usr/sbin/netdate
cat netdate.8 | gzip -9c > $PKG/usr/man/man8/netdate.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
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
cat pop3d > $PKG/usr/sbin/in.pop3d
mkdir -p $PKG/usr/man/preformat/cat8
#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
cat ncftp > $PKG/usr/bin/ncftp
cat ncftp.1 | gzip -9c > $PKG/usr/man/man1/ncftp.1.gz

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

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/net-tools
  rm -rf $TMP/NetKit-B-0.05
  rm -rf $TMP/NetKit-A-0.06
  rm -rf $TMP/nfs-server-2.1
  rm -rf $TMP/finger
  rm -rf $TMP/portmap_3
  rm -rf $TMP/fwhois-1.00
  rm -rf $TMP/pop
  rm -rf $TMP/wu-ftpd-2.4
  rm -rf $TMP/telnetsnoopd
  rm -rf $TMP/ttysnoop-0.11
  rm -rf $TMP/netdate
  rm -rf $TMP/pcnfsd_linux2
  rm -rf $TMP/rwhod
  rm -rf $TMP/bwnfsd
  rm -rf $TMP/nntp.1.5.11t
  rm -rf $TMP/pop3d
  rm -rf $TMP/ncftp183
  rm -rf $TMP/ruptime
  rm -rf $TMP/yp-clients-1.6
  rm -rf $TMP/ypserv-0.13.1
  rm -rf $PKG
fi
