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

VERSION=0.8.1a
ARCH=${ARCH:-powerpc}
BUILD=${BUILD:-1}

SLKCFLAGS="-O2"

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

cd $TMP
rm -rf pbbuttonsd-$VERSION
tar xzvf $CWD/pbbuttonsd-$VERSION.tar.gz

cd pbbuttonsd-$VERSION
chown -R root:root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 640 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -type d -exec chmod 755 {} \;

CFLAGS="$SLKCFLAGS" \
./configure LAPTOP=powerbook \
            --prefix=/usr \
            --sysconfdir=/etc \
	    --with-oss \
	    --with-alsa \
	    --without-pmud \
	    $ARCH-slackware-linux
make
make install DESTDIR=$PKG

chown -R root:root $PKG/usr/bin
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
gzip -9 $PKG/usr/share/man/man?/*
mkdir -p $PKG/usr/doc/pbbuttonsd-$VERSION
cp -a ABOUT-NLS AUTHORS BUGS COPYING ChangeLog INSTALL NEWS README TODO \
      $PKG/usr/doc/pbbuttonsd-$VERSION

# Do not overwrite previous config files!
mv $PKG/etc/pbbuttonsd.cnf $PKG/etc/pbbuttonsd.cnf.new
( cd $PKG/etc/power
for FILE in `find . -type f` ; do
    mv $FILE $FILE.new
done
)

mkdir -p $PKG/etc/rc.d
zcat $CWD/rc.pbbuttonsd.gz > $PKG/etc/rc.d/rc.pbbuttonsd
chmod 755 $PKG/etc/rc.d/rc.pbbuttonsd

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/pbbuttonsd-$VERSION-$ARCH-$BUILD.tgz

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