#!/bin/sh

#much of this was based on work provided at:
#http://www.kernelthread.com/mac/apme/archive/mkdmg.txt

VERSION=`cat ./src/config.h | awk '/ALBUMSHAPER_VERSION/ {print $3}' | sed s/\"//g`
MAJORVERSION=`cat ./src/config.h | awk '/ALBUMSHAPER_VERSION/ {print $3}' | sed s/\"//g | sed 's/\./ /' | awk '{print $1}'`

BUNDLE_SOURCE="build/AlbumShaper_Clean.app"
BUNDLE_NAME="Album Shaper ${MAJORVERSION}.app"
DISK_IMAGE="albumshaper_${VERSION}_mac.dmg"
MOUNTED_DISK_NAME="Album Shaper ${VERSION}"

#output function
croak()
{
  echo "$1"
}

#return error message
chkerror()
{
  if [ $? -ne 0 ]
  then
    echo "Error!"
    exit 1
  fi
}

#check binary does not depend on xslt, xml2, or qt
echo "Checking dependencies..."
if [ -n "`otool -L build/AlbumShaper.app/Contents/MacOS/AlbumShaper | egrep '(xslt.dylib|xml2.dylib|libqt.dylib)'`" ]; then

XSLT=`otool -L build/AlbumShaper.app/Contents/MacOS/AlbumShaper | grep xslt`
XML2=`otool -L build/AlbumShaper.app/Contents/MacOS/AlbumShaper | grep xml2`
QT=`otool -L build/AlbumShaper.app/Contents/MacOS/AlbumShaper | grep libqt`

echo "Binary is not completely static!"
echo $XSLT
echo $XML2
echo $QT
exit
fi

#make sure all resources and frameworks are placed in bundle
echo "Copying resources and frameworks into bundle"
make install

#make clean bundle, which means removing stuff like CVS directories. 
#unfortunatley we can't do this using cp so instead we'll make a 
#copy of the bundle directory, tar it, remove the copy, then untar 
#it before copying it to the disk image
echo "Cleaning bundle..."
cd build
cp -R AlbumShaper.app AlbumShaper_Clean.app
tar --exclude 'CVS' --exclude '.xvpics' -cf AlbumShaper_Clean.app.tar AlbumShaper_Clean.app
rm -rf AlbumShaper_Clean.app
tar -xf AlbumShaper_Clean.app.tar
rm -f AlbumShaper_Clean.app.tar
cd ../.

#estimate space required for disk image
SIZE=`du -s -k $BUNDLE_SOURCE | awk '{print $1}'`
chkerror
SIZE=`expr 5 + $SIZE / 1000`
chkerror
croak "Using $SIZE MB"

#remove old disk and sparse images if they exist
rm -f ../tmp/${DISK_IMAGE}
rm -rf ${SPARSE_IMAGE}

#create sparse image
hdiutil create "${DISK_IMAGE}.sparseimage" -volname "${MOUNTED_DISK_NAME}" -megabytes $SIZE -type SPARSE -fs HFS+ 2>/dev/null >/dev/null
chkerror
croak "${DISK_IMAGE}.sparseimage created"

#mount sparse image
hdiutil mount ./${DISK_IMAGE}.sparseimage 2>/dev/null >/dev/null
#hdid ./${DISK_IMAGE}.sparseimage 2>/dev/null >/dev/null
chkerror
croak "${DISK_IMAGE}.sparseimage mounted"

#find out allocated device
DEV=`mount | grep "Volumes/${MOUNTED_DISK_NAME}" | awk '{print $1}'`
croak "Device in use is $DEV"

# Use ditto to copy everything to the image, preserving resource forks
ditto -rsrcFork $BUNDLE_SOURCE "/Volumes/${MOUNTED_DISK_NAME}/${BUNDLE_NAME}" 2>/dev/null >/dev/null
chkerror
croak "Bundle coppied over successfully"

# Copy over the disk image background and hide it
#BACKGROUND="resources/icons/as128.png"
#ditto -rsrcFork $BACKGROUND "/Volumes/$MOUNTED_DISK_NAME/diskImageBackground.png" 2>/dev/null >/dev/null
#/Developer/Tools/SetFile -a V "/Volumes/$MOUNTED_DISK_NAME/diskImageBackground.png" 2>/dev/null >/dev/null
#chkerror
#croak "Disk image background copied, hide, and set"

# Detach the disk image
hdiutil detach $DEV 2>/dev/null >/dev/null
chkerror
croak "$DEV detached"

# Compress the image (maximum compression)
hdiutil convert "${DISK_IMAGE}.sparseimage" -format UDZO -o "${DISK_IMAGE}" -imagekey zlib-devel=9 2>/dev/null >/dev/null
chkerror
croak "Disk image successfully compressed"

#remove sparseimage folder
echo "Removing ${DISK_IMAGE}.sparseimage"
rm -rf ${DISK_IMAGE}.sparseimage

#remove clean app bundle
echo "Removing ${BUNDLE_SOURCE}"
rm -rf ${BUNDLE_SOURCE}

#make disk image interent enabled and announce being done
hdiutil internet-enable -yes ${DISK_IMAGE} 2>/dev/null >/dev/null
mv ${DISK_IMAGE} ../tmp/.
chkerror
croak "${DISK_IMAGE} created!"

