#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-afbinit

LONGVER=1.0.orig
VERSION=1.0_1
ARCH=${ARCH:-sparcv9}
BUILD=${BUILD:-0}

# I can't see you breaking this option out,
# AFB is a SPARC specialty ;-)
SLKCFLAGS="-O2"

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

### Unpack sources
cd $TMP
rm -rf afbinit-$LONGVER
tar xzvf $CWD/afbinit-$LONGVER.tar.gz
cd afbinit-$LONGVER

### Fix permissions
chown -R root.root .

### Patch
# We will add the Debian documentation patch to the mix
zcat $CWD/afbinit_1.0-1.diff.gz | patch -p1 --verbose

### Build
make
strip afbinit

### Install
# The executables:
mkdir -p $PKG/usr/sbin
cp afbinit $PKG/usr/sbin
chown -R root.bin $PKG/usr/sbin
mkdir -p $PKG/install
cp rc.afb $PKG/install
chmod 755 $PKG/install/rc.afb  # enable by default

# The documentation
mkdir -p $PKG/usr/man/man8
cp -a debian/afbinit.8 $PKG/usr/man/man8/
gzip -9 $PKG/usr/man/man8/*
mkdir -p $PKG/usr/doc/afbinit-$LONGVER
(  cd debian
   cp -a \
     README.Debian changelog copyright \
     $PKG/usr/doc/afbinit-$LONGVER
)

# The installation details
mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/afbinit-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/afbinit-$VERSION
  rm -rf $PKG
fi
