#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-bin
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
tar xzvf $CWD/_bin.tar.gz

# 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 "| fileutils-3.12+color-ls-3.12.0.2 |"
echo "+==================================+"
cd $TMP
tar xzvf $CWD/fileutils-3.12.tar.gz
cd fileutils-3.12
zcat $CWD/color-ls-3.12.0.2.patch.gz | patch -p1
# Next 3 define ETC_DIR to be /etc 
cd src
zcat $CWD/fileutils-3.12.diff.gz | patch
cd ..
CFLAGS=-O2 LDFLAGS=-s ./configure --prefix=/usr
make
cd src
cat chgrp > $PKG/bin/chgrp
cat chmod > $PKG/bin/chmod
cat chown > $PKG/bin/chown
cat cp > $PKG/bin/cp
cat dd > $PKG/bin/dd
cat df > $PKG/bin/df
cat dircolors > $PKG/bin/dircolors
cat du > $PKG/bin/du
cat ginstall > $PKG/usr/bin/ginstall
cat ln > $PKG/bin/ln
cat ls > $PKG/bin/ls
cat mkdir > $PKG/bin/mkdir
cat mkfifo > $PKG/bin/mkfifo
cat mknod > $PKG/bin/mknod
cat mv > $PKG/bin/mv
cat rm > $PKG/bin/rm
cat rmdir > $PKG/bin/rmdir
# cat sync > $PKG/bin/sync # Slackware uses the util-linux assembly sync
cat touch > $PKG/bin/touch
cd ../man
man2gz chgrp.1 $PKG/usr/man/preformat/cat1/chgrp.1.gz $SRC/usr/man/man1/chgrp.1
man2gz chmod.1 $PKG/usr/man/preformat/cat1/chmod.1.gz $SRC/usr/man/man1/chmod.1
man2gz chown.1 $PKG/usr/man/preformat/cat1/chown.1.gz $SRC/usr/man/man1/chown.1
man2gz cp.1 $PKG/usr/man/preformat/cat1/cp.1.gz $SRC/usr/man/man1/cp.1
man2gz dd.1 $PKG/usr/man/preformat/cat1/dd.1.gz $SRC/usr/man/man1/dd.1
man2gz df.1 $PKG/usr/man/preformat/cat1/df.1.gz $SRC/usr/man/man1/df.1
man2gz dircolors.1 $PKG/usr/man/preformat/cat1/dircolors.1.gz $SRC/usr/man/man1/dircolors.1
man2gz du.1 $PKG/usr/man/preformat/cat1/du.1.gz $SRC/usr/man/man1/du.1
man2gz install.1 $PKG/usr/man/preformat/cat1/install.1.gz $SRC/usr/man/man1/install.1
man2gz ln.1 $PKG/usr/man/preformat/cat1/ln.1.gz $SRC/usr/man/man1/ln.1
man2gz ls.1 $PKG/usr/man/preformat/cat1/ls.1.gz $SRC/usr/man/man1/ls.1
man2gz mkdir.1 $PKG/usr/man/preformat/cat1/mkdir.1.gz $SRC/usr/man/man1/mkdir.1
man2gz mkfifo.1 $PKG/usr/man/preformat/cat1/mkfifo.1.gz $SRC/usr/man/man1/mkfifo.1
man2gz mknod.1 $PKG/usr/man/preformat/cat1/mknod.1.gz $SRC/usr/man/man1/mknod.1
man2gz mv.1 $PKG/usr/man/preformat/cat1/mv.1.gz $SRC/usr/man/man1/mv.1
man2gz rm.1 $PKG/usr/man/preformat/cat1/rm.1.gz $SRC/usr/man/man1/rm.1
man2gz rmdir.1 $PKG/usr/man/preformat/cat1/rmdir.1.gz $SRC/usr/man/man1/rmdir.1
man2gz touch.1 $PKG/usr/man/preformat/cat1/touch.1.gz $SRC/usr/man/man1/touch.1
cd ../doc
cat fileutils.info | gzip -9c > $INFO/fileutils.info.gz
cp fileutils.texi $TEX
cp perm.texi $TEX

