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

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/_bin.tar.gz

echo "+=========+"
echo "| at-2.9b |"
echo "+=========+"
cd $TMP
tar xzvf $CWD/at-2.9b.tar.gz
cd at
./Configure
make
strip at atrun
cat at > $PKG/usr/bin/at
cat atrun > $PKG/usr/lib/atrun
for target in batch.1.gz atq.1.gz atrm.1.gz ; do
  cat batch.1 | gzip -9c > $PKG/usr/man/man1/$target
done
sed -f subs.sed < at.man | gzip -9c > $PKG/usr/man/man1/at.1.gz
sed -f subs.sed < atrun.man | gzip -9c > $PKG/usr/man/man8/atrun.8.gz

echo "+=================+"
echo "| apmd_3.0beta3-2 |"
echo "+=================+"
cd $TMP
tar xzvf $CWD/apmd_3.0beta3-2.tar.gz
cd apmd
make
strip apm apmd xapm
cat apm > $PKG/usr/bin/apm
chown root.bin $PKG/usr/bin/apm
chmod 755 $PKG/usr/bin/apm
cat apm.1 | gzip -9c > $PKG/usr/man/man1/apm.1.gz
cat apmd > $PKG/usr/sbin/apmd
chown root.bin $PKG/usr/sbin/apmd
chmod 755 $PKG/usr/sbin/apmd
cat apmd.8 | gzip -9c > $PKG/usr/man/man8/apmd.8.gz
mkdir -p $PKG/usr/X11R6/bin
chgrp bin $PKG/usr/X11R6/bin
cat xapm > $PKG/usr/X11R6/bin/xapm
chown root.bin $PKG/usr/X11R6/bin/xapm
chmod 755 $PKG/usr/X11R6/bin/xapm
mkdir -p $PKG/usr/X11R6/man/man1
cat xapm.1 | gzip -9c > $PKG/usr/X11R6/man/man1/xapm.1.gz
cat libapm.a > $PKG/usr/lib/libapm.a
cat apm.h > $PKG/usr/include/apm.h
mkdir -p $PKG/usr/doc/apmd_3.0beta3
cp -a ANNOUNCE LSM README README.transfer $PKG/usr/doc/apmd_3.0beta3
chown root.root $PKG/usr/doc/apmd_3.0beta3/*
chmod 644 $PKG/usr/doc/apmd_3.0beta3/*

echo "+===========+"
echo "| asapm-2.2 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/asapm-2.2.tar.gz
cd asapm-2.2
make
strip asapm
cat asapm > $PKG/usr/X11R6/bin/asapm
chown root.bin $PKG/usr/X11R6/bin/asapm
chmod 755 $PKG/usr/X11R6/bin/asapm
cat asapm.man | gzip -9c > $PKG/usr/X11R6/man/man1/asapm.1.gz
mkdir -p $PKG/usr/doc/asapm-2.2
cp -a LICENSE NOTES README TODO $PKG/usr/doc/asapm-2.2
chown root.root $PKG/usr/doc/asapm-2.2/*
chmod 644 $PKG/usr/doc/asapm-2.2/*

echo "+=============+"
echo "| eject-1.5-5 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/eject_1.5.orig.tar.gz
cd eject-1.5.orig
zcat $CWD/eject_1.5-5.diff.gz | patch -p1
zcat $CWD/eject_1.5.linux22diff.gz | patch
make
cat eject > $PKG/usr/bin/eject
cat eject.1 | gzip -9c > $PKG/usr/man/man1/eject.1.gz
mkdir -p $PKG/usr/doc/eject-1.5
cp -a ChangeLog README TODO $PKG/usr/doc/eject-1.5
chmod 644 $PKG/usr/doc/eject-1.5/*
chown root.root $PKG/usr/doc/eject-1.5/*

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

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

echo "+============+"
echo "| makewhatis |"
echo "+============+"
cd $TMP
tar xzvf $CWD/makewhatis.tar.gz
cd makewhatis
cat makewhatis > $PKG/usr/sbin/makewhatis
cat makewhatis.1 | gzip -9c > $PKG/usr/man/man1/makewhatis.1.gz

echo "+========+"
echo "| gencat |"
echo "+========+"
cd $TMP
tar xzvf $CWD/gencat.tar.gz
cd gencat
pmake
strip gencat
cat gencat > $PKG/usr/bin/gencat

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

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
cat ed.1 | gzip -9c > $PKG/usr/man/man1/ed.1.gz
cat ed.info | gzip -9c > $INFO/ed.info.gz

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
cat compress.1 | gzip -9c > $PKG/usr/man/man1/compress.1.gz
echo '.so man1/compress.1' | gzip -9c > $PKG/usr/man/man1/uncompress.1.gz

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
zcat $CWD/dcron22.diff.gz | patch
zcat $CWD/dcron.tmpdir.diff.gz | patch
make
cat crond > $PKG/usr/sbin/crond
cat crontab > $PKG/usr/bin/crontab
cat crontab.1 | gzip -9c > $PKG/usr/man/man1/crontab.1.gz
cat crond.8 | gzip -9c > $PKG/usr/man/man8/crond.8.gz

# IMHO, you'd have to be crazy to run this on your DOS partitions.  Use
# DOS defrag or something like that...  it's getting commented out of the
# SlackBuild, because I suspect it will do really bad things to FAT32
# volumes, and I don't like leaving traps like that laying around.
#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
#chown root.bin $PKG/sbin/dosfsck
#chmod 755 $PKG/sbin/dosfsck
#cat dosfsck.8 | gzip -9c > $PKG/usr/man/man8/dosfsck.8.gz

echo "+===========+"
echo "| file-3.24 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/file-3.24.tar.gz
cd file-3.24
./configure --prefix=/usr --sysconfdir=/etc --datadir=/etc
mkdir -p $PKG/usr/doc/file-3.24
cp -a LEGAL.NOTICE README $PKG/usr/doc/file-3.24
chown root.root $PKG/usr/doc/file-3.24/*
chmod 644 $PKG/usr/doc/file-3.24/*
make
strip file
cat file > $PKG/usr/bin/file
cat magic > $PKG/etc/magic
cat file.1 | gzip -9c > $PKG/usr/man/man1/file.1.gz
cat magic.4 | gzip -9c > $PKG/usr/man/man4/magic.4.gz

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
cat indent.1 | gzip -9c > $PKG/usr/man/man1/indent.1.gz
cat indent.info | gzip -9c > $INFO/indent.info.gz

echo "+============+"
echo "| gawk-3.0.3 |"
echo "+============+"
cd $TMP
tar xzvf $CWD/gawk-3.0.3.tar.gz
cd gawk-3.0.3
./configure linux --prefix=/usr
make CC=gcc CFLAGS=-O2 LDFLAGS=-s
strip gawk
cat gawk > $PKG/usr/bin/gawk
cat awklib/igawk > $PKG/usr/bin/igawk
cat awklib/grcat > $PKG/usr/libexec/awk/grcat
cat awklib/pwcat > $PKG/usr/libexec/awk/pwcat
cat doc/gawk.1 | gzip -9c > $PKG/usr/man/man1/gawk.1.gz
cat doc/igawk.1 | gzip -9c > $PKG/usr/man/man1/igawk.1.gz
cat doc/gawk.info | gzip -9c > $PKG/usr/info/gawk.info.gz
mkdir $PKG/usr/doc/gawk-3.0.3
cp -a ACKNOWLEDGMENT COPYING FUTURES INSTALL LIMITATIONS NEWS \
PORTS PROBLEMS README $PKG/usr/doc/gawk-3.0.3
chown -R root.root $PKG/usr/doc/gawk-3.0.3
cd awklib
cp -a *.awk $PKG/usr/share/awk
cd eg/lib
cp -a *.awk $PKG/usr/share/awk
chown root.root $PKG/usr/share/awk/*

echo "+==========+"
echo "| man-1.5b |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/man-1.5b.tar.gz
cd man-1.5b
./configure +lang all
make
cd src
cat man > $PKG/usr/bin/man
cat apropos > $PKG/usr/bin/apropos
#cat makewhatis > $PKG/usr/lib/makewhatis
cat mwi > $PKG/usr/bin/mwi
cd ../man/en
for page in apropos.1 man.1 whatis.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cat man.conf.5 | gzip -9c > $PKG/usr/man/man5/man.conf.5.gz

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

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

echo "+===========+"
echo "| rpm2targz |"
echo "+===========+"
cd $TMP
cc -o rpmoffset $CWD/rpmoffset.c
strip rpmoffset
cat rpmoffset > $PKG/usr/bin/rpmoffset
rm rpmoffset
cat $CWD/rpm2targz > $PKG/usr/bin/rpm2targz

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
# Use the sed man page from BSD...
cat $CWD/sed.1.gz > $PKG/usr/man/man1/sed.1.gz

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
for page in shar.1 unshar.1 uuencode.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
echo '.so man1/uuencode.1' | gzip -9c > $PKG/usr/man/man1/uudecode.1.gz
cat uuencode.5 | gzip -9c > $PKG/usr/man/man5/uuencode.5.gz
cat sharutils.info | gzip -9c > $INFO/sharutils.info.gz

echo "+===============+"
echo "| splitvt-1.6.3 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/splitvt-1.6.3.tar.gz
cd splitvt-1.6.3
./Configure
make
cat splitvt > $PKG/usr/bin/splitvt
cat splitvt.1 | gzip -9c > $PKG/usr/man/man1/splitvt.1.gz
mkdir -p $PKG/usr/doc/splitvt
cp -a examples ANNOUNCE CHANGES NOTES README TODO $PKG/usr/doc/splitvt
chown -R root.root $PKG/usr/doc/splitvt

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

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

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
cat which.1 | gzip -9c > $PKG/usr/man/man1/which.1.gz

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
for page in fiz.1 zoo.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done

# Strip everything for good measure:

strip $PKG/bin/* $PKG/usr/bin/* $PKG/usr/sbin/*

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

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/pwd
  rm -rf $TMP/ed-0.2
  rm -rf $TMP/at
  rm -rf $TMP/asapm-2.2
  rm -rf $TMP/apmd
  rm -rf $TMP/banners
  rm -rf $TMP/bpe
  rm -rf $TMP/dcron
  rm -rf $TMP/eject-1.5.orig
  rm -rf $TMP/file-3.24
  rm -rf $TMP/hdparm-3.5
  rm -rf $TMP/indent-1.9.1
  rm -rf $TMP/gawk-3.0.3
  rm -rf $TMP/gencat
  rm -rf $TMP/lha-1.00
  rm -rf $TMP/man-1.5b
  rm -rf $TMP/makewhatis
  rm -rf $TMP/mkdosfs-0.4
  rm -rf $TMP/patch-2.1b
  rm -rf $TMP/sed-2.05
  rm -rf $TMP/sharutils-4.1
  rm -rf $TMP/time-1.7
  rm -rf $TMP/tput-1.0
  rm -rf $TMP/unarj230
  rm -rf $TMP/which
  rm -rf $TMP/zoo-2.10
  rm -rf $TMP/compress
  rm -rf $TMP/splitvt-1.6.3
  rm -rf $PKG
fi