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

VERSION=0.99
ARCH=${ARCH:-i486}
BUILD=2

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "powerpc" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
fi

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

cd $TMP
rm -rf tvtime-$VERSION
tar xzvf $CWD/tvtime-$VERSION.tar.gz
cd tvtime-$VERSION
chown -R root.root .
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc
make
make install DESTDIR=$PKG

mv $PKG/etc/tvtime/tvtime.xml $PKG/etc/tvtime/tvtime.xml.new

gzip -9 $PKG/usr/man/man?/*.?
gzip -9 $PKG/usr/man/??/man?/*.?

mkdir -p $PKG/usr/doc/tvtime-$VERSION
cp -a \
  ABOUT-NLS AUTHORS COPYING COPYING.LGPL ChangeLog INSTALL NEWS README \
  $PKG/usr/doc/tvtime-$VERSION/
mkdir -p $PKG/usr/doc/tvtime-$VERSION/html
cp -a \
  docs/html/* \
  $PKG/usr/doc/tvtime-$VERSION/html/

chown -R root.root $PKG/
chgrp -R bin $PKG/usr/bin

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

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

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