#!/bin/sh
CWD=`pwd`
PKG=/tmp/package-man

VERSION=1.6c
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}

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

cd /tmp
rm -rf $PKG
mkdir -p $PKG
rm -rf man-$VERSION
tar xjvf $CWD/man-$VERSION.tar.bz2
cd man-$VERSION

# Back out a change in man-1.6c that breaks makewhatis:
zcat $CWD/man.makewhatis.diff.gz | patch -p1 --verbose || exit 1

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -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 {} \;
CFLAGS="$SLKCFLAGS" \
./configure +lang all +traditional
make
make install PREFIX=$PKG
mv $PKG/usr/lib/man.conf $PKG/usr/lib/man.conf.new
( 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 $PKG/usr/man
  find . -name "*.?" -exec gzip -9 {} \;
  # hack to remove empty man directories:
  ( find . -type d | xargs rmdir ) 2> /dev/null
  ( find . -type d | xargs rmdir ) 2> /dev/null
)
mkdir -p $PKG/usr/doc/man-$VERSION
cp -a \
  COPYING INSTALL LSM README TODO \
  $PKG/usr/doc/man-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

cd $PKG
makepkg -l y -c n /tmp/man-$VERSION-$ARCH-$BUILD.tgz