#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG1=$TMP/package-ghostscr
PKG2=$TMP/package-gs_x11

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

echo "+==================+"
echo "| ghostscript-4.03 |"
echo "+==================+"
cd $TMP
tar xzvf $CWD/ghostscript-4.03.tar.gz
cd gs4.03
tar xzvf $CWD/ghostscript-4.03jpeg.tar.gz
tar xzvf $CWD/ghostscript-4.03libpng.tar.gz
tar xzvf $CWD/ghostscript-4.03zlib.tar.gz
zcat $CWD/ghostscript-4.03-gdevl256.diff.gz | patch
zcat $CWD/ghostscript-4.03-makefile.diff.gz | patch
# Build the full X11 version of gs:
make
strip gs
mv gs gs-x11
cat gs-x11 > $PKG2/usr/bin/gs
# Now, we need to make the /usr/bin/gs with no X11 support:
make clean
zcat $CWD/ghostscript-4.03-nox11.diff.gz | patch
make
strip gs
cat gs > $PKG1/usr/bin/gs
for file in gsbj gsdj gsdj500 gslj gslp gsnd bdftops font2c \
pdf2dsc pdf2ps printafm ps2ascii ps2epsi ps2pdf wftopfa ; do
 strip $file
 cat $file > $PKG1/usr/bin/$file
 chmod 755 $PKG1/usr/bin/$file
 chown root.bin $PKG1/usr/bin/$file
done
for file in gs pdf2ps ps2ascii ps2epsi ps2pdf ; do
 cat $file.1 | gzip -9c > $PKG1/usr/man/man1/$file.1.gz
done
for file in Fontmap cbjc600.ppd cbjc800.ppd gs_init.ps gs_btokn.ps \
gs_ccfnt.ps gs_cidfn.ps gs_cmap.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_ttf.ps \
gs_typ42.ps gs_type1.ps gs_dbt_e.ps gs_iso_e.ps gs_ksb_e.ps gs_std_e.ps \
gs_sym_e.ps acctest.ps align.ps bdftops.ps caption.ps decrypt.ps docie.ps \
font2c.ps gslp.ps impath.ps landscap.ps level1.ps lines.ps markhint.ps \
markpath.ps packfile.ps pcharstr.ps pfbtogs.ps ppath.ps prfont.ps \
printafm.ps ps2ai.ps ps2ascii.ps ps2epsi.ps ps2image.ps pstoppm.ps \
quit.ps showchar.ps showpage.ps stcinfo.ps stcolor.ps traceimg.ps \
traceop.ps type1enc.ps type1ops.ps unprot.ps viewcmyk.ps viewgif.ps \
viewjpeg.ps viewpcx.ps viewpbm.ps viewps2a.ps winmaps.ps wftopfa.ps \
wrfont.ps zeroline.ps gs_l2img.ps gs_pdf.ps pdf2dsc.ps pdf_base.ps \
pdf_draw.ps pdf_font.ps pdf_main.ps pdf_sec.ps pdf_2ps.ps gs_mex_e.ps \
gs_mro_e.ps gs_pdf_e.ps gs_wan_e.ps gs_pdfwr.ps ; do
 cp -a $file $PKG1/usr/lib/ghostscript/$file
 chmod 644 $PKG1/usr/lib/ghostscript/$file
 chown root.root $PKG1/usr/lib/ghostscript/$file
done
for file in COPYING NEWS PUBLIC README c-style.txt current.txt devices.txt \
drivers.txt fonts.txt helpers.txt hershey.txt history1.txt history2.txt \
history3.txt humor.txt install.txt language.txt lib.txt make.txt \
new-user.txt ps2epsi.txt ps2pdf.txt psfiles.txt public.txt unix-lpr.txt \
use.txt xfonts.txt ; do
 cp -a $file $PKG1/usr/lib/ghostscript/doc/$file
 chmod 644 $PKG1/usr/lib/ghostscript/doc/$file
 chown root.root $PKG1/usr/lib/ghostscript/doc/$file
done
for file in alphabet.ps chess.ps cheq.ps colorcir.ps escher.ps golfer.ps \
grayalph.ps snowflak.ps tiger.ps waterfal.ps ; do
 cp -a $file $PKG1/usr/lib/ghostscript/examples/$file
 chmod 644 $PKG1/usr/lib/ghostscript/examples/$file
 chown root.root $PKG1/usr/lib/ghostscript/examples/$file
done

# Build the packages:
cd $PKG2
tar czvf $TMP/gs_x11.tgz .
# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done
cd $PKG1
tar czvf $TMP/ghostscr.tgz .
# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

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