#!/bin/bash

# postflight script for the MacTeX installer

PKG_PATH=$1

# Adjust PATH and MANPATH

# Much of this code was copied from Gerben Wierda long ago.
# Later, Leopard provided more convenient ways to modify the
# PATH, and this is reflected below. The code has not been touched
# since Leopard.

# add path setup code to /etc/profile and /etc/csh.login
# and man setup code to /etc/manpath.config (for 10.3) or
# /usr/share/misc/man.conf (for 10.4 and higher)

manpath="/Library/TeX/Distributions/.DefaultTeX/Contents/Man"
binpath="/usr/texbin"

# if [[ `/usr/bin/uname -r | /usr/bin/cut -f 1 -d .` -lt 9 ]]; then
# 	"${PKG_PATH}"/Contents/Resources/setloginpath $binpath TeX
# 	"${PKG_PATH}"/Contents/Resources/setmanpath "MANPATH $manpath" TeX
# 	"${PKG_PATH}"/Contents/Resources/setmanpath  "MANPATH_MAP $binpath $manpath" TeXMap 
# fi

# in Leopard, add elements to path and manpath

if [[ ( `/usr/bin/uname -r | /usr/bin/cut -f 1 -d .` -gt 8 ) && (  -d /etc/paths.d ) ]]; then
 	cp -n -X "${PKG_PATH}"/Contents/Resources/TeXPath /etc/paths.d/TeX
 	chown root /etc/paths.d/TeX
 	chgrp wheel /etc/paths.d/TeX
	chmod 644 /etc/paths.d/TeX
fi

if [[ ( `/usr/bin/uname -r | /usr/bin/cut -f 1 -d .` -gt 8 ) && (  -d /etc/manpaths.d ) ]]; then
 	cp -n -X "${PKG_PATH}"/Contents/Resources/TeXManPath /etc/manpaths.d/TeX
 	chown root /etc/manpaths.d/TeX
 	chgrp wheel /etc/manpaths.d/TeX
	chmod 644 /etc/manpaths.d/TeX
fi

# in Leopard, if /etc/profile or /etc/csh.login do NOT contain a call to path_helper, then use the old modifications
# don't do this in Snow Leopard

if [[ ( `/usr/bin/uname -r | /usr/bin/cut -f 1 -d .` -eq 9 ) ]]; then
if [[ ( -e /etc/profile ) && ( $( grep -c '^[^#]*eval `/usr/libexec/path_helper -s`' /etc/profile ) = 0 ) ]]; then
	"${PKG_PATH}"/Contents/Resources/setloginpath $binpath TeX
fi
if [[ ( -e /etc/csh.login ) && ( $( grep -c '^[^#]*eval `/usr/libexec/path_helper -c`' /etc/csh.login ) = 0 ) ]]; then
	"${PKG_PATH}"/Contents/Resources/setloginpath $binpath TeX
fi
fi


# Add /usr/local/texlive/2011basic/texmf-local if completely absent

 if [[ ! -d /usr/local/texlive/2011basic/texmf-local ]]; then
 	mkdir /usr/local/texlive/2011basic/texmf-local
 	mkdir /usr/local/texlive/2011basic/texmf-local/bibtex
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/bibtex/bib/local
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/bibtex/bst/local
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/dvips/local
 	mkdir /usr/local/texlive/2011basic/texmf-local/fonts
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/fonts/source/local
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/fonts/tfm/local
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/fonts/type1/local
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/fonts/vf/local
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/metapost/local
 	mkdir /usr/local/texlive/2011basic/texmf-local/tex
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/tex/latex/local
 	mkdir -p /usr/local/texlive/2011basic/texmf-local/tex/plain/local
	mkdir /usr/local/texlive/2011basic/texmf-local/web2c
	chmod -R 755 /usr/local/texlive/2011basic/texmf-local
fi

# Set the default paper size according to the user's preference.

# The install package is constructed in the US, and default paper size is "letter".  Note that
# resetting paper size also rebuilds formats.  So MacTeX will make formats for most users.
# The lucky few who avoid that step are running Intel machines and using letter paper; they use the
# prebuilt formats.


PAPER=`sudo -u $USER defaults read com.apple.print.PrintingPrefs DefaultPaperID | perl -pe 's/^iso-//; s/^na-//'`

PROCESSOR=`/usr/bin/uname -p`