echo "+=====+"
echo "| bpe |"
echo "+=====+"
cd $TMP
tar xzvf $CWD/bpe.tar.gz
cd bpe
mklinux
strip bpe
cat bpe > $PKG/usr/bin/bpe
man2gz bpe.1 $PKG/usr/man/preformat/cat1/bpe.1.gz $SRC/usr/man/man1/bpe.1

echo "+========+"
echo "| ed-0.2 |"
echo "+========+"
cd $TMP
tar xzvf $CWD/ed-0.2.tar.gz
cd ed-0.2
CFLAGS=-O2 LDFLAGS=-s ./configure --prefix=/usr
make
cat ed > $PKG/bin/ed
man2gz ed.1 $PKG/usr/man/preformat/cat1/ed.1.gz $SRC/usr/man/man1/ed.1
cat ed.info | gzip -9c > $INFO/ed.info.gz
cp ed.texinfo $TEX/ed.texi

echo "+========+"
echo "| at-2.7 |"
echo "+========+"
cd $TMP
tar xzvf $CWD/at-2.7.tar.gz
cd at
zcat $CWD/at-2.7.diff.gz | patch
make
strip at atrun
cat at > $PKG/usr/bin/at
cat atrun > $PKG/usr/lib/atrun
man2gz at.man $PKG/usr/man/preformat/cat1/at.1.gz $SRC/usr/man/man1/at.1
( cd $SRC/usr/man/man1 ; ln -sf at.1 batch.1 )
( cd $SRC/usr/man/man1 ; ln -sf at.1 atq.1 )
( cd $SRC/usr/man/man1 ; ln -sf at.1 atrm.1 )
man2gz atrun.man $PKG/usr/man/preformat/cat1/atrun.1.gz $SRC/usr/man/man1/atrun.1

echo "+==========+"
echo "| compress |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/compress.tar.gz
cd compress
zcat $CWD/compress.diff.gz | patch
make
cat compress > $PKG/usr/bin/compress
man2gz compress.1 $PKG/usr/man/preformat/cat1/compress.1.gz $SRC/usr/man/man1/compress.1
( cd $SRC/usr/man/man1 ; ln -sf compress.1 uncompress.1 )

echo "+=========+"
echo "| banners |"
echo "+=========+"
cd $TMP
tar xzvf $CWD/banners.tar.gz
cd banners
make
cat bban > $PKG/usr/bin/bban
cat sysvbanner > $PKG/usr/bin/sysvbanner

echo "+===========+"
echo "| dcron 2.2 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/dcron22.tar.gz
cd dcron
make
cat crond > $PKG/usr/sbin/crond
cat crontab > $PKG/usr/bin/crontab
man2gz crontab.1 $PKG/usr/man/preformat/cat1/crontab.1.gz $SRC/usr/man/man1/crontab.1
man2gz crond.8 $PKG/usr/man/preformat/cat8/crond.8.gz $SRC/usr/man/man8/crond.8

echo "+===========+"
echo "| dosfsck.1 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/dosfsck.1.tar.gz
cd dosfsck
zcat $CWD/dosfsck.1.diff.gz | patch
make
strip dosfsck
cat dosfsck > $PKG/sbin/dosfsck
man2gz dosfsck.8 $PKG/usr/man/preformat/cat8/dosfsck.8.gz $SRC/usr/man/man8/dosfsck.8

