#!/bin/bash

# Script used on a Mac as part of the Enigma release process.
# Run this on a machine that has Enigma installed by Hombrew to create a dmg suitable for distribution.
# The script copies files installed in /usr/local/bin/enigma and /usr/local/share/enigma to make an app bundle

# Fixes up the library dependencies in the application bundle
# and creates a Enigma.dmg file in the current directory suitable for distribution to systems without Homebrew
# Generates a few warnings if the application bundle has already been fixed up, but doesn't break it
# If there is an existing Enigma.dmg file in the current directory it will be deleted and a new one created
# Result is a file Enigma.dmg in the directory that is current when this is run.

# This should be installed by a Homebrew formula to ensure that tools it depends on are installed
# enigma osxutils imagemagick fileicon create-dmg dylibbundler

# check for required utilities that can be installed using brew
echo "Checking for packages that are required to run this script. If any are missing run"
echo "brew install enigma osxutils imagemagick fileicon create-dmg dylibbundler"
hash mkalias 2>/dev/null  || { echo >&2 "This script requires mkalias from osxutils but it's not installed. Install osxutils from homebrew. Aborting."; exit 1; }
hash convert 2>/dev/null  || { echo >&2 "This script requires ImageMagick convert but it's not installed. Install imagemagick from homebrew. Aborting."; exit 1; }
for foo in fileicon create-dmg dylibbundler ; do
    hash $foo 2>/dev/null || { echo >&2 "This script requires $foo but it's not installed. Install it from homebrew. Aborting."; exit 1; }
done

DEST=/tmp/Enigma_temp_dist_build
DMG_FILE_NAME="Enigma.dmg"
ENIGMA_APP_DIR=`brew --prefix enigma`
ENIGMABUILDDMG_DIR="$ENIGMA_APP_DIR/etc"
ENIGMA_APP_PATH="$DEST/Enigma/Enigma.app"
#BACKGROUND_IMAGE="$ENIGMA_APP_PATH/Contents/Resources/doc/images/menu_bg.jpg"
BACKGROUND_IMAGE="$ENIGMABUILDDMG_DIR/menu_bg.jpg"
MESSAGE_IN_FILE_NAME1=".dummy1"
MESSAGE_IN_FILE_NAME2=".dummy2"

if [ -z "$ENIGMA_APP_DIR" ]; then
    echo "Error: No enigma installed in homebrew"
    exit 1
fi

if [ ! -d "$ENIGMA_APP_DIR" ]; then
    echo "Error: No installed directory '$ENIGMA_APP_DIR'"
    exit 1
fi

if [ ! -d "$ENIGMABUILDDMG_DIR" ]; then
    echo "Error: No installed directory '$ENIGMABUILDDMG_DIR'"
    exit 1
fi

### create an app bundle in $DEST/Enigma/Enigma.app
# it will need files from the installation of this script Info.plist enigma.icns
# the rest come from the installed files in /usr/local
rm -rf "$DEST"
mkdir -p "$ENIGMA_APP_PATH/Contents/MacOS"
cp "$ENIGMA_APP_DIR/bin/enigma" "$ENIGMA_APP_PATH/Contents/MacOS/"
mkdir -p "$ENIGMA_APP_PATH/Contents/Resources"
cp -a "$ENIGMA_APP_DIR/share/enigma" "$ENIGMA_APP_PATH/Contents/Resources/data"
cp -a "$ENIGMA_APP_DIR/share/doc/enigma" "$ENIGMA_APP_PATH/Contents/Resources/doc"
mkalias -r "$ENIGMA_APP_PATH/Contents/Resources/doc/index.html" "$DEST/Enigma/EnigmaDocs.html"
echo "APPL????" > "$ENIGMA_APP_PATH/Contents/PkgInfo"
cp "$ENIGMABUILDDMG_DIR/Info.plist" "$ENIGMA_APP_PATH/Contents/"
cp "$ENIGMABUILDDMG_DIR/enigma.icns" "$ENIGMA_APP_PATH/Contents/Resources/"

if [ ! -d "$ENIGMA_APP_PATH" ]; then
    echo "Error: No application path '$ENIGMA_APP_PATH'"
    exit 1
fi

if [ ! -f "$BACKGROUND_IMAGE" ]; then
    echo "Error: No background image file '$BACKGROUND_IMAGE'"
    exit 1
fi


# if dylibbundler doesn't work right, take a look at github.com/chearon/macpack
# setting DYLD_LIBRARY_PATH and the -i option allows this to be run more than once on the same file, with only a few warnings as a result
DYLD_LIBRARY_PATH="$ENIGMA_APP_PATH/Contents/lib" dylibbundler -cd -x $ENIGMA_APP_PATH/Contents/MacOS/enigma -b -i $ENIGMA_APP_PATH/Contents/lib/ -d $ENIGMA_APP_PATH/Contents/lib/ -p @executable_path/../lib/

# Create some files on the fly that will be used to put a message on the dmg folder
rm -f "$DMG_FILE_NAME"
touch "$DEST/$MESSAGE_IN_FILE_NAME1"
touch "$DEST/$MESSAGE_IN_FILE_NAME2"
convert -size 16x16 xc:transparent "$DEST/.transparent.ico"
fileicon set "$DEST/$MESSAGE_IN_FILE_NAME1" "$DEST/.transparent.ico"
fileicon set "$DEST/$MESSAGE_IN_FILE_NAME2" "$DEST/.transparent.ico"

# spaces at start of message file names are there to get Finder list view sorted by name to display them in proper order. The coordinates in the command place them for icon view
create-dmg --no-internet-enable --volname Enigma --background "$BACKGROUND_IMAGE" --window-pos 50 60 --window-size 400 320 --icon-size 50 --icon "Enigma" 20 50 --app-drop-link 200 50 --add-file "  Enigma folder contains app and manual" "$DEST/$MESSAGE_IN_FILE_NAME1" 10 130 --add-file " Drag it to Applications to install" "$DEST/$MESSAGE_IN_FILE_NAME2" 210 130 --text-size 14 "$DMG_FILE_NAME" "$DEST"

rm -rf "$DEST"