PATH=/usr/local/texlive/2011basic/bin/universal-darwin:$PATH; export PATH

echo "### setting default paper size $PAPER"

if [[ "$PAPER" == "a4" ]]; then 
	/usr/local/texlive/2011basic/bin/universal-darwin/tlmgr paper $PAPER
fi





# Finally, construct the TeX Distribution data structure. Start with the key link

# First, set up /usr/texbin for the modern Pref Panel

if [[ ( !  -L /usr/texbin ) && ( -d /usr/texbin ) && ( -e /usr/texbin/tex ) ]]; then
	rm -R /usr/texbin
fi

if [[ -L /usr/texbin ]]; then
	 rm /usr/texbin
fi

ln -fhs /Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin /usr/texbin


# Next install the Preference Pane, but only do that if there is no existing pane, or if
# the existing pane is older than the pane being installed.

if [[ ! -d /Library/PreferencePanes ]]; then
	mkdir /Library/PreferencePanes
	chmod 755 /Library/PreferencePanes
	chgrp wheel /Library/PreferencePanes 
fi

if [[ ! -d /Library/PreferencePanes/TeXDistPrefPane.prefPane ]]; then
	cp -R "${PKG_PATH}"/Contents/Resources/PrefPane/TeXDistPrefPane.prefPane /Library/PreferencePanes/TeXDistPrefPane.prefPane
fi

if  [[ -d /Library/PreferencePanes/TeXDistPrefPane.prefPane ]]; then
	if [[ ! -e /Library/PreferencePanes/TeXDistPrefPane.prefPane/Contents/MacOS/GetSourceVersion ]]; then
		rm -R /Library/PreferencePanes/TeXDistPrefPane.prefPane
		cp -R "${PKG_PATH}"/Contents/Resources/PrefPane/TeXDistPrefPane.prefPane /Library/PreferencePanes/TeXDistPrefPane.prefPane
	fi
fi
	
if  [[ -d /Library/PreferencePanes/TeXDistPrefPane.prefPane ]]; then
	if [[ -e /Library/PreferencePanes/TeXDistPrefPane.prefPane/Contents/MacOS/GetSourceVersion ]]; then
		VERSION=`/Library/PreferencePanes/TeXDistPrefPane.prefPane/Contents/MacOS/GetSourceVersion`
		VERSIONNEW=`"${PKG_PATH}"/Contents/Resources/PrefPane/TeXDistPrefPane.prefPane/Contents/MacOS/GetSourceVersion` 
		if [[ $VERSION -lt $VERSIONNEW ]]; then
			rm -R /Library/PreferencePanes/TeXDistPrefPane.prefPane
			cp -R "${PKG_PATH}"/Contents/Resources/PrefPane/TeXDistPrefPane.prefPane /Library/PreferencePanes/TeXDistPrefPane.prefPane
		fi
		if [[ $VERSION -eq  151 ]]; then
			rm -R /Library/PreferencePanes/TeXDistPrefPane.prefPane
			cp -R "${PKG_PATH}"/Contents/Resources/PrefPane/TeXDistPrefPane.prefPane /Library/PreferencePanes/TeXDistPrefPane.prefPane
		fi
	fi
fi
	

# create the texdist structure

if [[ ! -d /Library/TeX ]]; then
	mkdir /Library/TeX
fi

 if [[ ! -d /Library/TeX/.scripts ]]; then
 	mkdir /Library/TeX/.scripts
 	cp "${PKG_PATH}"/Contents/Resources/texdist /Library/TeX/.scripts/texdist
 	chmod 755 /Library/TeX/.scripts/texdist
 fi

if [[ -L /Library/TeX/Documentation ]]; then
	rm /Library/TeX/Documentation
fi

if [[ ! -e /Library/TeX/Documentation ]]; then
	ln -s Distributions/.DefaultTeX/Contents/Doc /Library/TeX/Documentation 
fi

if [[ -L /Library/TeX/Root ]]; then
	rm /Library/TeX/Root
fi

if [[ ! -e /Library/TeX/Root ]]; then
	ln -s Distributions/.DefaultTeX/Contents/Root /Library/TeX/Root
fi

if [[ -L /Library/TeX/Local ]]; then
	rm /Library/TeX/Local
fi

