#!/bin/sh
# Set initial variables:

GS_VERSION=$PKGVERSION

ARCH=${ARCH:-i486}

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"
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

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
make
make install

# 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" \
./configure --prefix=/usr \
    --enable-cups \
    --with-drivers=ALL \
    --with-ijs \
    --with-gimp-print \
    --with-omni \
    --with-x \
    $ARCH-slackware-linux
make clean
make
make install
# 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 \
    $ARCH-slackware-linux
make clean
make
cat bin/gs > /usr/bin/gs-no-x11
chmod 755 /usr/bin/gs-no-x11

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

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

