\documentclass[11pt, oneside]{amsart}
\usepackage{geometry}                % See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   % ... or a4paper or a5paper or ... 
%\geometry{landscape}                % Activate for for rotated page geometry
\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\usepackage{url}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}

\title{TUG-Ghostscript}
\author{Richard Koch}
\date{\today}                                           % Activate to display a given date or no date

\begin{document}
\maketitle
\section{Ghostscript}
This is the portion of the TUG documentation which deals with making the Ghostscript package.

\section{Important Warnings}

This section contains lessons learned in 2013. The material here is important for all packages made with the new
PackageMaker.

Folders in which packages are built should live on the hard disk rather than an external
disk. Building constructs subfolders named ``root'' which contain copies of material to be installed. This root folder and its
contents are usually owned by root. But material on an external drive is owned by the user instead, so owners will be
incorrect after installing.

The final MacTeX template refers to  root folders of the various sub projects. In the template, each sub project
is listed in the left column; clicking on an arrow there reveals a panel in which the root folder is determined and configured.


Configuration of a root folder is governed by a panel with four tabs: Configuration, Contents, Components, and Scripts.
The material in the first and last tabs is important and should be entered carefully. The second Contents tab allows owners, groups, and
permissions of individual files in the folder to be set. This task is impossible for a huge root folder like TeXLive, but luckily
Apple provides a button "Apply Recommedations" which does it automatically following Apple's "best practices policy."
Always click this button.

For binary files like texlive/2013/bin/universal-darwin, the recommendations are unexpected. Actual binaries have owner root
and group wheel, but symbolic links have owner an individual user and group wheel. Permissions are -rwxr-xr-x. Experiments
and email conversations show that these choices work well.

For the GUI-package, the third tab, "Components", is very important. It lists actual apps and offers to relocate them. {\em Turn this off}.
Otherwise the installer won't upgrade programs in /Applications/TeX, but instead will search the disk for a copy to update.
Always check that these items are off. 

\section{Contents} 
A clean package file will contain
\begin{itemize}
\item ``About'' folder with public documentation explaining how the package was constructed
\item ``Binaries'' folder with empty subfolders  ``10.5'', ``10.6'' and the file ``liposcript'' 
\item Files ``buildPackage.sh''  and ``buildTeXfonts.sh''
\item Files ``background.jpg'', ``License.rtf'', ``ReadMe.rtf'', and ``Welcome.rtf''
\item ``scripts'' folder containing files ``postinstall'' and ``setloginpath''
\item ``source'' folder containing ``ghostscript-fonts-std-8.11.tar'' and a folder of fonts named ``fonts''. The
tar folder contains a packed version of these fonts. (This folder is not needed in the current package because
Ghostscript now contains the 35 standard fonts. It will eventually be removed.)
The ``source'' folder also contains a folder named ``TeXfonts'' which in turn contains files named ``Fontmap'' and
``Fontmap.cmr'' and a folder named ``Bruno-Arnold''. This ``BA'' folder contains correspondence about a problem in earlier
distributions of the Ghostscript package which is fixed in this version. For details, see later sections of this document.
\item ``TUG'' folder with this document
\item Empty ``Raw Binaries'' folder
\end{itemize}

