#!/bin/bash


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

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

mkdir -p ${WORK}/rootDVD
echo "Changing permissions"
chmod -R a+r ${WORK}/rootDVD



# Ensure target directories we need for applications are present
mkdir -p ${WORK}/rootDVD/Applications/TeX
chmod 775 ${WORK}/rootDVD/Applications
chmod 755 ${WORK}/rootDVD
chmod 775 ${WORK}/rootDVD/Applications/TeX
chgrp admin ${WORK}/rootDVD/Applications
chgrp wheel ${WORK}/rootDVD
chgrp admin ${WORK}/rootDVD/Applications/TeX


# Get latest TeXShop-Leopard
# echo "Getting TeXShop-Leopard"
# ditto ${WORK}/apps/"TeXShop-Leopard.app" ${WORK}/rootDVD/Applications/TeX/"TeXShop-Leopard.app"

# Get latest TeXShop-Lion-NoMovie
echo "Getting TeXShop-Lion-NoMovie"
ditto ${WORK}/apps/"TeXShop-Lion-NoMovie.app" ${WORK}/rootDVD/Applications/TeX/"TeXShop-Lion.app"


# Get latest LaTeXiT
echo "Getting LaTeXiT"
ditto ${WORK}/apps/"LaTeXiT.app" ${WORK}/rootDVD/Applications/TeX/"LaTeXiT.app"

# Get BibDesk 
echo "Getting BibDesk"
ditto ${WORK}/apps/"BibDesk.app" ${WORK}/rootDVD/Applications/TeX/"BibDesk.app"

# Get TeX Live Utility
echo "Getting TeX Live Utility"
ditto ${WORK}/apps/"TeX Live Utility.app" ${WORK}/rootDVD/Applications/TeX/"TeX Live Utility.app"

# Get TeXworks-Leopard
# echo "Getting TeXworks-Leopard"
# ditto ${WORK}/apps/"TeXworks-Leopard.app" ${WORK}/rootDVD/Applications/TeX/"TeXworks-Leopard.app"

# Get TeXworks-SnowLeopard
# echo "Getting TeXworks-SnowLeopard"
# ditto ${WORK}/apps/"TeXworks-SnowLeopard.app" ${WORK}/rootDVD/Applications/TeX/"TeXworks-SnowLeopard.app"

# Get TeXworks-MountainLion
echo "Getting TeXworks-MountainLion"
ditto ${WORK}/apps/"TeXworks-MountainLion.app" ${WORK}/rootDVD/Applications/TeX/"TeXworks-MountainLion.app"

# Get FixMacTeX2013
echo "Getting FixMacTeX2013"
ditto ${WORK}/apps/"FixMacTeX2013.pkg" ${WORK}/rootDVD/Applications/TeX/"FixMacTeX2013.pkg"

# Get LuaTeX and XeTeX documents
echo "Getting LuaTeX and XeTeX documents"
ditto ${WORK}/apps/LuaTeX-and-XeTeX/ ${WORK}/rootDVD/Applications/TeX/LuaTeX-and-XeTeX/

# Copy Excalibur (not distributed on a .dmg, unfortunately)
 echo "Getting Excalibur"
 ditto ${WORK}/apps/Excalibur-4.0.7/ ${WORK}/rootDVD/Applications/TeX/Excalibur-4.0.7/
 
# Adding the README
 echo "Getting README FIRST"
 ditto ${WORK}/apps/"READ ME FIRST".pdf ${WORK}/rootDVD/Applications/TeX/"READ ME FIRST".pdf
 
# Adding "What Is Installed"
 echo "Getting What Is Installed"
 ditto ${WORK}/apps/"What Is Installed".pdf ${WORK}/rootDVD/Applications/TeX/"What Is Installed".pdf
 
 # Adding "Eps-and-PdfLaTeX"
 echo "Getting Eps-and-PdfLaTeX"
 ditto ${WORK}/apps/Eps-Tiff-Conversion.pdf ${WORK}/rootDVD/Applications/TeX/Eps-Tiff-Conversion.pdf
 
 # Changing Groups
 chgrp -R admin ${WORK}/rootDVD/Applications/TeX/*

# Build the package!
# echo "Building the Installer package"
# ./../PackageMaker.app/Contents/MacOS/PackageMaker  -build -p ${WORK}/"GUI-Applications.pkg" -ds -s \
# 	-f ${WORK}/rootDVD -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}/GUI-Applications.pkg  --target 10.5  \
# 	--rootDVD ${WORK}/rootDVD --resources ${WORK}/resources --id org.tug.mactex.gui  --verbose --no-recommend  \
# 	--rootDVD-volume-only --no-relocate --scripts ${WORK}/scripts

