#!/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-3.33 |"
echo "+==================+"
cd $TMP
tar xzvf $CWD/ghostscript-3.33.tar.gz
cd gs3.33
tar xzvf $CWD/ghostscript-3.33jpeg.tar.gz
tar xzvf $CWD/gs-svgalib-3.33.tar.gz
# Patch the source:
cat gs-svgalib-3.33/gs-svgalib-3.33.diff | patch -p1
# Remove A4 paper default:
zcat $CWD/Makefile-letter.diff.gz | patch
# They didn't really mean to install everything setuid root, did they?
zcat $CWD/Makefile.security.diff.gz | patch
# Use usual Slackware paths:
zcat $CWD/Makefile.paths.diff.gz | patch
# Patch Makefile to build all available drivers:
zcat $CWD/Makefile.diff.gz | patch
# Build the full X11 version of gs:
make
cat gs > $PKG2/usr/bin/gs
# Now, we need to make the /usr/bin/gs with no X11 support:
make clean
zcat $CWD/Makefile-nox11.diff.gz | patch
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 gsdj500 > $PKG1/usr/bin/gsdj500
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 wftopfa > $PKG1/usr/bin/wftopfa
cat gs.1 | gzip -9c > $PKG1/usr/man/man1/gs.1.gz
cat ps2epsi.1 | gzip -9c > $PKG1/usr/man/man1/ps2epsi.1.gz
for file in gslp.ps gs_init.ps gs_btokn.ps gs_ccfnt.ps gs_cidfn.ps \
gs_dps1.ps gs_fonts.ps gs_kanji.ps gs_lev2.ps gs_pfile.ps gs_res.ps \
gs_setpd.ps gs_statd.ps gs_type0.ps gs_type1.ps \
gs_dbt_e.ps gs_iso_e.ps gs_ksb_e.ps gs_std_e.ps gs_sym_e.ps \
quit.ps Fontmap bdftops.ps decrypt.ps font2c.ps impath.ps landscap.ps \
level1.ps packfile.ps prfont.ps printafm.ps ps2ascii.ps ps2epsi.ps \
ps2image.ps pstoppm.ps showpage.ps type1enc.ps type1ops.ps \
wftopfa.ps wrfont.ps \
gs_pdf.ps pdf_base.ps pdf_draw.ps pdf_font.ps pdf_main.ps pdf_2ps.ps \
gs_mex_e.ps gs_mro_e.ps gs_pdf_e.ps gs_wan_e.ps ; do
  cp $file $PKG1/usr/lib/ghostscript
done
for file in COPYING NEWS PUBLIC README current.doc devices.doc \
drivers.doc fonts.doc gs.1 hershey.doc history1.doc history2.doc humor.doc \
language.doc lib.doc make.doc ps2epsi.1 ps2epsi.doc psfiles.doc public.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 {} \;

# 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/gs3.33
  rm -rf $PKG1
  rm -rf $PKG2
fi
