#!/bin/bash

# inn.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/source/$PKGNAM*
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac 

# Grab the FAQ:
wget -r -v http://blank.org/innfaq

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$VERSION
slackhousekeeping

# Apply patches.  I was looking for ARM patches since 2.4.5 failed
# on ARM, but found these in Debian which fix genuine problems with this older
# release; so I'm applying them even though they aren't in Slackware.
zcat $PORTCWD/sources/inn*diff* | patch -p1
for i in configure-hostname \
 fix_ad_flag \
 fix_body_regexps \
 no-makedbz-on-install \
 nocem-gpg-import \
 typo_inn_conf_man \
 u_innreport_misc \
 u_right_length \
 u_status_init_ip \
 u_tls_duplicate_reply \
 u_xhdr_permissions \
 u_xover_duplicate_reply ; do
  patch --verbose -p1 < debian/patches/$i  || exit 1
 done

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -favv CONTRIBUTORS HACKING INSTALL LICENSE MANIFEST NEWS README* TODO \
         $PKG/usr/doc/$PKGNAM-$VERSION
( cd $TMPBUILD/blank.org ; cp -favv innfaq $PKG/usr/doc/$PKGNAM-$VERSION )

# Configure:
rm -f config.cache
LDFLAGS='-lpthread' \
CFLAGS="$SLKCFLAGS -I/usr/include/db42 " \
BERKELEY_DB_LIB="-ldb-4.2" \
sh configure \
   --enable-tagged-hash \
   --with-perl \
   --disable-shared \
   --with-sendmail=/usr/sbin/sendmail \
   --prefix=/usr/lib/news \
   --mandir=/usr/man \
   --with-news-user=news \
   --with-news-group=news \
   --with-news-master=news \
   --with-db-dir=/var/lib/news \
   --with-etc-dir=/etc/news \
   --with-log-dir=/var/log/news \
   --with-run-dir=/var/lib/news/run \
   --with-spool-dir=/var/spool/news \
   --with-tmp-path=/var/lib/news/tmp \
   --with-berkeleydb \
   $ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install onto the filesystem because we require some binaries
# to exist for the rest of the installation to complete:
make install

# Install into package framework:
# This is lifted from the INN makefile, and redirected at $PKG//, since this part
# of the Makefile doesn't honor the $DESTDIR variable:
for D in /usr/lib/news /usr/lib/news/bin /usr/lib/news/bin/auth \
         /usr/lib/news/bin/auth/resolv /usr/lib/news/bin/auth/passwd \
         /usr/lib/news/bin/control /usr/lib/news/bin/filter \
         /usr/lib/news/bin/rnews.libexec /var/lib/news /etc/news \
         /usr/lib/news/lib /usr/man /usr/man/man1 /usr/man/man3 \
         /usr/man/man5 /usr/man/man8 /var/spool/news /var/lib/news/tmp \
         /var/spool/news/archive /var/spool/news/articles \
         /var/spool/news/incoming /var/spool/news/incoming/bad \
         /var/spool/news/innfeed /var/spool/news/overview \
         /var/spool/news/outgoing /var/log/news \
         /var/log/news/OLD ; do
    support/install-sh -o news -g news -m 0755 -d $PKG/$D
done
support/install-sh -o news -g news -m 0750 -d $PKG/var/lib/news/run
make install DESTDIR=$PKG || failinstall

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Add some default files:
mkdir -p $PKG/etc/rc.d
( cd $PKG/etc/rc.d ; rm -rf rc.news )
( cd $PKG/etc/rc.d ; ln -sf /usr/lib/news/bin/rc.news . )
mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin ; ln -sf /usr/lib/news/bin/inews . )
# Still listed in include/paths.h:
touch $PKG/var/lib/news/subscriptions
chown news:news $PKG/var/lib/news/subscriptions
chmod 644 $PKG/var/lib/news/subscriptions
# Still listed in include/paths.h:
install -m 644 -o news -g news $CWD/distributions \
        $PKG/var/lib/news/distributions
rm -f $PKG/var/lib/news/history
touch $PKG/var/lib/news/history
touch $PKG/var/lib/news/.news.daily
chown news:news $PKG/var/lib/news/history \
                $PKG/var/lib/news/.news.daily
install -m 644 -o news -g news $CWD/history.dir \
        $PKG/var/lib/news/history.dir
install -m 644 -o news -g news $CWD/history.pag \
        $PKG/var/lib/news/history.pag
# if you transfer with uucp, you'll need to allow rnews access:
chown uucp:news $PKG/usr/lib/news/bin/rnews
chmod 4550 $PKG/usr/lib/news/bin/rnews
umask 002
touch $PKG/var/log/news/news.notice
touch $PKG/var/log/news/news.crit
touch $PKG/var/log/news/news.err
touch $PKG/var/lib/news/active.times
chown news:news $PKG/var/lib/news/active.times
chown -R news:news $PKG/var/log/news
chown -R root:root $PKG/usr/man

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
# /extra has packages in separate dirs:
export PKGSERIES=$PKGSERIES/$PKGNAM
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
