#!/bin/zsh

######################################################
# Manual preparation before running this...
#
# SAVE ORIGINAL /usr/local AND MOVE IT OUT OF THE WAY
# mv /usr/local /usr/local.save
#
# Or just rename /usr/local/bin and /usr/local/share
#
# 
# Install Ghostscript. The non-binaries will become part of this package
# but the binary will be replaced by separately compiled binaries
# Don't install the Ghostscript fonts; the script below will supply them.
#
######################################################

# We're going to run this in the MacTeXDVD working directory...
WORK=`pwd`

if [[ ! -x /usr/local/bin/gs ]]; then
	echo "### error: Ghostscript is missing"
	exit 1
fi

echo "Removing old file tree at ${WORK}/root"
rm -rf ${WORK}/root

# Copy everything from /usr/local into our root
echo "Collecting files from /usr/local"
mkdir -p ${WORK}/root/usr/local
cp -pRf /usr/local/bin ${WORK}/root/usr/local/bin
cp -pRf /usr/local/share ${WORK}/root/usr/local/share


# Deal with TeX fonts

cp ${WORK}/BrunoContributions/Bruno-TeXLive/Resource/init/Fontmap.cmr ${WORK}/root/usr/local/share/ghostscript/9.55.0/Resource/Init/Fontmap.cmr
cp ${WORK}/BrunoContributions/Bruno-TeXLive/Resource/Init/Fontmap ${WORK}/root/usr/local/share/ghostscript/9.55.0/Resource/Init/Fontmap
cp ${WORK}/BrunoContributions/Bruno-TeXLive/Resource/Init/Fontmap.MacTeX ${WORK}/root/usr/local/share/ghostscript/9.55.0/Resource/Init/Fontmap.MacTeX
cp -pRf ${WORK}/TeXfonts/Font/*.pfb ${WORK}/root/usr/local/share/ghostscript/9.55.0/Resource/Font


# Replace gs binary with binaries compiled on various platforms; active program determined by postinstall script

rm ${WORK}/root/usr/local/bin/gs

cp ${WORK}/Binaries/Universal-Binaries/gs-noX11 ${WORK}/root/usr/local/bin/gs-noX11
cp ${WORK}/Binaries/Universal-Binaries/gs-X11 ${WORK}/root/usr/local/bin/gs-X11



# chmod
echo "Changing permissions"
chmod -R a+r ${WORK}/root/usr
chown -R root  ${WORK}/root/usr
chgrp -R wheel  ${WORK}/root/usr

chmod 755 ${WORK}/root/usr
chgrp wheel ${WORK}/root/usr


