#!/bin/sh

NAME=libselinux
VERSION=1.26
ARCH=${ARCH:-i486}
BUILD=2

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.tgz
cd $NAME-$VERSION
chown -R root.root .

# Compile package:
CFLAGS="$SLKCFLAGS" \

make
make install DESTDIR=$PKG


gzip_man_pages() {
for file in `ls` ; do
  if [ -r $file ]; then
    manlink=`cat $file|grep -e "^\.so\ "`
    if [ "$manlink" != "" ];then
      rm `echo $file`
      gzip -9 `echo $manlink|sed s/^\.so\ //` 2>/dev/null
      ln -s `echo $manlink|sed s/^\.so\ //`.gz `echo $file`.gz
    else
      gzip -9 `echo $file` 2>/dev/null
    fi
  fi
done
}
						    
cd $PKG/usr/share/man/man3
gzip_man_pages
cd $PKG/usr/share/man/man8
gzip_man_pages

PKG=/tmp/package-$NAME

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

cd /tmp/$NAME-$VERSION

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

cp ChangeLog LICENSE VERSION $PKG/usr/doc/$NAME-$VERSION

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

# Copy runtime installation files:
mkdir -p $PKG/install
cat $CWD/install/slack-desc > $PKG/install/slack-desc

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