echo "+=====================+"
echo "| elvis-1.8pl3.tar.gz |"
echo "+=====================+"
cd $TMP
tar xzvf $CWD/elvis-1.8pl3.tar.gz
cd elvis
zcat $CWD/elvis-1.8pl3.diff.gz | patch
make
strip ctags elvis elvprsv elvrec fmt ref
cat ctags > $PKG/usr/bin/ctags
cat elvis > $PKG/usr/bin/elvis
cat elvprsv > $PKG/usr/bin/elvprsv
cat elvrec > $PKG/usr/bin/elvrec
cat fmt > $PKG/usr/bin/fmt
cat ref > $PKG/usr/bin/ref
cd doc
man2gz ctags.man $PKG/usr/man/preformat/cat1/ctags.1.gz $SRC/usr/man/man1/ctags.1
man2gz elvis.man $PKG/usr/man/preformat/cat1/elvis.1.gz $SRC/usr/man/man1/elvis.1
man2gz elvprsv.man $PKG/usr/man/preformat/cat1/elvprsv.1.gz $SRC/usr/man/man1/elvprsv.1
man2gz elvrec.man $PKG/usr/man/preformat/cat1/elvrec.1.gz $SRC/usr/man/man1/elvrec.1
man2gz fmt.man $PKG/usr/man/preformat/cat1/fmt.1.gz $SRC/usr/man/man1/fmt.1
man2gz ref.man $PKG/usr/man/preformat/cat1/ref.1.gz $SRC/usr/man/man1/ref.1

echo "+===========+"
echo "| file-3.14 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/file-3.14.tar.gz
cd file-3.14
zcat $CWD/file-3.14b.diff.gz | patch -p1
make
strip file
cat file > $PKG/usr/bin/file
cat magic > $PKG/etc/magic
man2gz file.1 $PKG/usr/man/preformat/cat1/file.1.gz $SRC/usr/man/man1/file.1
man2gz magic.4 $PKG/usr/man/preformat/cat4/magic.4.gz $SRC/usr/man/man1/magic.4

echo "+==============+"
echo "| indent-1.9.1 |"
echo "+==============+"
cd $TMP
tar xzvf $CWD/indent-1.9.1.tar.gz
cd indent-1.9.1
./configure --prefix=/usr
zcat $CWD/indent-1.9.1.diff.gz | patch
make
cat indent > $PKG/usr/bin/indent
man2gz indent.1 $PKG/usr/man/preformat/cat1/indent.1.gz $SRC/usr/man/man1/indent.1
cat indent.info | gzip -9c > $INFO/indent.info.gz
cp indent.texinfo $TEX/indent.texi

echo "+=============+"
echo "| gawk-2.15.5 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/gawk-2.15.5.tar.gz
cd gawk-2.15.5
./configure linux
zcat $CWD/gawk-2.15.5.diff.gz | patch
make
cat gawk > $PKG/usr/bin/gawk
man2gz gawk.1 $PKG/usr/man/preformat/cat1/gawk.1.gz $SRC/usr/man/man1/gawk.1
makeinfo gawk.texi
cat gawk.info | gzip -9c > $INFO/gawk.info.gz
cp gawk.texi $TEX/gawk.texi

echo "+==========+"
echo "| man-1.4c |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/man-1.4c.tar.gz
cd man-1.4c
configure +lang all
make
cd src
cat man > $PKG/usr/bin/man
cd ../man/en
man2gz apropos.1 $PKG/usr/man/preformat/cat1/apropos.1.gz $SRC/usr/man/man1/apropos.1
man2gz man.1 $PKG/usr/man/preformat/cat1/man.1.gz $SRC/usr/man/man1/man.1
man2gz man.config.5 $PKG/usr/man/preformat/cat5/man.config.5.gz $SRC/usr/man/man5/man.config.5
man2gz whatis.1 $PKG/usr/man/preformat/cat1/whatis.1.gz $SRC/usr/man/man1/whatis.1

echo "+=============+"
echo "| mkdosfs-0.2 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/mkdosfs-0.2.tar.gz
cd mkdosfs-0.2
zcat $CWD/mkdosfs-0.2.diff.gz | patch
make
cat mkdosfs > $PKG/sbin/mkdosfs
man2gz mkdosfs.8 $PKG/usr/man/preformat/cat8/mkdosfs.8.gz $SRC/usr/man/man8/mkdosfs.8

