#!/bin/bash

######################################################
# 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

# Add Missing Document Reported by Bruno
cp -pRf ${WORK}/DocumentsForGS/gdevds32.c ${WORK}/root/usr/local/share/doc/ghostscript/9.23

# Deal with TeX fonts

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



# Bruno Voisin's Final Take, May 3

FILES1=${WORK}/BrunoContributions/Bruno-Japan/Resource/Font/*
for file in $FILES1
do
	cp -pRf $file ${WORK}/root/usr/local/share/ghostscript/9.23/Resource/Font 
done

# FILES2=${WORK}/Bruno-Japan/Resource/CIDFont/*
# for file in $FILES2
# do
# 	cp -pRf $file ${WORK}/root/usr/local/share/ghostscript/9.23/Resource/CIDFont 
# done

FILES3=${WORK}/BrunoContributions/Bruno-Japan/Resource/CIDFSubst/*
for file in $FILES3
do
	cp -pRf $file ${WORK}/root/usr/local/share/ghostscript/9.23/Resource/CIDFSubst
done

FILES4=${WORK}/BrunoContributions/Bruno-Japan/Resource/Init/*
for file in $FILES4
do
	cp -pRf $file ${WORK}/root/usr/local/share/ghostscript/9.23/Resource/Init
done


# 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/gs-noX11 ${WORK}/root/usr/local/bin/gs-noX11
cp ${WORK}/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


