\documentclass[notitlepage,onecolumn]{article}
\usepackage{anysize}
\marginsize{2.5cm}{2.5cm}{2.5cm}{1cm}
\setlength{\headsep}{0.0in}
\setlength{\headheight}{0.0in}
\pagestyle{empty}
\title{Setting Up PySZG on Mac OS X 10.5}
\author{William Davis}

\begin{document}
 	\noindent \textbf{Setting Up PySZG on Mac OS X 10.5}
	\\{\footnotesize William Davis}
	\\{\footnotesize Thursday, July 10, 2008}
	\\
	\\
	\noindent{\bf \normalsize Introduction}
	\paragraph{}
	This report simply details the method I used to get PySZG working with our current Syzygy version on Mac OS X 10.5. I'm not claiming that this is a great way to do it - but as of today it's the only way I've found. This setup uses Python 2.3 and SWIG 1.3.21, with the Wii Syzygy 1.1 that has been patched for the Mac OSX 10.5 Darwin kernel. There seems to be some path issues which require that the core Syzygy dynamic libraries (*.dylib) be located in the same directory as the \verb!python2.3! executable, though there is probably a simple fix to this.\\
	\\
	\noindent{\bf \normalsize What You Need}
	\paragraph{}
	This section assumes only a few things - that you are using Mac 10.5 in Altgeld 102, you have the patched version of Syzygy installed (all machines we use in here currently do), and Python 2.3 installed as well (as with Syzygy, Python 2.3 is also installed on all machines in here) - however, the current installation that Mac defaults to is Python 2.5. We will circumvent this without destroying the 2.5 installation. The basic outline is as follows:
	\begin{list}{\textbullet}{}
	 \item Re-patch the \verb!arDataUtilities! source code in Syzygy
	 \item Re-install the newly patched Syzygy
	 \item Get and install SWIG 1.3.21
	 \item Bind the Python and Syzygy libraries using SWIG
	 \item Clean things up
	 \item Enjoy the fruits of your labor!
	\end{list}
	\paragraph{\normalsize Repatching Syzygy\\}
	The 1.1 version of Syzygy has a recent bug in \verb!arDataUtilities.cpp! and \verb!arDataUtilities.h! that prevents PySZG from binding correctly. It's a simple fix, and hopefully soon it should have proliferated among the lab machines so this part can be skipped. Many thanks to Jim Crowell for pointing out the bug.\\
	\\
	To fix it, change directories to the Syzygy source code:
	\begin{verbatim}$ cd /sw/syzygy/wii/szg/src/language \end{verbatim}
	We're going to edit the source files, but first make backups:
	\begin{verbatim}$ cp arDataUtilities.cpp arDataUtilities.cpp.back
	$ cp arDataUtilities.h arDataUtilities.h.back
	\end{verbatim}
	Hopefully you won't have to use them, but just to be safe.
	Use whatever editor you like to edit the \verb!arDataUtilities! files - I will use \verb!vim! to edit:
	\begin{verbatim}$ vim arDataUtilities.cpp \end{verbatim}
	We need to find the following block of code:
	\begin{verbatim}static string __version("");

			string ar_versionString() {
 			if (__version == "") {
   			ostringstream os;
   			os << SZG_MAJOR_VERSION << "."
      			<< SZG_MINOR_VERSION << "."
      			<< SZG_PATCH_VERSION;
   			__version = os.str();
 			}
 			return __version;
			} \end{verbatim}
	We see that this block of code comes shortly after a compiler directive:
	\begin{verbatim}#ifdef AR_USE_WIN_32 \end{verbatim}
	We need to move the above block of code to outside this compiler directive, so move it after the line
	\begin{verbatim}#endif \end{verbatim}
	but make sure it is the one that immediately follows the \verb!#ifdef! statement - then save and quit.\\
	Now for \verb!arDataUtilities.h!:
	\begin{verbatim}$ vim arDataUtilities.h \end{verbatim}
	Find the line that says:
	\begin{verbatim}string SZG_CALL ar_versionString(); \end{verbatim}
	and change it to:
	\begin{verbatim}SZG_CALL string ar_versionString(); \end{verbatim}
	Save the changes and quit. If anything goes terribly wrong, you can always revert back to your backup files
	and start over.

	\paragraph{\normalsize Re-installing Your Patched Syzygy\\}
	To reinstall the patched Syzygy, change directories to the build directory:
	\begin{verbatim}$ cd /sw/syzygy/wii/szg/build/ \end{verbatim}
	Make it clean first:
	\begin{verbatim}$ make demo-clean 
	$ make clean\end{verbatim}
	and then build it again:
	\begin{verbatim}$ make demo \end{verbatim}
	and that should do it.

	\paragraph{\normalsize Get and Install SWIG 1.3.21\\}
	To get SWIG 1.3.21, go to this link:
	\begin{verbatim}http://sourceforge.net/project/downloading.php?groupname=swig&
		   filename=swig-1.3.21.tar.gz&use_mirror=internap \end{verbatim}
	or Google ``SWIG 1.3.21'' and find the Sourceforge link (should be one of the top results). Put it wherever you like and unpack it:
	\begin{verbatim}$ gunzip swig-1.3.21.tar.gz
	$ tar -xf swig-1.3.21.tar
	$ cd SWIG-1.3.21
	\end{verbatim}
	Now configure SWIG for installation and install it:
	\begin{verbatim}$ ./configure
	$ make
	$ make install
	\end{verbatim}
	Again, should anything go wrong, you can uninstall it by typing:
	\begin{verbatim}$ make uninstall
	$ make clean
	\end{verbatim}
	and that should cover it. SWIG is probably smart enough to put itself in all the right places, but you can check it by going to an arbitrary place in your filesystem and typing:
	\begin{verbatim}$ swig -version \end{verbatim}
	to check that the SWIG binaries are on your \verb!PATH!. If not, you may have to add your \verb!bin! to the path:
	\begin{verbatim}$ export PATH=$PATH:/usr/local/bin \end{verbatim}
	It may be helpful to add that line to your \verb!.bash_profile!, as is described in the \textbf{Cleaning Up}  section.

	\paragraph{\normalsize Bind the Python and Syzygy Libraries\\}
	While you have just installed SWIG, you won't actually have to explicitly call it - this is built in to the PySZG makefiles. Before you make PySZG, however, you need to set a few environment variables. First, get the \verb!szguser.profile! from \verb!dent!. Connect to \verb!dent! from the Finder by hitting the Apple-Command (Flower) + K. A dialog will pop up, and in the top text field type:
	\begin{verbatim}afp://dent \end{verbatim}
	and click \verb!Connect!. Enter in your user name and password in the subsequent dialog and click \verb!Connect! once again, and from the list that comes up select \verb!netgroups! and click \verb!OK!. Navigate into the \verb!illimath! directory, and copy the file \verb!szguser.profile! to your home directory, \verb!/Users/<Your_User_Name>/!
	\paragraph{}
	Now from the command line, navigate to your home directory and source the user profile:
	\begin{verbatim}$ cd ~
	$ source szguser.profile \end{verbatim}
	which should set up a few of your environment variables, namely \verb!$SZGHOME! and \verb!$SZGBIN!. We still need a couple more however. Issue the following commands from the terminal:
	\begin{verbatim}$ export SZG_PYINCLUDE=/sw/include/python2.3/
	$ export SZG_PYLIB=
	$ export PATH=$PATH:$SZG_PYINCLUDE
	$ ln -s python2.3 python
	\end{verbatim}
	The first tells Syzygy where the header \verb!Python.h! is. The second doesn't actually need to be done, but we do it just in case \verb!SZG_PYLIB! has been previously set. The third line will put the \verb!python2.3! executable on your path, so it can be executed from anywhere. The final line is optional - it will override the current Python installation on the machine, by linking the Python 2.3 executable to the word \verb!python!. If you still wish to use Python 2.5, I would suggest skipping this line.
	\paragraph{}
	Now change directory into the PySZG directory, now known as \verb!python/! within the Syzygy directory tree, and make: 
	\begin{verbatim}$ cd $SZGHOME/python/
	$ make
	\end{verbatim}
	If it compiles, all is well so far. If not, this installation method will not work and there is something wrong. But we're still not quite ready yet - there are a few more things to be done. We must add the \verb! /sw/bin/! to the \verb!PATH!, link the libraries there, and set the \verb!PYTHONPATH!: 
	\begin{verbatim}$ export PATH=$PATH:/sw/bin/
	$ ln -s $SZGBIN/* /sw/bin/
	$ export PYTHONPATH=$SZGBIN
	\end{verbatim}
	The second line is very messy - I'm not sure precisely which files it needs to run - it may just need the dynamic libraries, so you could first try the following:
	\begin{verbatim}$ ln -s $SZGBIN/*.dylib /sw/bin \end{verbatim}
	But nonetheless, it should work if you dump everything in there.
	PySZG should now be ready - you can test it by executing the following:
	\begin{verbatim}
	$ python2.3
	>>> from PySZG import *
	>>> g = arGraphicsDatabase()
	>>> g.printStructure()
	\end{verbatim}
	If the above executes without errors, your installation was a success. To exit the Python interpreter, just type \verb!Ctrl+D!. You can further test it by trying the demos:
	\begin{verbatim}
	$ cd $SZGHOME/python/demo/blobby/
	$ python2.3 blobby.py
	\end{verbatim}
	If this does not work, again, sorry, something is wrong.\\
	\paragraph{Cleaning Up\\}
	This method leaves much to be desired: it is complicated to set up, it is not modular at all, and the paths have to be reset each time a new bash terminal is started. We can at least alleviate the last one of these slightly by building it into a shell script.
	\paragraph{}
	If you want this to be readily available each time you open a new terminal, you can edit/create a file called
	\verb!.bash_profile! in your home directory (\verb!/Users/<Your_User_Name>/!).
	Simply add the following to the file\footnotemark[1]\footnotetext[1]{Note that if PySZG was not set up in your account, you should replace Your User Name with the name of the account that PySZG was set up under, in the case of euclid, this user is reulab}:
	\begin{verbatim}
$ export SZGHOME=/sw/syzygy/wii/szg/
$ export SZGBIN=/Users/<Your_User_Name>/szgbin/
$ export SZG_PYINCLUDE=/sw/include/python2.3/
$ export SZG_PYLIB=
$ export PYTHONPATH=$SZGBIN
	\end{verbatim}
	Or, if you don't wish to edit your \verb!.bash_profile!, you can add the last three of those to the \verb!szguser.profile! and \verb!source! it every time you open a new shell.
	\paragraph{}
	And that should do it! This is definitely an imperfect process, and I'm open to improvement on it.
\end{document}
