#!/bin/bash


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

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

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

PATH=/usr/local/texlive/2017basic/bin/x86_64-darwin:$PATH; export PATH


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


echo "Resetting tlmgr source"
/usr/local/texlive/2017basic/bin/x86_64-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/2017basic ${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



