#!/bin/bash

# netpbmp.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# Please see changelog.txt for revisions to this package's build script history.

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$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 

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM* || exit 1
slackhousekeeping

# Build:
# On ARM and any other arch for which svgalib cannot be built (thus not installed)
# you'll see: ppmsvgalib.c:14:17: vga.h: No such file or directory
# But ARMedslack now uses the svgalib-dummy package from Debian as a work-around.
#make CC="gcc $ARCH_CFLAGS" || failmake

# Apply build fixes:
#patch -p1 --verbose < $PORTCWD/sources/netpbm-10.35-gcc43.patch || exit 1
#patch -p1 --verbose < $PORTCWD/sources/netpbm-10.35-glibc.patch || exit 1
# glibc2.10 compat:
#sed -i "s:getline:getline_nonlibc:" converter/ppm/xvminitoppm.c

# Configure:
if [ "$ARCH" = "x86_64" ]; then
  zcat $CWD/config.mk.gz \
    | sed -e '/^CFLAGS/s/^CFLAGS = .*/& -fPIC/' > config.mk
else
  zcat $CWD/config.mk.gz > config.mk
fi

# Build (doesn't like parallel builds)
make -j1 || failmake

# Install into package:
rm -rf $PKG # otherwise the installation fails; it likes to make the directory itself
make package pkgdir=$PKG

# Put things in proper directories:
( cd $PKG
  rm README
  mkdir -p usr
  mv * usr
  cd usr
  mkdir -p doc/netpbm-$VERSION
  mv VERSION bin/doc.url doc/netpbm-$VERSION
  ( cd lib ; ln -sf libnetpbm.so.10 libnetpbm.so )
  rm -rf config_template link misc pkginfo man/man1/manweb.1 man/web
)

# Even out-of-date manpages are better than nothing.  IMHO.
#( cd $PKG/usr
#  tar jxvvf $CWD/netpbm-manpages.tar.bz2 )

# Add a documentation directory:
( cd doc
  cp -a \
  COPYRIGHT.PATENT GPL_LICENSE.txt INSTALL Netpbm.programming \
  README* USERDOC copyright_summary lgpl_v21.txt \
  $PKG/usr/doc/${PKGNAM}-$VERSION
  if [ -r HISTORY ]; then
    DOCSDIR=$(echo $PKG/usr/doc/${PKGNAM}-$VERSION)
    cat HISTORY | head -n 1000 > $DOCSDIR/HISTORY
    touch -r HISTORY $DOCSDIR/HISTORY
  fi
)

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

# 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
slackmp         # run makepkg

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