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

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

VERSION=1.0.3
ARCH=i486
BUILD=1

echo "+===============+"
echo "| genpower-$VERSION |"
echo "+===============+"
cd $TMP
rm -rf genpower-$VERSION
tar xzvf $CWD/genpower-$VERSION-1.tar.gz
cd genpower-$VERSION
chown -R root.root .
find . -perm 664 -exec chmod 644 {} \;
zcat $CWD/genpower.var.diff.gz | patch -p1 -E
make
strip genpowerd gentest
mkdir -p $PKG/sbin
cat genpowerd > $PKG/sbin/genpowerd
cat gentest > $PKG/sbin/gentest
cat genpowerfail > $PKG/sbin/genpowerfail
mkdir -p $PKG/etc
cat genpowerd.conf > $PKG/etc/genpowerd.conf
chown -R root.bin $PKG/sbin
chmod 755 $PKG/sbin/*
mkdir -p $PKG/usr/man/man8
cat genpowerd.8 | gzip -9c > $PKG/usr/man/man8/genpowerd.8.gz
cat gentest.8 | gzip -9c > $PKG/usr/man/man8/gentest.8.gz
mkdir -p $PKG/usr/doc/genpower-$VERSION
cp -a \
  COPYING Change.Log README TODO genpower.docs cables \
  $PKG/usr/doc/genpower-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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

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