#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG1=$TMP/package-ghostscr
PKG2=$TMP/package-gs_x11
SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
for dir in $PKG1 $PKG2 ; do
  if [ ! -d $dir ]; then
    mkdir -p $dir # place for the package to be built
  fi
done

# Explode the package framework:
cd $PKG1
explodepkg $CWD/_ghostscr.tar.gz
cd $PKG2
explodepkg $CWD/_gs_x11.tar.gz

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+===================+"
echo "| ghostscript-2.6.2 |"
echo "+===================+"
cd $TMP
tar xzvf $CWD/ghostscript-2.6.2.tar.gz
cd gs262
# Patch the source:
zcat $CWD/Makefile-no-x11.gz > Makefile
zcat $CWD/gdevlinux.c.gz > gdevlinux.c
zcat $CWD/gs.1.diff.gz | patch
zcat $CWD/gs.c.diff.gz | patch
# Save the patches in the new package:
zcat $CWD/Makefile.gz > $PKG1/usr/src/ghostscript-2.6.2/Makefile
zcat $CWD/Makefile-no-x11.gz > $PKG1/usr/src/ghostscript-2.6.2/Makefile-no-x11
zcat $CWD/gdevlinux.c.gz > $PKG1/usr/src/ghostscript-2.6.2/gdevlinux.c
zcat $CWD/gs.1.diff.gz > $PKG1/usr/src/ghostscript-2.6.2/gs.1.diff
zcat $CWD/gs.c.diff.gz > $PKG1/usr/src/ghostscript-2.6.2/gs.c.diff
# Build the first package:
make
cat bdftops > $PKG1/usr/bin/bdftops
cat font2c > $PKG1/usr/bin/font2c
cat gs > $PKG1/usr/bin/gs
cat gsbj > $PKG1/usr/bin/gsbj
cat gsdj > $PKG1/usr/bin/gsdj
cat gslj > $PKG1/usr/bin/gslj
cat gslp > $PKG1/usr/bin/gslp
cat gsnd > $PKG1/usr/bin/gsnd
cat ps2ascii > $PKG1/usr/bin/ps2ascii
cat ps2epsi > $PKG1/usr/bin/ps2epsi
cat gs.1 | gzip -9c > $PKG1/usr/man/man1/gs.1.gz
for file in README gslp.ps gs_init.ps gs_dps1.ps gs_fonts.ps gs_lev2.ps \
  gs_statd.ps gs_type0.ps gs_dbt_e.ps gs_sym_e.ps quit.ps Fontmap uglyr.gsf \
  bdftops.ps decrypt.ps font2c.ps impath.ps landscap.ps level1.ps prfont.ps \
  ps2ascii.ps ps2epsi.ps ps2image.ps pstoppm.ps showpage.ps type1ops.ps \
  wrfont.ps ; do 
  cp $file $PKG1/usr/lib/ghostscript
done
for file in NEWS ansi2knr.1 devices.doc drivers.doc fonts.doc gs.1 hershey.doc \
  history.doc humor.doc language.doc lib.doc make.doc ps2epsi.doc \
  psfiles.doc readme.doc use.doc xfonts.doc ; do 
  cp $file $PKG1/usr/lib/ghostscript/doc
done
for file in chess.ps cheq.ps colorcir.ps golfer.ps escher.ps snowflak.ps \
  tiger.ps ; do 
  cp $file $PKG1/usr/lib/ghostscript/examples
done
chown -R root.root $PKG1/usr/lib/ghostscript
find $PKG1/usr/lib/ghostscript -type f -exec chmod 644 {} \;
# Now, we need to make the /usr/bin/gs with X11 support:
make clean
zcat $CWD/Makefile.gz > Makefile
make
cat gs > $PKG2/usr/bin/gs

# Build the package:
cd $PKG1
tar czvf $TMP/ghostscr.tgz .
cd $PKG2
tar czvf $TMP/gs_x11.tgz .

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/gs262
  rm -rf $PKG1
  rm -rf $PKG2
fi