#!/bin/sh

if [ -f mac_release_build ]
then
   echo 'You must execute this script from the base Audacity directory'
   exit 1
fi

plugs='gverb_1216.so hard_limiter_1413.so sc4_1882.so'
for plug in $plugs
do
   if [ ! -f plug-ins/$plug ]
   then
      echo "Missing plugin: $plug"
      echo
      echo "You must copy the following to the plug-ins directory before building:"
      echo
      echo $plugs
      echo
      exit 1
   fi
done

#
# Build it
#
cd mac
xcodebuild -configuration 'Release Static'
cd ..

# Setup
VERSION=`awk '/^#define+ AUDACITY_VERSION / {print $3}' src/Audacity.h`
RELEASE=`awk '/^#define+ AUDACITY_RELEASE / {print $3}' src/Audacity.h`
REVISION=`awk '/^#define+ AUDACITY_REVISION / {print $3}' src/Audacity.h`

if [ ! -z "$SUFFIX" ]
then
   SUFFIX="-$SUFFIX"
fi

VERSION=$VERSION.$RELEASE.$REVISION$SUFFIX

VOL="Audacity $VERSION"
DMG="audacity-macosx-ub-$VERSION"

# Preclean
rm -rf "$DMG" "$DMG.dmg" TMP.dmg

# Create structure
mkdir "$DMG"

# Copy directories
SUBDIRS="Audacity.app Languages nyquist plug-ins modules"
for SUBDIR in $SUBDIRS
do
   cp -pR "mac/build/Release Static/$SUBDIR" "$DMG"
done

# Retrieve and Copy help
cd scripts/mw2html_audacity
./wiki2htm.sh
cd ../..
mkdir "$DMG/help"
mv "help/manual" "$DMG/help/"

# Strip binary
strip "$DMG/Audacity.app/Contents/MacOS/"*

# Install misc files
cp -pR README.txt LICENSE.txt "$DMG"

# Create and mount the image
hdiutil create -ov -srcdir "$DMG" -fs HFS+ -volname "$VOL" TMP.dmg

# Compress and prepare for Internet delivery
hdiutil convert TMP.dmg -format UDZO -imagekey zlib-level=9 -o "$DMG.dmg"
hdiutil internet-enable -yes "$DMG.dmg"

# Cleanup
rm TMP.dmg

# Move DMG to parent
mv *.dmg ..

# Remove help contents
rm -rf "$DMG/help/"*

# Create the zip version
zip -r9 "../$DMG.zip" "$DMG"

exit