#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-at
NAME=at
VERSION=3.1.8
ARCH=i386
BUILD=1

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

echo "+==========+"
echo "| at-$VERSION |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/at_$VERSION.orig.tar.gz
cd at-$VERSION.orig
# Pre-shipped crud:
rm y.tab.c y.tab.h lex.yy.c
zcat $CWD/at_$VERSION-10.2.diff.gz | patch -p1 --backup --verbose -E
mkdir -p $PKG/usr/doc/at-$VERSION
cp -a COPYING ChangeLog Copyright Problems README timespec \
  $PKG/usr/doc/at-$VERSION
chown root.root $PKG/usr/doc/at-$VERSION/*
chmod 644 $PKG/usr/doc/at-$VERSION/*
./configure i386-slackware-linux
make
strip at atd
cat at > $PKG/usr/bin/at
cat atrun > $PKG/usr/sbin/atrun
cat atd > $PKG/usr/sbin/atd
cat batch > $PKG/usr/bin/batch
cat at.1 | gzip -9c > $PKG/usr/man/man1/at.1.gz
cat atrun.8 | gzip -9c > $PKG/usr/man/man8/atrun.8.gz
cat atd.8 | gzip -9c > $PKG/usr/man/man8/atd.8.gz

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
echo "n" | makepkg $TMP/$NAME-$VERSION-$ARCH-$BUILD.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/at-$VERSION
  rm -rf $PKG
fi