#!/bin/sh

NAME=pwdb
VERSION=0.62
ARCH=${ARCH:-i486}
BUILD=4

CWD=`pwd`
PKG=/tmp/package-$NAME

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" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

# Clean target location:
rm -rf $PKG
mkdir -p $PKG

# Prepare the framework and extract the package:
cd /tmp
rm -rf $PKG $NAME-$VERSION
tar xzvf $CWD/$NAME-$VERSION.tar.gz
cd $NAME-$VERSION
chown -R root.root .

#cat $CWD/default.defs.diff | patch -p0 --verbose --backup

# Compile package:
CFLAGS="$SLKCFLAGS" \

make

# Install the package:
#export PREFIX=$PKG
mkdir -p $PKG/lib
mkdir -p $PKG/usr/include

make install PREFIX=$PKG

cp -r include/* $PKG/usr/include
cp libpwdb/radius/public.h $PKG/usr/include/pwdb/pwdb_radius.h

( 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
)

# Install docs:
mkdir -p $PKG/usr/doc/$NAME-$VERSION

cp -r doc/* $PKG/usr/doc/$NAME-$VERSION

chmod 644 $PKG/usr/doc/$NAME-$VERSION/*

# Copy runtime installation files:
mkdir -p $PKG/install
cat $CWD/install/doinst.sh > $PKG/install/doinst.sh
cat $CWD/install/slack-desc > $PKG/install/slack-desc
mkdir -p $PKG/etc
cat $CWD/etc/pwdb.conf.new > $PKG/etc/pwdb.conf.new

# Create the package itself:
cd $PKG
makepkg -l y -c n /tmp/$NAME-$VERSION-$ARCH-$BUILD.tgz
