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

PACKAGE=@PACKAGE@
VERSION=@VERSION@
if [ "$ARCH" = "" ]; then
  ARCH="`arch`"
  if [ "$ARCH" = "" ]; then
    ARCH="i486"
  fi
fi
BUILD=1
#      |-----handy-ruler------------------------------------------------------|"
DESC="\
microdc: microdc (a command-line based Direct Connect client)
microdc:
microdc: microdc is a command-line based Direct Connect client that uses the
microdc: GNU Readline library for user interaction. It was developed from
microdc: ground up and does not depend on any other program. Despite the
microdc: command-line user interface, microdc was designed to be user
microdc: friendly and simple to use.
microdc:
microdc: microdc is currently in beta state - there may be many bugs not yet
microdc: discovered. It also lacks some features that other clients support,
microdc: such as file hashing, multiple hub connections, and hub list support.
microdc:
microdc: microdc homepage: http://www.nongnu.org/microdc/"

PKG=$TMP/package-$PACKAGE

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"
else
  SLKCFLAGS="-O2 -march=$ARCH"
fi

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

cd $TMP || exit 1
rm -rf $PACKAGE-$VERSION
tar xzvf $CWD/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-$VERSION || exit 1
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --libdir=/usr/lib$LIBDIRSUFFIX \
  $ARCH-slackware-linux || exit 1
make || exit 1
make install DESTDIR=$PKG

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

mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a \
  ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL INTERNALS NEWS README TODO \
  $PKG/usr/doc/$PACKAGE-$VERSION/
rm -rf $PKG/usr/share/doc
chmod 644 $PKG/usr/doc/$PACKAGE-$VERSION/*

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

( cd $PKG || exit 1
  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
echo "$DESC" > $PKG/install/slack-desc

# Build the package:
cd $PKG || exit 1
makepkg --linkadd y --chown n $CWD/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz

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