#!/bin/bash

######################################################
# Manual preparation before running this...
#
# SAVE ORIGINAL /usr/local/texlive AND MOVE IT OUT OF THE WAY
# sudo mv /usr/local/texlive /usr/local/texlive-old
#
# Install TeXLive-2018
#
######################################################

# 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/2018 ]]; then
	echo "### error: main texlive directory is missing"
	exit 1
fi

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

echo "Resetting tlmgr source"
/usr/local/texlive/2018/bin/x86_64-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/2018/bin/x86_64-darwin/man


echo "Running texhash"
/usr/local/texlive/2018/bin/x86_64-darwin/texhash

# Copy everything from /usr/local into our root
echo "Collecting files from /usr/local"
mkdir -p ${WORK}/root/usr/local
cp -pRf /usr/local/texlive ${WORK}/root/usr/local
# 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


 