echo "+================+"
echo "| modules-1.1.87 |"
echo "+================+"
cd $TMP
tar xzvf $CWD/modules-1.1.87.tar.gz
cd modules-1.1.87
zcat $CWD/modules-1.1.87.diff.gz | patch -p1
make
cd insmod
cat insmod > $PKG/sbin/insmod
man2gz insmod.1 $PKG/usr/man/preformat/cat1/insmod.1.gz $SRC/usr/man/man1/insmod.1
man2gz ksyms.1 $PKG/usr/man/preformat/cat1/ksyms.1.gz $SRC/usr/man/man1/ksyms.1
man2gz lsmod.1 $PKG/usr/man/preformat/cat1/lsmod.1.gz $SRC/usr/man/man1/lsmod.1
man2gz modules.2 $PKG/usr/man/preformat/cat2/modules.2.gz $SRC/usr/man/man2/modules.2
man2gz rmmod.1 $PKG/usr/man/preformat/cat1/rmmod.1.gz $SRC/usr/man/man1/rmmod.1
cd ../genksyms
cat genksyms > $PKG/sbin/genksyms
man2gz genksyms.8 $PKG/usr/man/preformat/cat8/genksyms.8.gz $SRC/usr/man/man8/genksyms.8
cd ../depmod
cat modprobe > $PKG/sbin/modprobe
man2gz depmod.1 $PKG/usr/man/preformat/cat1/depmod.1.gz $SRC/usr/man/man1/depmod.1

echo "+===========+"
echo "| patch-2.1 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/patch-2.1.tar.gz
cd patch-2.1
configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cat patch > $PKG/usr/bin/patch
man2gz patch.man $PKG/usr/man/preformat/cat1/patch.1.gz $SRC/usr/man/man1/patch.1

echo "+==========+"
echo "| sed-2.05 |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/sed-2.05.tar.gz
cd sed-2.05
./configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cat sed > $PKG/usr/bin/sed

echo "+===============+"
echo "| sharutils-4.1 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/sharutils-4.1.tar.gz
cd sharutils-4.1
CFLAGS=-O2 LDFLAGS=-s ./configure
make
cat shar > $PKG/usr/bin/shar
cat unshar > $PKG/usr/bin/unshar
cat uuencode > $PKG/usr/bin/uuencode
cat uudecode > $PKG/usr/bin/uudecode
man2gz shar.1 $PKG/usr/man/preformat/cat1/shar.1.gz $SRC/usr/man/man1/shar.1
man2gz unshar.1 $PKG/usr/man/preformat/cat1/unshar.1.gz $SRC/usr/man/man1/unshar.1
man2gz uuencode.1 $PKG/usr/man/preformat/cat1/uuencode.1.gz $SRC/usr/man/man1/uuencode.1
( cd $SRC/usr/man/man1 ; ln -sf uuencode.1 uudecode.1 )
man2gz uuencode.5 $PKG/usr/man/preformat/cat5/uuencode.5.gz $SRC/usr/man/man5/uuencode.5
cat sharutils.info | gzip -9c > $INFO/sharutils.info.gz
cp sharutils.texi $TEX/sharutils.texi

echo "+===============+"
echo "| splitvt-1.6.1 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/splitvt-1.6.1.tar.gz
cd splitvt-1.6.1
Configure
make
cat splitvt > $PKG/usr/bin/splitvt
man2gz splitvt.1 $PKG/usr/man/preformat/cat1/splitvt.1.gz $SRC/usr/man/man1/splitvt.1

echo "+==========+"
echo "| time-1.6 |"
echo "+==========+"
cd $TMP
tar xvzf $CWD/time-1.6.tar.gz
cd time-1.6
configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cat time > $PKG/usr/bin/time
cat time.info | gzip -9c > $INFO/time.info.gz
cp time.texi $TEX/time.texi

echo "+==========+"
echo "| tput-1.0 |"
echo "+==========+"
cd $TMP
tar xvzf $CWD/tput-1.0.tar.gz
cd tput-1.0
./configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cat tput > $PKG/usr/bin/tput
cat tput.info | gzip -9c > $INFO/tput.info.gz
cp tput.texinfo $TEX/tput.texi

