#!/bin/zsh

######################################################
# 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-2022
#
######################################################

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

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

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


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

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


 

