#!/bin/bash

LEOPARDTOOLS=NO

sudo /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr option repository http://mirror.ctan.org/systems/texlive/tlnet

######################################################
# 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
#
# To install from svn,
#
#	cd
#	cd texlive/MASTER
#	sudo sh install-live.sh
#
# At the start of the install script, add binaries for both powerpc and intel.
#
# Change TEXDIR from /usr/local/texlive/2013 to /usr/local/texlive/2013basic
#
# Change TEXMFHOME from $HOME/texmf to $HOME/Library/texmf
# Change TEXMFVAR from $HOME/./texmf-var to ~/Library/texlive/2013basic/texmf-var
# Change TEXMFCONFIG from $HOME/texmf-config to ~/Library/texlive/2013basic/texmf-config
#
# Under "options", check ''use letter size'' and uncheck all other options
#
# Change the Installation scheme to "TeX Live Basic Scheme"
#
# Customize this scheme by noticing that "Essential programs and files" and "Basic LaTeX packages" are installed
# and adding to this "LaTeX recommended packages" and
# MetaPost (and Metafont) drawing" and "PSTricks packages" and "XeTeX macros"
#
# Install
#
# Notice that the script below makes further modifications before creating the package.
#
# Finally, we need to configure the distribution and make format files by running the commands below.
# This will be done in the postinstall script.
#
#	sudo bash
#	rm -R /usr/local/texlive/2013basic/texmf-local
#	PATH=/usr/local/texlive/2013basic/bin/i386-darwin:$PATH; export PATH
#	texconfig-sys paper letter
#	texconfig-sys dvips paper letter
#	texconfig-sys init
#
######################################################

# 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/2013basic ]]; then
	echo "### error: main texlive directory is missing"
	exit 1
fi
 if [[ ! -x /usr/local/texlive/2013basic/bin/universal-darwin/pdflatex ]]; then
	echo "### error: pdfLaTeX is missing"
	exit 1
fi

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

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

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

# Add extra packages to the distribution
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr option location http://mirror.ctan.org/systems/texlive/tlnet/2013
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr option location /Users/koch/texlive2013/Master


# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install synctex
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install revtex
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install realscripts
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install luatexbase
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install lualibs
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install luaotfload.universal-darwin
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-basque
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-danish
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-dutch
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-finnish
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-french
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-german
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-hungarian
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-italian
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-norwegian
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-polish
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-portuguese
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-spanish
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-swedish
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install hyphen-ukenglish
# /usr/local/texlive/2013basic/bin/universal-darwin/tlmgr install times


# Remove the empty directory for local additions; this will be recreated by the postinstall script
rm -R /usr/local/texlive/2013basic/texmf-local


# rm -R /usr/local/texlive/2013basic/texmf/dvipdfm
# rm -R /usr/local/texlive/2013basic/texmf/dvipdfmx

# rm /usr/local/texlive/2013basic/bin/universal-darwin/dvipdfm
# rm /usr/local/texlive/2013basic/bin/universal-darwin/dvipdfmx

# rm -R /usr/local/texlive/2013basic/texmf-dist/fonts/pk

# rm -R /usr/local/texlive/2013basic/texmf-var/web2c/luatex
# rm -R /usr/local/texlive/2013basic/texmf-var/web2c/pdftex
# rm -R /usr/local/texlive/2013basic/texmf-var/web2c/xetex

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

# echo "Running texhash"
# /usr/local/texlive/2013basic/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

# Remove format files (not needed, formats aren't made)
# cd /usr/local/texlive/2013/texmf-local/web2c
# cd luatex
# rm *
# cd ..
# cd metafont
# rm *
# cd ..
# cd pdftex
# rm *
# cd ..
# cd xetex
# rm *
# cd ..




# Build the package!
# echo "Building the Installer package"
# cd ${WORK}
# ./../PackageMaker.app/Contents/MacOS/PackageMaker  -build -p ${WORK}/BasicTeX-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}/BasicTeX-2013.pkg  --target 10.5  \
# 	--root ${WORK}/root --resources ${WORK}/resources --id org.tug.mactex.basictex-2013  --verbose --no-recommend  \
# 	--root-volume-only --no-relocate --scripts ${WORK}/scripts

