#!/bin/sh
# Set initial variables:
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-espgs
VER=7.07.1
VERSION=7.07
ARCH=alpha
BUILD=3
TARGET=$ARCH-alphaslack-linux

# 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-$VERSION
tar xjf $CWD/espgs-$VERSION-source.tar.bz2
cd espgs-$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
zcat $CWD/espgs.ijs.makefile.in.bug.diff.gz | patch -p1 --verbose
chown -R root.root .
cd ijs
./configure --prefix=/usr $TARGET
make -i
mkdir -p $PKG/usr/bin $PKG/usr/lib/cups/filter $PKG/etc/cups $PKG/usr/share/ghostscript/$VERSION
cat libijs.a > $PKG/usr/lib/libijs.a
#strip --strip-unneeded libijs.so
#cat libijs.so > /usr/lib/libijs.so
#chmod 755 /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
  cd 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-$VERSION-source.tar.bz2
cd espgs-$VERSION

# Build the version with X11/CUPS/Omni/gimp-print/etc support:
./configure --prefix=/usr --enable-cups --with-drivers=ALL --with-ijs --with-gimp-print --with-omni --with-x $TARGET
make clean
make
cat bin/gs > $PKG/usr/bin/gs ; strip --strip-unneeded $PKG/usr/bin/gs
chmod 755 $PKG/usr/bin/gs
# Now make a "basic" version of gs:
./configure --prefix=/usr --enable-cups=no --with-drivers=ALL --with-ijs --with-gimp-print=no --with-omni=no --with-x=no $TARGET
make clean
make
cat bin/gs > $PKG/usr/bin/gs-no-x11 ; strip --strip-unneeded $PKG/usr/bin/gs-no-x11
cp -a lib/wftopfa lib/sysvlp.sh lib/unix-lpr.sh lib/pv.sh lib/ps2ps lib/ps2pdfwr lib/ps2pdf14 lib/ps2pdf13 lib/ps2pdf12 lib/ps2pdf lib/ps2epsi \
    lib/ps2ascii lib/printafm lib/pphs lib/pj-gs.sh lib/pfbtopfa lib/pf2afm lib/pdfopt lib/pdf2ps lib/pdf2dsc lib/lprsetup.sh lib/gsnd lib/gslp \
    lib/gslj lib/gsdj500 lib/gsdj lib/gsbj lib/font2c lib/fixmswrd.pl lib/eps2eps lib/dvipdf lib/bdftops $PKG/usr/bin
chmod 755 $PKG/usr/bin/* ; chown root.root $PKG/usr/bin/*
mkdir -p $PKG/usr/doc/espgs-$VERSION ; mkdir -p $PKG/usr/share/ghostscript/$VERSION/doc ; mkdir -p $PKG/usr/share/ghostscript/$VERSION/examples
cp -a doc/*.ht* doc/*.css doc/README $PKG/usr/share/ghostscript/$VERSION/doc
cp -a examples/cjk examples/alphabet.ps examples/colorcir.ps examples/doretree.ps examples/escher.ps examples/golfer.ps examples/grayalph.ps \
    examples/ridt91.eps examples/snowflak.ps examples/tiger.ps examples/vasarely.ps examples/waterfal.ps $PKG/usr/share/ghostscript/$VERSION/examples
mkdir -p $PKG/usr/share/ghostscript/$VERSION/lib
cp -a lib/CIDF* lib/*.GS lib/*.upp lib/Fontmap lib/*.ps lib/*.rpd lib/*.ppd lib/*.src lib/*.xbm lib/*.xpm $PKG/usr/share/ghostscript/$VERSION/lib
mkdir -p $PKG/usr/man/man1 ;cp -a man/*.1 $PKG/usr/man/man1
( cd $PKG
( cd usr/man/man1 ; gzip -9 * )
)
cat pstoraster/pstoraster > $PKG/usr/lib/cups/filter/pstoraster ; chmod 755 $PKG/usr/lib/cups/filter/pstoraster
cat pstoraster/pstoraster.convs > $PKG/etc/cups/pstoraster.convs
( cd $PKG
( cd usr/doc/espgs-$VERSION ; ln -sf /usr/share/ghostscript/$VERSION/doc doc )
( cd usr/doc/espgs-$VERSION ; ln -sf /usr/share/ghostscript/$VERSION/examples examples )
( cd usr/man/man1 ; ln -sf gslp.1.gz gsbj.1.gz )
( cd usr/man/man1 ; ln -sf ps2ps.1.gz eps2eps.1.gz )
( cd usr/man/man1 ; ln -sf gslp.1.gz gsdj.1.gz )
( cd usr/man/man1 ; ln -sf gslp.1.gz gslj.1.gz )
( cd usr/man/man1 ; ln -sf ps2pdf.1.gz ps2pdf12.1.gz )
( cd usr/man/man1 ; ln -sf ps2pdf.1.gz ps2pdf13.1.gz )
( cd usr/man/man1 ; ln -sf gslp.1.gz gsdj500.1.gz )
)
( cd $PKG
 find . -type f | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
 find . -type f | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $PKG
makepkg -l y -c n $TMP/espgs-$VER-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/espgs.build.log
