#!/bin/bash


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

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

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



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


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

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


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

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

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

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

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

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

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

# Copy Excalibur (not distributed on a .dmg, unfortunately)
 echo "Getting Excalibur"
 ditto ${WORK}/apps/Excalibur-4.0.7/ ${WORK}/root/Applications/TeX/Excalibur-4.0.7/
 
# Adding the README
 echo "Getting README FIRST"
 ditto ${WORK}/apps/"READ ME FIRST".pdf ${WORK}/root/Applications/TeX/"READ ME FIRST".pdf
 
# Adding "What Is Installed"
 echo "Getting What Is Installed"
 ditto ${WORK}/apps/"What Is Installed".pdf ${WORK}/root/Applications/TeX/"What Is Installed".pdf
 
 # Adding "Eps-and-PdfLaTeX"
 echo "Getting Eps-and-PdfLaTeX"
 ditto ${WORK}/apps/Eps-Tiff-Conversion.pdf ${WORK}/root/Applications/TeX/Eps-Tiff-Conversion.pdf
 
 # Changing Groups
 chgrp -R admin ${WORK}/root/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}/root -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  \
# 	--root ${WORK}/root --resources ${WORK}/resources --id org.tug.mactex.gui  --verbose --no-recommend  \
# 	--root-volume-only --no-relocate --scripts ${WORK}/scripts

