#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi

PKG=$TMP/package-espgs
mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/include
mkdir -p $PKG/usr/lib
chown -R root.root $PKG
chmod -R 755       $PKG

ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

GS_VERSION=7.07.1
# Used for documentation symlinks:
SHORTVER=7.07

# 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
rm -rf espgs-$GS_VERSION
tar xjvf $CWD/espgs-$GS_VERSION-source.tar.bz2
cd espgs-$GS_VERSION
# 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
chown -R root.root .
cd ijs
./configure --prefix=/usr
make CFLAGS="-ansi -pedantic -Wmissing-prototypes"
cat libijs.a > $PKG/usr/lib/libijs.a
#strip --strip-unneeded libijs.so
#cat libijs.so > $PKG/usr/lib/libijs.so
#chmod 755 $PKG/usr/lib/libijs.so
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 )
# Now the main source is compiled:
cd $TMP
# This should have been untarred when we compiled libijs.
#tar xjvf $CWD/espgs-$GS_VERSION-source.tar.bz2
cd espgs-$GS_VERSION

# Build the version with X11/CUPS/Omni/gimp-print/etc support:
./configure --prefix=/usr \
    --enable-cups \
    --with-drivers=ALL \
    --with-ijs=$PKG/usr \
    --with-gimp-print \
    --with-omni \
    --with-x \
    $ARCH-slackware-linux
make clean
make
make install DESTDIR=$PKG

# Now make a "basic" version of gs:
./configure --prefix=/usr \
    --enable-cups=no \
    --with-drivers=ALL \
    --with-ijs=$PKG/usr \
    --with-gimp-print=no \
    --with-omni=no \
    --with-x=no \
    $ARCH-slackware-linux
make clean
make
cat bin/gs > $PKG/usr/bin/gs-no-x11
chmod 755 $PKG/usr/bin/gs-no-x11

mkdir -p $PKG/usr/doc/espgs-$GS_VERSION
( cd $PKG/usr/doc/espgs-$GS_VERSION
  rm -rf doc examples
  ln -sf /usr/share/ghostscript/$SHORTVER/doc doc
  ln -sf /usr/share/ghostscript/$SHORTVER/examples examples )

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/espgs-$GS_VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  cd $CWD
  rm -rf $TMP/espgs-$GS_VERSION
  rm -rf $PKG
fi

