#!/bin/sh
CWD=`pwd`

VERSION=${VERSION:-7.07.1}
SHORTVER=${SHORTVER:-7.07}
ARCH=${ARCH:-sparc}
BUILD=${BUILD:-0}
# TUPLE=$ARCH-slackware-linux
TUPLE=$ARCH-splack-linux

TMP=/tmp/espgs-$VERSION
PKG=/tmp/package-espgs


cd /tmp
rm -rf $TMP $PKG

tar xjvf $CWD/espgs-$VERSION-source.tar.bz2

# We need ijs
# OK, first we need the IJS library and include files.
# Since it doesn't use an soname, and is very small (about 20K)
# there's no good reason to compile it shared, and it would be
# likely to be a bad idea anyway as the author says the protocol
# is still very much in flux.
cd $TMP
mkdir -p $PKG/usr/lib $PKG/usr/bin

# Make sure -lz is used with -lpng, or PNG detection will fail
# and PNG devices will not be included.
zcat $CWD/espgs.png.lz.diff.gz | patch -p1 --verbose

# Kludge around a substitution failure that would prevent IJS from building:
zcat $CWD/espgs.ijs.makefile.in.bug.diff.gz | patch -p1 --verbose

# Fix missing include in espgs
# Patch taken from the BLFS project
zcat $CWD/patches-splack/espgs-7.07.1rc2-gdevpng-1.patch.gz | patch -p1 --verbose

chown -R root.root .

cd ijs

CFLAGS="-O2" \
  ./configure --prefix=/usr
make CFLAGS="-ansi -pedantic -Wmissing-prototypes"

cat libijs.a > $PKG/usr/lib/libijs.a
cat ijs-config > $PKG/usr/bin/ijs-config
chown root.bin $PKG/usr/bin/ijs-config
chmod 755 $PKG/usr/bin/ijs-config
mkdir -p $PKG/usr/include/ijs
cp -a ijs.h ijs_client.h ijs_server.h $PKG/usr/include/ijs
( cd $PKG/usr/include/ijs
  chmod 644 ijs.h ijs_client.h ijs_server.h
  chown root.root ijs.h ijs_client.h ijs_server.h )
## Done with IJS

# Now the main source is compiled
# Build the version with X11/CUPS/Omni/gimp-print/etc support:
cd $TMP
CFLAGS="-O3" \
./configure \
  --prefix=/usr \
  --enable-cups \
  --with-drivers=ALL \
  --with-ijs \
  --with-gimp-print \
  --with-omni \
  --with-x \
  $TUPLE
make clean
# the inclusion of glib 1.2 by means of include statements
# is kind of a sledgehammer approach, but the espgs build
# system seems awkward otherwise.
make CFLAGS="-O3 \
  -I/usr/include/glib-1.2 -I/usr/lib/glib/include/"
make install prefix=$PKG/usr

# time for the basic version
CFLAGS="-O3" \
  ./configure \
  --prefix=/usr \
  --enable-cups=no \
  --with-drivers=ALL \
  --with-ijs \
  --with-gimp-print=no \
  --with-omni=no \
  --with-x-no \
  $TUPLE
make clean
make

# Copy files into the package
cat bin/gs > $PKG/usr/bin/gs-no-x11
chmod 755 $PKG/usr/bin/gs-no-x11
mkdir -p /usr/doc/espgs-$VERSION
( cd /usr/doc/espgs-$VERSION
  rm -rf doc examples
  ln -sf /usr/share/ghostscript/$SHORTVER/doc doc
  ln -sf /usr/share/ghostscript/$SHORTVER/examples examples )

cd $PKG
sh $CWD/espgs.fixup

mkdir -p install
cat $CWD/slack-desc > install/slack-desc
makepkg -l y -c n ../espgs-$VERSION-$ARCH-$BUILD.tgz
