#!/bin/sh

# usage: ./mkdmg-osx.sh release/debug (case insensitive)
# Allows us to run mkdmg-osx.sh from anywhere in the three, rather than the tools/darwin/packaging/osx folder only
SWITCH=`echo $1 | tr [A-Z] [a-z]`
DIRNAME=`dirname $0`

if [ ${SWITCH:-""} = "debug" ]; then
  echo "Packaging Debug target for OSX"
  APP="$DIRNAME/../../../../build/Debug/@APP_NAME@.app"
elif [ ${SWITCH:-""} = "release" ]; then
  echo "Packaging Release target for OSX"
  APP="$DIRNAME/../../../../build/Release/@APP_NAME@.app"
else
  echo "You need to specify the build target"
  exit 1
fi

if [ ! -d $APP ]; then
  echo "@APP_NAME@.app not found! are you sure you built $1 target?"
  exit 1
fi
ARCHITECTURE=`file $APP/Contents/MacOS/@APP_NAME@ | awk '{print $NF}'`

PACKAGE=org.xbmc.@APP_NAME_LC@-osx

VERSION=@APP_VERSION_MAJOR@.@APP_VERSION_MINOR@
REVISION=0

if [ "@APP_VERSION_TAG_LC@" != "" ]; then
  REVISION=$REVISION~@APP_VERSION_TAG_LC@
fi

ARCHIVE=${PACKAGE}_${VERSION}-${REVISION}_macosx-intel-${ARCHITECTURE}

echo Creating $PACKAGE package version $VERSION revision $REVISION
rm -rf $DIRNAME/$ARCHIVE.dmg

if [ -e "/Volumes/@APP_NAME_LC@" ]; then
  umount /Volumes/@APP_NAME_LC@
fi

#generate volume iconset
if [ `which iconutil` ]
then
  echo "Generating volumeIcon.icns"
  iconutil -c icns --output "VolumeIcon.icns" "../media/osx/volumeIcon.iconset"
fi

$DIRNAME/dmgmaker.pl $APP $ARCHIVE

echo "done"