To construct the package, download the latest Ghostscript tar file from \url{http://www.ghostscript.com/} and put the file
in the ``source'' folder. Then unzip this file; the resulting folder is the source code for Ghostscript.

\section{OverView}

This folder contains an ``About'' folder giving an overview of the construction of the Ghostscript package. You may
want to read that document, because otherwise the compilation steps below will seem unnecessarily baroque.

\section{Leopard binaries}

{\bf Warning:} There is a bug in Ghostscript 9.07 which breaks Leopard PPC binaries. They compile fine, but do not run.
This bug was patched by Chris Liddell on the web; I don't know if the patch is now in Ghostscript. The patched file is included
in this directory as {\em osx-x86-x86\_64-gcc.h}. It is only necessary to use the patched file when compiling on Leopard PPC.
Go to the directory ``arch'' in the Ghostscript source. This folder has three files. Replace the file named {\em osx-x86-x86\_64-gcc.h}
with the patched file.


Build Ghostscript on Leopard Intel and Leopard PPC. On each of these operating systems, built a Ghostscript binary with
X11 support, and a binary without X11 support. Before building, make sure that Apple's X11 package is installed on both systems. 

We'll use Intel as an example. Move a copy of the Ghostscript source code to the Intel Leopard system. Using Terminal, switch directories to
/usr and rename local to local-temp. Then in the Ghostscript source code type
\begin{verbatim}
     make clean
     LDFLAGS="-L/usr/lib" ./configure
     make
     sudo make install
\end{verbatim}

Use the Finder's ``Go'' menu to go to /usr/local/bin and copy gs to the desktop. Rename it gs-X11-Intel. Next go to /usr and 
\begin{verbatim}
	sudo rm -R local
	sudo mv X11R6 X11R6-temp
\end{verbatim}

If there is a folder named X11, rename it as well. Then rebuild Ghostscript as above, move gs in /usr/local/bin to the desktop, and name it
gs-noX11-Intel.

Finally, clean up /usr by moving /usr/local-temp back to /usr/local, moving X11R6-temp to X11R6, and similarly for X11 if it existed.

Repeat this entire process on PowerPC Leopard, calling the resulting binaries gs-X11-PPC and gs-noX11-PPC.

Finally, move the four resulting binaries to the Binaries/10.5 directory of this MacTeX Ghostscript build tree.

\section{Snow Leopard binary}

Build two binaries on Snow Leopard (Intel only, of course), one with X11 support and one without. Note that X11 is installed by default on Snow Leopard
and Lion, but not on Mountain Lion, so the 64 bit binary without X11 support is needed.

Repeat the steps in the Leopard build described earlier. 
In the actual compile step on Snow Leopard, the LDFLAGS flag is not needed, so the compile instructions become 
\begin{verbatim}
     make clean
     ./configure
     make
     sudo make install
\end{verbatim}

Call the resulting binaries gs-X11-64 and gs-noX11-64. Move the resulting binaries to
the Binaries/10.6 directory of this MacTeX Ghostscript build tree.
\section{ Putting It All Together}

On whatever platform you use to build install packages, switch to the MacTeX build tree's Ghostscript folder; change to the Binaries
directory, and type
\begin{verbatim}
     sh liposcript
\end{verbatim}
This will lipo the various binaries together. The script will automatically throw away old copies and then create new copies of
gs-noX11, gs-X11, gs-X11-64Bit and gs-noX11-64Bit. Leave them in the directories where the liposcript put them. The first two
binaries are universal with 32 bit code for PPC and Intel. The last two binaries are universal with Intel 32 bit and 64 bit code. 
The 32 bit code is required because Snow Leopard runs on early Intel machines without 64 bit processors.

Next edit the file ``buildPackage.sh'' to reflect the current version of Ghostscript. Currently this file contains ``9.06'', referring to
folders installed by Ghostscript. Change this number appropriately.

Next make sure the latest TeX Live is installed on this machine and type
\begin{verbatim}
     sh buildTeXfonts.sh
\end{verbatim} 
This step will create a folder named ``fonts'' in ``source/TeXfonts/ containing symbolic links to the pfb font files in this latest
TeX Live distribution. 


Next change directory to /usr and rename /usr/local to /usr/local-temp. Copy the Ghostscript source folder to your machine and
install Ghostscript using
\begin{verbatim}
     make clean
     ./configure
     make
     sudo make install
\end{verbatim}

In this step we are not interested in the binaries, but instead in the support files for Ghostscript. 

Then change to the main Ghostscript directory of the MacTeX build system and type
\begin{verbatim}
     sudo sh buildPackage.sh
\end{verbatim}

This step will copy Ghostscript support files from /usr/local to the root directory of  MacTeX's  Ghostscript build folder.
It will add the binaries constructed earlier, add ``Fontmap'' and ``Fontmap.cmr'' to root/usr/local/share/ghostscript/9.06/Resource/Init
and add the symbolic links to .pfb fonts to root/usr/local/share/ghostscript/9.06/Resource/Font. Finally, it will build the final
root folder containing software to be installed, and make sure permissions are correct in this folder.


To finish, remove /usr/local and move /usr/local-temp back to /usr/local.

\section{Adjusting Files in the Ghostscript Install Package}

This package contains some files which are shown to the user during installation. One of these files
is a brief Welcome document, a second is a longer ReadMe file, and a third shows the License for the code. After the
Ghostscript install package is constructed, you may need to revise these documents. For example, the Welcome document
contains the release date. Usually TeX Live authors are overly ambitious and predict a release several months earlier
than it actually occurs, and you'll need to change the release date from time to time.

None of these files appear in MacTeX, but they appear if you make an optional Ghostscript Install Package.

\section{Making a Standalone Install Package}

This step is optional. If you want an install package containing only Ghostscript, construct it as follows. 

This section of the MacTeX package contains a Packagemaker project file named ``GhostscriptBuild''. Edit this project file using the
GUI interface of PackageBuilder as follows:

\begin{enumerate}
\item Click the top item on the left. The right side will change to a view with three tabs. First select
the Configuration tab. Edit so
\begin{enumerate}
\item Title: ``Ghostscript-9.06'
\item User Sees: Easy Install Only
\item Install Destination: System volume
\item Certificate: Leave this blank. You might think that putting a certificate name here would correctly sign
the package, but experiments show that it does not. The package needs to be signed after it is constructed.
\item Description: Empty
\end{enumerate}
\item The Requirements and Actions tab entries for the top item can be left blank

\item Click the entry in the Contents section on the left. The right side will change to a view with two tabs.
Select the Configuration tab. Edit so
\begin{enumerate}
\item Choice Name: Ghostscript-9.06
\item Identifier: choice1
\item Initial State: Selected and Enabled
\item Destiination: Leave Blank
\item Tooltip: Leave Blank
\item Description: Leave Blank
\end{enumerate}
\item The Requirements tab entries for this contents item can be left blank
\item Open the Ghostscript-9.06 entry in the bottom left column. This item should show a ``local'' directory,
obtained by dragging root/user/local to the left column. The right side will change to a view with four tabs.
The item under the Components tab can be left blank. It is not necessary to edit the
items under the Contents tab. The items under the
Configuration tab should be edited as follows:
\begin{enumerate}
\item Install: root/usr/local (The item on the right should have selected ``Relative To Project'')
\item Destination: /usr/local (The item on the right should have selected ``Relative To Project'')
\item Do not check ``Allow custom location''
\item Patch: No patch root selected
\item Package Identifier: org.tug.mactex.ghostscript9.06
\item Package Version: 1.0
\item Restart Action: None
\item Check ``Require admin authentication''
\item PackageLocation: Self-Contained
\end{enumerate}
The items under the Scripts tab should be edited to read
\begin{enumerate}
\item Scripts directory: scripts (The item on the right should have selected ``Relative To Project'')
\item Preinstall: Leave Blank
\item Postinstall: scripts/postinstall (The item on the right should have selected ``Relative To Project'')
\end{enumerate}

\end{enumerate}

After verifying all these items, build the install package by clicking the ``Build'' icon. When asked to select
a name, choose ``Ghostscript-9.06-Temp''. 
After the package has been made, sign it by executing the following line in Terminal:
\begin{verbatim}
     sh signPackage.sh
\end{verbatim}
This script simply calls
\begin{verbatim}
     productsign --sign "Developer ID Installer: Richard Koch" old-name new-name
\end{verbatim}
where ``old-name'' is Ghostscript-9.06-Temp.pkg and ``new-name'' is Ghostscript-9.06.pkg









\end{document}  



The following warnings will be posted from the previous step. These can be ignored.
\begin{verbatim}
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/man1/eps2eps.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/man1/gsbj.1".     
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/man1/gsdj.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/man1/gsdj500.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/man1/gslj.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/man1/ps2pdf12.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/man1/ps2pdf13.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/man1/ps2pdf14.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/de/man1/eps2eps.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/de/man1/ps2pdf12.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/de/man1/ps2pdf13.1". 
Warning: Mode lrwxr-xr-x  does not match -rw-r--r--  of root volume 
     for "/usr/local/share/man/de/man1/ps2pdf14.1". 
\end{verbatim}
