#!/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 TeXLive-2013
#
# Use the default /usr/local/texlive/2013 and all remaining defaults except
# that $HOME/texmf should be changed to
#
#	$HOME/Library/texmf
#
# After installing, run tlmgr and enter the command
#
#      sudo /usr/local/texlive/2013/bin/universal-darwin/tlmgr option repository http://mirror.ctan.org/systems/texlive/tlnet
#
# or
#
# edit /usr/local/texlive/2009/tlpkg/texlive.tlpdb
# and change the location: line very near the top to:
#
#          depend location:http://mirror.ctan.org/systems/texlive/tlnet
#
######################################################

# We're going to run this in the MacTeXDVD working directory...
WORK=`pwd`

echo "Checking that /usr/local content seems reasonable"
if [[ ! -d /usr/local/texlive/2013 ]]; then
	echo "### error: main texlive directory is missing"
	exit 1
fi

if [[ ! -x /usr/local/texlive/2013/bin/universal-darwin/pdflatex ]]; then
	echo "### error: pdfLaTeX is missing"
	exit 1
fi

echo "Resetting tlmgr source"
/usr/local/texlive/2013/bin/universal-darwin/tlmgr option location http://mirror.ctan.org/systems/texlive/tlnet


echo "Removing texmf-local tree"
cd /usr/local/texlive
if [[ -d /usr/local/texlive/texmf-local ]]; then
	rm -R texmf-local
fi

echo "Removing old file tree at ${WORK}/root"
rm -rf ${WORK}/root

echo "Adding man symbolic link"
ln -s ../../texmf/doc/man /usr/local/texlive/2013/bin/universal-darwin/man

echo "Adding man symbolic link"
ln -s ../../texmf/doc/man /usr/local/texlive/2013/bin/x86_64-darwin/man


echo "Running texhash"
/usr/local/texlive/2013/bin/universal-darwin/texhash

# 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
# chmod
echo "Changing permissions"
chmod -R a+r ${WORK}/root/usr

# Ensure target directories we need for applications are present
chmod 755 ${WORK}/root/usr
chgrp wheel ${WORK}/root/usr

# Copy the TeXDist Preference Panel
# mkdir  ${WORK}/root/Library
# mkdir ${WORK}/root/Library/PreferencePanes
# chmod 1775 ${WORK}/root/Library
# chmod 775 ${WORK}/root/Library/PreferencePanes
# chgrp admin ${WORK}/root/Library
# chgrp admin ${WORK}/root/Library/PreferencePanes

# echo "Copying TeXDist Preference Panel"
# ditto ${WORK}/PreferencePane/TeXDistPrefPane.prefPane \
# 		${WORK}/root/Library/PreferencePanes/TeXDistPrefPane.prefPane
# chown -R root ${WORK}/root/Library/PreferencePanes/TeXDistPrefPane.prefPane

# Copy the format files for PPC
# rm ${WORK}/resources/dvilualatex.fmt
# rm ${WORK}/resources/dviluatex.fmt
# rm ${WORK}/resources/lualatex.fmt
# rm ${WORK}/resources/luatex.fmt
# ditto ${WORK}/luatex-fmt/dvilualatex.fmt ${WORK}/resources/dvilualatex.fmt
# ditto ${WORK}/luatex-fmt/dviluatex.fmt ${WORK}/resources/dviluatex.fmt
# ditto ${WORK}/luatex-fmt/lualatex.fmt ${WORK}/resources/lualatex.fmt
# ditto ${WORK}/luatex-fmt/luatex.fmt ${WORK}/resources/luatex.fmt

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



# Build the package!
# echo "Building the Installer package"
#  /Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/Packagemaker   --out ${WORK}/TeXLive-2013.pkg  --target 10.5  \
# 	--root ${WORK}/root --resources ${WORK}/resources --id org.tug.mactex.texlive2013  --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
######################################################