if [[ ! -e /Library/TeX/Local ]]; then
	ln -s Distributions/.DefaultTeX/Contents/TexmfLocal /Library/TeX/Local
fi

if [[ ! -d /Library/TeX/Distributions ]]; then
	mkdir /Library/TeX/Distributions
fi

if [[ ! -e /Library/TeX/Distributions/TeXDist-description.rtf ]]; then
	cp "${PKG_PATH}"/Contents/Resources/TeXDist-description.rtf /Library/TeX/Distributions/TeXDist-description.rtf
	chmod 644 /Library/TeX/Distributions/TeXDist-description.rtf
fi

 if [[  -L /usr/local/bin/texdist ]]; then
 	rm /usr/local/bin/texdist
 fi

 if [[ ! -d /usr/local/bin ]]; then
	mkdir /usr/local/bin
 fi

 if [[ ! -e /usr/local/bin/texdist ]]; then
 	ln -s /Library/TeX/.scripts/texdist /usr/local/bin/texdist
 fi

if [[ ! -d /Library/TeX/Distributions/.DefaultTeX ]]; then
	mkdir /Library/TeX/Distributions/.DefaultTeX
fi 


# /Library/TeX/Distributions/.DefaultTeX/Contents will become a link to the Contents
# folder of the active distribution. Make TeXLive-2011-Basic that active distribution
# These are symbolic links, so the linked material will be created later on.

if [[  -L /Library/TeX/Distributions/.DefaultTeX/Contents ]]; then
	rm /Library/TeX/Distributions/.DefaultTeX/Contents
fi

if [[ !  -e /Library/TeX/Distributions/.DefaultTeX/Contents ]]; then
	ln -s  ../TeXLive-2011-Basic.texdist/Contents /Library/TeX/Distributions/.DefaultTeX/Contents
fi


# Now we come to the key data defining the various TeX distributions which can occur.
# It is legal to install this data even if the actual distribution isn't available, because the
# Preference Pane checks to make sure the links point somewhere before using them.

# MacTeX installs this data for itself, and for the TeX distributions in Fink and MacPorts.
# This last data is provided because Fink and MacPorts don't install the data and yet
# some users install those distributions.

# MacTeX-2011 only installs data for 2011, and not for other years. 

# The TeX Distribution data is much more elaborate than necessary because Jerome
# Laurens and Gerben Wierda thought of other uses of the data, which front ends haven't
# yet implemented.

# For mysterious reasons, each actual distribution occurs twice. Take for example TeXLive-2011-Basic.
# In /Library/TeX/Distributions, there is a folder named TeXLive-2011-Basic.texdist. But there is also
# a hidden folder of distributions, /Library/TeX/Distributions/.FactoryDefaults, and this folder
# contains a folder TeXLive-2011-Basic as well. Notice that the first name has an extension and the
# second does not.

# The TeXLive-2011-Basic folder in .FactoryDefaults contains a subfolder named Contents. The
# TeXLive-2011-Basic.texdist folder in Distributions contains an item named Contents, but this
# item is a symbolic link to the Contents folder in TeXLive-2011-Basic. Aside from this element,
# TeXLive-2011-Basic.texdist is empty. All of the interesting links are in
# .FactoryDefaults/TeXLive-2011-Basic/Contents

# This strange design was required so icons could be assigned to the various distributions,
# but that was never carried out. The extra complications now have no purpose, but they take
# almost no disk space and the only people noticing them are people reading these comments!

# All the items of interest are in /Library/TeX/Distributions/.FactoryDefaults/Contents
# Many of these items are there for future use and aren't currently used by software. The key
# subfolder of Contents for us is named Programs. It contains four links titled i386, ppc, powerpc,
# and x86_64. Each is a link to the actual binary directory of the distribution of indicated type.
# Some of these links might be missing for other distributions, but for TeXLive-2011-Basic, the
# links i386, ppc, and powerpc all point to universal-darwin.
# Incidentally, ppc and powerpc will usually point to the same place; this link is duplicated for
# historical reasons.

# Programs also contains a symbolic link named texdist which points to one of the links
# just described. The Preference Pane may reset this link. For instance, the drop down panel
# allowing the user to choose 32 or 64 bit binaries resets the texdist link.

