#!/bin/bash

# libpng.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 14-Jun-2004

VERSION_OLD=1.2.50
VERSION_NEW=1.4.12

# 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

build_source() {
cd $TMPBUILD
rm -rf libpng*

# Create some package framework:
# (make install fails to do this)
mkdir -p $PKG/usr/{man/man{3,5},lib/pkgconfig,bin,include/libpng12,doc/$PKGNAM-$VERSION}

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

# Need this to substitute macros, but we'll keep using the
# pre-built Makefile for now anyway...
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man || exit 1

# Build:
make $NUMJOBS prefix=/usr LIBPATH=/usr/lib${LIBDIRSUFFIX} ZLIBLIB="-L/usr/lib${LIBDIRSUFFIX} -lm -lz" || exit 1

# Install:
make install prefix=/usr LIBPATH=/usr/lib${LIBDIRSUFFIX} ZLIBLIB="-L/usr/lib${LIBDIRSUFFIX} -lm -lz" DESTDIR=$PKG

# We'll try to remove this in 5 to 10 years, just like before.
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  if [ ! -e libpng.so.14 -a -e libpng14.so.14 ]; then
    ln -vsf libpng14.so.14 libpng.so.14
  fi
)

# Install docs:
cp -favv ANNOUNCE CHANGES INSTALL KNOWNBUG LICENSE README TODO Y2KINFO libpng.txt example.c \
         $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION
}

# Build the 1.2 version:
VERSION=$VERSION_OLD
build_source

# Build the 1.4 version:
VERSION=$VERSION_NEW
build_source

# Default the includes to version 1.4.x:
( cd $PKG/usr/include ; ln -sf libpng14 libpng )

# 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 -l y -c n

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