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

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

GS_VERSION=8.15.4
ARCH=${ARCH:-x86_64}
BUILD=${BUILD:-2_slamd64_12.0}

PKG=/tmp/package-espgs/
rm -rf $PKG

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

# 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
zcat $CWD/ghostscript-CESA-2008-001.diff.gz | patch -p0 || exit 1

chown -R root:root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
cd ijs
rm -f install.sh missing
ln -sf /usr/share/automake-1.9/install-sh .
ln -sf /usr/share/automake-1.9/missing .
CFLAGS="$SLKCFLAGS" \
./configure \
	--prefix=/usr \
	--libdir=/usr/lib64 \
	--mandir=/usr/man
make -j10 || exit 1
make install DESTDIR=$PKG

# 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:
CFLAGS="$SLKCFLAGS" \
./autogen.sh --prefix=/usr \
    --enable-cups \
		--mandir=/usr/man \
    --with-drivers=ALL \
    --with-ijs \
    --with-gimp-print \
    --with-omni \
    --with-x \
		--libdir=/usr/lib64 \
    $ARCH-slackware-linux
make clean
make -j10 || ( make obj/arch.h ; make -j10 || exit 1 ) || exit 1
zcat $CWD/mak/unixinst.mak.gz > src/unixinst.mak
zcat $CWD/mak/addons.mak.gz > addons/addons.mak
zcat $CWD/mak/cups.mak.gz > pstoraster/cups.mak
echo "Ready to make install - press enter to continue"
make install DESTDIR=$PKG
# Now make a "basic" version of gs:
CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr \
    --enable-cups \
    --with-drivers=ALL \
    --with-ijs \
    --with-gimp-print \
    --with-omni \
    --with-x=no \
		--libdir=/usr/lib64 \
    $ARCH-slackware-linux
make clean
make -j10 || ( make obj/arch.h ; make -j10 || exit 1 ) || exit 1
cat bin/gs > $PKG/usr/bin/gs-no-x11
chmod 755 $PKG/usr/bin/gs-no-x11

( gzip -9 $PKG/usr/man/{man1,de/man1}/* )

# Used (maybe) for documentation symlinks:
SHORTVER=$(echo $GS_VERSION | cut -f 1,2 -d .)
mkdir -p $PKG/usr/doc/espgs-$GS_VERSION
( cd $PKG/usr/doc/espgs-$GS_VERSION
  rm -rf doc examples
  if [ -d $PKG/usr/share/ghostscript/$GS_VERSION ]; then
    ln -sf /usr/share/ghostscript/$GS_VERSION/doc doc
    ln -sf /usr/share/ghostscript/$GS_VERSION/examples examples
  elif [ -d $PKG/usr/share/ghostscript/$SHORTVER ]; then
    ln -sf /usr/share/ghostscript/$SHORTVER/doc doc
    ln -sf /usr/share/ghostscript/$SHORTVER/examples examples
  else
    echo
    echo "$PKG/usr/share/ghostscript/$GS_VERSION and $PKG/usr/share/ghostscript/$SHORTVER"
    echo "not found!  Please adjust the build script."
    echo "halting."
    # this should get your attention
    sleep 12000
  fi
)

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

cd $PKG
rm eps2eps.1 # Why is this here?!
find -type f | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded
sh $CWD/espgs.fixup
makepkg -l y -c n ../espgs-$GS_VERSION-${ARCH}_slamd64-$BUILD.tgz
