#!/bin/bash

# netpbmp.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 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 $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
#tar jxvvf $CWD/$PKGNAM-$VERSION.tar.bz2
tar jxvvf $PORTCWD/sources/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$VERSION
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

# Configure:
#zcat $CWD/Makefile.config.gz > Makefile.config
zcat $PORTCWD/sources/netpbm.config.diff.gz | patch -p1 --verbose

# Build:
#make CFLAGS="-I. -I/usr/include -I/usr/X11/include" -j4 || failmake
make || 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 HISTORY INSTALL Netpbm.programming README.CYGWIN README.DJGPP \
  USERDOC copyright_summary lgpl_v21.txt \
  $PKG/usr/doc/${PKGNAM}-$VERSION
)

# 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