echo "+==========+"
echo "| unarj230 |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/unarj230.tar.gz
cd unarj230
zcat $CWD/unarj230.diff.gz | patch
make
strip unarj
cat unarj > $PKG/usr/bin/unarj

echo "+=======+"
echo "| which |"
echo "+=======+"
cd $TMP
tar xzvf $CWD/which.tar.gz
cd which
make
cat which > $PKG/usr/bin/which
man2gz which.1 $PKG/usr/man/preformat/cat1/which.1.gz $SRC/usr/man/man1/which.1

echo "+==========+"
echo "| zoo-2.10 |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/zoo-2.10.tar.gz
cd zoo-2.10
zcat $CWD/zoo-2.10.diff.gz | patch
make generic
strip fiz
strip zoo
cat fiz > $PKG/usr/bin/fiz
cat zoo > $PKG/usr/bin/zoo
man2gz fiz.1 $PKG/usr/man/preformat/cat1/fiz.1.gz $SRC/usr/man/man1/fiz.1
man2gz zoo.1 $PKG/usr/man/preformat/cat1/zoo.1.gz $SRC/usr/man/man1/zoo.1

echo "+================+"
echo "| xiafspgm-0.8.1 |"
echo "+================+"
cd $TMP
tar xzvf $CWD/xiafspgm-0.8.1.tar.gz
cd xiafspgm-0.8.1
make xiafspgm
strip mkxfs xfsck
cat mkxfs > $PKG/sbin/mkxfs
cat xfsck > $PKG/sbin/xfsck
man2gz mkxfs.8 $PKG/usr/man/preformat/cat8/mkxfs.8.gz $SRC/usr/man/man8/mkxfs.8
man2gz xfsck.8 $PKG/usr/man/preformat/cat8/xfsck.8.gz $SRC/usr/man/man8/xfsck.8

#echo "+==============+"
#echo "| shadow-3.3.1 |"
#echo "+==============+"
## Note that the only reason we build the shadow stuff is so that we can use the
## version of 'chfn' that it comes with.  Converting the whole system to use
## shadow passwords is not big on the list of priories right now...
##
## I sure wish I could get userdel/useradd/usermod to work without shadow like
## they used to.
##
#cd $TMP
#tar xzvf $CWD/shadow-3.3.1.tar.gz
#cd shadow-3.3.1
#zcat $CWD/shadow-3.3.1-2.diff.gz | patch
#make chfn
#if [ ! -r chfn ]; then
#  zcat $CWD/shadow-3.3.1-extra.diff.gz | patch
#  make chfn
#fi
#cat chfn > $PKG/usr/bin/chfn
#man2gz chfn.1 $PKG/usr/man/preformat/cat1/chfn.1.gz $SRC/usr/man/man1/chfn.1

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

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/fileutils-3.12
  rm -rf $TMP/pwd
  rm -rf $TMP/ed-0.2
  rm -rf $TMP/at
  rm -rf $TMP/banners
  rm -rf $TMP/bpe
  rm -rf $TMP/dcron
  rm -rf $TMP/dosfsck
  rm -rf $TMP/elvis
  rm -rf $TMP/file-3.9
  rm -rf $TMP/file-3.14
  rm -rf $TMP/indent-1.9.1
  rm -rf $TMP/gawk-2.15.5
  rm -rf $TMP/man-1.4c
  rm -rf $TMP/mkdosfs-0.2
  rm -rf $TMP/modules-1.1.87
  rm -rf $TMP/patch-2.1
  rm -rf $TMP/sed-2.05
  rm -rf $TMP/sharutils-4.1
  rm -rf $TMP/time-1.6
  rm -rf $TMP/tput-1.0
  rm -rf $TMP/unarj230
  rm -rf $TMP/which
  rm -rf $TMP/zoo-2.10
#  rm -rf $TMP/shadow-3.3.1
  rm -rf $TMP/compress
  rm -rf $TMP/splitvt-1.6.1
  rm -rf $TMP/xiafspgm-0.8.1
  rm -rf $PKG
fi