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

echo "+============+"
echo "| mtools-3.8 |"
echo "+============+"
cd $TMP
tar xzvf $CWD/mtools-3.8.tar.gz
cd mtools-3.8
mkdir $PKG/usr/doc/mtools-3.8
cp -a COPYING Changelog README* TODO $PKG/usr/doc/mtools-3.8
chmod 644 $PKG/usr/doc/mtools-3.8/*
chown root.root $PKG/usr/doc/mtools-3.8/*
./configure i486-slackware-linux --prefix=/usr --sysconfdir=/etc
make CFLAGS="-O2 -m486 -fno-strength-reduce -Wall" LDFLAGS=-s
strip mtools
# For now, we include mtools.conf in _aaa_base.tar.gz, since it requires
# a little bit of editing.
#cat mtools.conf > $PKG/etc/mtools.conf
cat mtools > $PKG/usr/bin/mtools
make mkmanifest
strip mkmanifest
cat mkmanifest > $PKG/usr/bin/mkmanifest
for page in *.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cat mtools.5 | gzip -9c > $PKG/usr/man/man5/mtools.5.gz
makeinfo mtools.texi
for file in mtools.info* ; do
  cat $file | gzip -9c > $PKG/usr/info/$file.gz
done
## install optional scripts:
## No longer done by default, since most of these are for Solaris
## or just plain look like clutter/bloat :^)
#cd scripts
#chmod 755 *
#chown root.bin *
#cp -a * $PKG/usr/bin
#cd $PKG/usr/bin

echo "+=============+"
echo "| fdutils-5.2 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/fdutils-5.2.tar.gz
cd fdutils-5.2
./configure --prefix=/usr --sysconfdir=/etc
make CFLAGS=-O2 LDFLAGS=-s
mkdir $PKG/usr/doc/fdutils-5.2
cp -a COPYING CREDITS INSTALL $PKG/usr/doc/fdutils-5.2
cd doc
cp -a README FAQ.html floppy_formats cmdname $PKG/usr/doc/fdutils-5.2
chmod 644 $PKG/usr/doc/fdutils-5.2/*
install -c -m 644 -o root -g root diskd.1 diskseekd.1 fdrawcmd.1 \
floppycontrol.1 getfdprm.1 makefloppies.1 superformat.1 xdfcopy.1 \
floppymeter.1 fdmount.1 $PKG/usr/man/man1
( cd $PKG/usr/man/man1 ; gzip -9 *.1 )
install -c -m 644 -o root -g root fd.4 $PKG/usr/man/man4
gzip -9 $PKG/usr/man/man4/fd.4
install -c -m 644 -o root -g root setfdprm.1 $PKG/usr/man/man1
gzip -9 $PKG/usr/man/man1/setfdprm.1
mkdir -p $PKG/usr/info
cp -a fdutils.info* $PKG/usr/info
gzip -9 $PKG/usr/info/*.info*
cd ../src
#install -c -m 755 -o root -g floppy MAKEFLOPPIES $PKG/usr/sbin
install -c -s -m 755 -o root -g floppy floppycontrol $PKG/usr/bin
install -c -c -s -m 755 -o root -g floppy floppymeter $PKG/usr/bin
install -c -s -m 755 -o root -g floppy getfdprm $PKG/usr/bin
install -c -s -m 755 -o root -g floppy setfdprm $PKG/usr/bin
install -c -s -m 755 -o root -g floppy fdrawcmd $PKG/usr/bin
install -c -s -m 755 -o root -g floppy superformat $PKG/usr/bin
install -c -s -m 755 -o root -g floppy xdfcopy $PKG/usr/bin
install -c -s -m 4750 -o root -g floppy fdmount $PKG/usr/bin
install -c -s -m 4750 -o root -g floppy fdmount $PKG/usr/bin/fdumount

echo "+===============+"
echo "| todos/fromdos |"
echo "+===============+"
cd $TMP 
tar xzvf $CWD/todos.tar.gz
cd todos
make
strip todos fromdos
cat todos > $PKG/usr/bin/todos
cat fromdos > $PKG/usr/bin/fromdos
cat todos.1.gz > $PKG/usr/man/man1/todos.1.gz
cat fromdos.1.gz > $PKG/usr/man/man1/fromdos.1.gz

echo "+=========+"
echo "| zip-2.2 |"
echo "+=========+"
cd $TMP
tar xzvf $CWD/zip22.tar.gz
cd zip-2.2
mkdir -p $PKG/usr/doc/zip-2.2
cp -a BUGS CHANGES INSTALL MANUAL README TODO WHATSNEW WHERE \
      $PKG/usr/doc/zip-2.2
chmod 644 $PKG/usr/doc/zip-2.2/*
chown root.root $PKG/usr/doc/zip-2.2/*
make -f unix/Makefile generic_gcc
strip zip zipnote zipsplit zipcloak
cat zip > $PKG/usr/bin/zip
cat zipnote > $PKG/usr/bin/zipnote
cat zipsplit > $PKG/usr/bin/zipsplit
cat zipcloak > $PKG/usr/bin/zipcloak
for page in man/zip.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/`basename $page`.gz
done

echo "+==========+"
echo "| unzip540 |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/unzip540.tar.gz
cd unzip-5.40
mkdir -p $PKG/usr/doc/unzip-5.40
cp -a BUGS COPYING Contents History.540 INSTALL README ToDo WHERE \
      $PKG/usr/doc/unzip-5.40
chmod 644 $PKG/usr/doc/unzip-5.40/*
chown root.root $PKG/usr/doc/unzip-5.40/*
make -f unix/Makefile linux
strip unzip unzipsfx funzip
cat unzip > $PKG/usr/bin/unzip
cat unzipsfx > $PKG/usr/bin/unzipsfx
cat funzip > $PKG/usr/bin/funzip
cat unix/zipgrep > $PKG/usr/bin/zipgrep
cd unix
for page in funzip.1 unzip.1 unzipsfx.1 zipgrep.1 zipinfo.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cd ..

# Build the package:
cd $PKG
tar czvf $TMP/aaa_base.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/mtools-3.8
  rm -rf $TMP/todos
  rm -rf $TMP/zip-2.2
  rm -rf $TMP/unzip-5.40
  rm -rf $PKG
fi