#!/bin/bash

######################################################
# Manual preparation before running this...
#
# SAVE ORIGINAL /usr/local AND MOVE IT OUT OF THE WAY
# mv /usr/local /usr/local.save
#
# 
# 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
cp -pRf /usr/local ${WORK}/root/usr

# Deal with TeX fonts

mkdir -p ${WORK}/root/usr/local/share/ghostscript/9.07/Resource/Init
mkdir -p ${WORK}/root/usr/local/share/ghostscript/9.07/Resource/Font
cp ${WORK}/source/TeXfonts/Fontmap.cmr ${WORK}/root/usr/local/share/ghostscript/9.07/Resource/Init/Fontmap.cmr
cp ${WORK}/source/TeXfonts/Fontmap ${WORK}/root/usr/local/share/ghostscript/9.07/Resource/Init/Fontmap
cp -pRf ${WORK}/source/TeXfonts/fonts/*.pfb ${WORK}/root/usr/local/share/ghostscript/9.07/Resource/Font

# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/amsfonts/cm/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/amsfonts/latxfont/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/amsfonts/cmextra/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/amsfonts/cyrillic/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/amsfonts/euler/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/amsfonts/symbols/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/

# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/hoekwater/context/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/hoekwater/mflogo/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/hoekwater/manfnt/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/

# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/rsfs/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/stmaryrd/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/tipa/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/wasy/*.pfb${WORK}/root/usr/local/share/ghostscript/fonts/

# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/belleek/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/cmcyr/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/cs/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/marvosym/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/omega/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/pl/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/pxfonts/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/txfonts/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/
# ln -s /Library/TeX/Root/texmf-dist/fonts/type1/public/xypic/*.pfb ${WORK}/root/usr/local/share/ghostscript/fonts/


rm ${WORK}/root/usr/local/bin/gs
# cp ${WORK}/Binaries/10.3--10.4/gs-noX11-tiger ${WORK}/root/usr/local/bin/gs-noX11-tiger
# cp ${WORK}/Binaries/10.3--10.4/gs-X11-tiger ${WORK}/root/usr/local/bin/gs-X11-tiger
cp ${WORK}/Binaries/10.5/gs-noX11 ${WORK}/root/usr/local/bin/gs-noX11
cp ${WORK}/Binaries/10.5/gs-X11 ${WORK}/root/usr/local/bin/gs-X11
cp ${WORK}/Binaries/10.6/gs-X11-64Bit ${WORK}/root/usr/local/bin/gs-X11-64Bit
cp ${WORK}/Binaries/10.6/gs-noX11-64Bit ${WORK}/root/usr/local/bin/gs-noX11-64Bit

# cp -pRf ${WORK}/source/fonts ${WORK}/root/usr/local/share/ghostscript/fonts


# 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

# Build the package!
# echo "Building the Installer package"
# ./../PackageMaker.app/Contents/MacOS/PackageMaker  -build -p ${WORK}/Ghostscript-9.07.pkg -ds -s \
# 	-f ${WORK}/root -r ${WORK}/resources -i ${WORK}/Info.plist -d ${WORK}/Description.plist


#  /Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/Packagemaker   --out ${WORK}/Ghostscript-9.07.pkg  --target 10.5  \
#  	--root ${WORK}/root --resources ${WORK}/resources --id  org.tug.mactex.ghostscript9.07 --verbose --no-recommend  \
# 	--root-volume-only --no-relocate --scripts ${WORK}/scripts


######################################################
# FINALLY, PUT MY /usr/local BACK AS IT WAS
# mv -f /usr/local.save /usr/local
######################################################
