#!/bin/sh
# Ported from Slackware build
# Set initial variables:
PKGNAM=nn
VERSION=6.7.3
BUILD=${BUILD:-2}

. /etc/pkghelpers
pkghelpers_env

rm -rf $PKG
mkdir -p $PKG
cd /tmp
rm -rf nn-$VERSION
tar xjvf $CWD/nn-$VERSION.tar.bz2
cd nn-$VERSION
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 {} \;
zcat $CWD/nn.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1
if [ x$LIBSUFFIX = x64 ]; then
  zcat $CWD/nn.lib64.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1
fi
zcat $CWD/nn.badnntphost.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1
zcat $CWD/nn.destdir.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1 # Adapted from an old Ubuntu patch
cp config.h-dist config.h
make # CFLAGS break this quite quickly - don't apply them!
# A quick note here:
# The original Slackware build file installs this to the root partition, then tries to undo broken
# hardlinks... the destdir patch also fixes this silly behaviour, but for some reason, nn doesn't 
# want to play nice and create the directories it needs. So we'll do it here.
mkdir -p $PKG/etc
mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/lib$LIBSUFFIX
mkdir -p $PKG/usr/man/man1
mkdir -p $PKG/usr/man/man8
echo "*** USE install option 's'"
sleep 5
DESTDIR=$PKG ./inst
#rm $PKG/usr/bin/nn.old # Don't know why this is here...
zcat $CWD/nntpserver.gz > $PKG/etc/nntpserver.new

chown -R root.root $PKG/usr/bin
mkdir -p $PKG/usr/doc/nn-$VERSION
cp -a \
  INSTALLATION README \
$PKG/usr/doc/nn-$VERSION
# This might be useful, so we'll put it in the docs just in case:
cp -a \
  $CWD/NNTP.gz \
  $PKG/usr/doc/nn-$VERSION
  gzip -d $PKG/usr/doc/nn-$VERSION/NNTP.gz
  chmod 644 $PKG/usr/doc/nn-$VERSION/NNTP
  chown root:root $PKG/usr/doc/nn-$VERSION/NNTP
mkdir -p $PKG/usr/doc/nn-$VERSION/contrib
( cd contrib ; cp -a \
  README aspell cn howto-metamail mail-alias nn_elm nn-use-mh nnsum recmail.c recmail.sh \
  $PKG/usr/doc/nn-$VERSION/contrib )
chmod 644 $PKG/usr/doc/nn-$VERSION/*
mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
pkghelpers_fixup
pkghelpers_makepkg