#!/bin/zsh


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

echo "Checking that /usr/local content seems reasonable"
if [[ ! -d /usr/local/texlive/2022basic ]]; then
	echo "### error: main texlive directory is missing"
	exit 1
fi
 if [[ ! -x /usr/local/texlive/2022basic/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/2020basic/bin/universal-darwin/man

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

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


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


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


# Copy everything from /usr/local into our root
echo "Collecting files from /usr/local"
mkdir -p ${WORK}/root/usr/local/texlive
cp -pRf /usr/local/texlive/2022basic ${WORK}/root/usr/local/texlive


# 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