# There is one other key item. Although Leopard can run 64 bit programs, the TeX Live 64 bit
# programs are compiled on Snow Leopard and require that operating system. Jerome Laurens has
# provided a mechanism for handling that situation. On Mac OS X, the Contents directory can contain
# an Info.plist file. Jerome defined such a plist file which lives the minimal operating version which
# can run particular binaries. This would allow us in the future to compile the 64 bit binaries on Lion
# or another advanced system.

# We first create .FactoryDefaults where the interesting data lives. We usually write data there
# only if it doesn't already exist. But we will rewrite the data for our own distribution.

if [[ ! -d /Library/TeX/Distributions/.FactoryDefaults ]]; then
	mkdir /Library/TeX/Distributions/.FactoryDefaults
fi

if [[ -d /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic ]]; then
	rm -R /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic
fi

# Next we create links to the main data in .Factory Defaults

if [[ ! -d /Library/TeX/Distributions/TeXLive-2011-Basic.texdist ]]; then
	mkdir -p /Library/TeX/Distributions/TeXLive-2011-Basic.texdist
	ln -s ../.FactoryDefaults/TeXLive-2011-Basic/Contents /Library/TeX/Distributions/TeXLive-2011-Basic.texdist/Contents
fi


# At last! Here's the data!

if [[ ! -d /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic ]]; then
	mkdir -p  /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/AllTexmf
	mkdir -p /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Doc
	mkdir -p /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Programs
	mkdir -p /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Resources
	mkdir -p /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Resources/English.lproj
	cp "${PKG_PATH}"/Contents/Resources/TeXDistVersion  /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/TeXDistVersion
	chmod 644 /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/TeXDistVersion
	cp "${PKG_PATH}"/Contents/Resources/Description.rtf  /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Resources/English.lproj/Description.rtf
	chmod 644  /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Resources/English.lproj/Description.rtf
	ln -s ../../../../../../usr/local/texlive/2011basic/texmf/doc/info /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Info
	ln -s ../../../../../../usr/local/texlive/2011basic/texmf/doc/man /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Man
	ln -s ../../../../../../usr/local/texlive/2011basic /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Root
	ln -s ../../../../../../usr/local/texlive/2011basic/texmf-local /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/TexmfLocal
	ln -s ../../../../../../usr/local/texlive/2011basic/texmf-var /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/TexmfSysVar
	ln -s ../../../../../../../usr/local/texlive/2011basic/bin/universal-darwin /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Programs/i386
	ln -s ../../../../../../../usr/local/texlive/2011basic/bin/universal-darwin /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Programs/powerpc
	ln -s ../../../../../../../usr/local/texlive/2011basic/bin/universal-darwin /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Programs/ppc
	ln -s ../../../../../../../usr/local/texlive/2011basic/texmf-dist/doc /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Doc/texmf-dist-doc
	ln -s ../../../../../../../usr/local/texlive/2011basic/texmf/doc /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Doc/texmf-doc
	ln -s ../../../../../../../usr/local/texlive/2011basic/texmf-doc/doc /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Doc/texmf-doc-doc
	ln -s ../../../../../../../usr/local/texlive/2011basic/texmf-var/doc /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Doc/texmf-var-doc
	ln -s ../../../../../../../usr/local/texlive/2011basic/texmf /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/AllTexmf/texmf
	ln -s ../../../../../../../usr/local/texlive/2011basic/texmf-dist /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/AllTexmf/texmf-dist
	ln -s ../../../../../../../usr/local/texlive/2011basic/texmf-doc /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/AllTexmf/texmf-doc
	ln -s ../../../../../../../usr/local/texlive/2011basic/texmf-var /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/AllTexmf/texmf-var
	ln -s ../../../../../../../usr/local/texlive/2011basic/texmf-local /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/AllTexmf/texmf-local
	if [[ "$PROCESSOR" == "powerpc" ]]; then
		echo "### setting program link powerpc"
		ln -s powerpc  /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Programs/texbin
	fi
	if [[ "$PROCESSOR" == "i386" ]]; then
		echo "### setting program link i386"
		ln -s i386  /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Programs/texbin
	fi
	cp "${PKG_PATH}"/Contents/Resources/PrefPane/Info.plist /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Info.plist
	chmod 644 /Library/TeX/Distributions/.FactoryDefaults/TeXLive-2011-Basic/Contents/Info.plist
fi


/usr/local/texlive/2011basic/bin/universal-darwin/fmtutil-sys --all



