#!/bin/sh
#
# runsim   Run an app in the iOS Simulator
#
# Copyright (c) 2012 Psellos   http://psellos.com/
# Licensed under the MIT License:
#     http://www.opensource.org/licenses/mit-license.php
#
USAGE='usage:  runsim  executable  [ nib/storyboard-file ]'
VERSION=1.0.8

# Figure out startup file, if any
NIBFILE=
STORYFILE=
case $# in
1)
    if [ -f "$1.nib" ]; then
        NIBFILE="$1"
    elif [ -f "$1.storyboard" ]; then
        STORYFILE="$1"
    fi
    ;;
2)
    case "$2" in
    *.nib) NIBFILE="$(basename "$2" .nib)" ;;
    *.storyboard) STORYFILE="$(basename "$2" .storyboard)" ;;
    *)
        if [ -f "$2.nib" ]; then
            NIBFILE="$2"
        elif [ -f "$2.storyboard" ]; then
            STORYFILE="$2"
        fi
        ;;
    esac
    ;;
*) echo "$USAGE" >&2; exit 1;;
esac

# Pick a uuid for the app (or reuse existing one).
if ! [ -f runsim.uuid ]; then
    uuidgen > runsim.uuid
fi
UUID=$(cat runsim.uuid)

TOPDIR="$HOME/Library/Application Support/\
iPhone Simulator/5.1/Applications/$UUID/"
mkdir -p "$TOPDIR"
mkdir -p "$TOPDIR/Documents"
mkdir -p "$TOPDIR/Library"
mkdir -p "$TOPDIR/tmp"
mkdir -p "$TOPDIR/$1.app"

cp $1 "$TOPDIR/$1.app"

if [ "$NIBFILE" != "" ]; then
    cp "$NIBFILE.nib" "$TOPDIR/$1.app"
elif [ "$STORYFILE" != "" ]; then
    cp "$STORYFILE.storyboard" "$TOPDIR/$1.app"
fi

# If Info.plist exists, use it.  Otherwise make one.
if [ -f Info.plist ]; then
    cp Info.plist "$TOPDIR/$1.app"
else
    cat > "$TOPDIR/$1.app/Info.plist" <<HERE1
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"\
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>English</string>
	<key>CFBundleDisplayName</key>
	<string>$1</string>
	<key>CFBundleExecutable</key>
	<string>$1</string>
        <key>CFBundleIconFile</key>
        <string>Icon.png</string>
	<key>CFBundleIdentifier</key>
	<string>com.example.$1</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>$1</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleSignature</key>
	<string>????</string>
        <key>CFBundleShortVersionString</key>
        <string>1.0</string>
	<key>CFBundleVersion</key>
	<string>1.0.0</string>
        <key>UIStatusBarStyle</key>
        <string>UIStatusBarStyleBlackOpaque</string>
	<key>LSRequiresIPhoneOS</key>
	<true/>
HERE1
    if [ "$NIBFILE" != "" ]; then
        cat >> "$TOPDIR/$1.app/Info.plist" << HERE2
	<key>NSMainNibFile</key>
	<string>$NIBFILE</string>
HERE2
    elif [ "$STORYFILE" != "" ]; then
        cat >> "$TOPDIR/$1.app/Info.plist" << HERE3
	<key>NSMainStoryboardFile</key>
	<string>$STORYFILE</string>
HERE3
    fi
    cat >> "$TOPDIR/$1.app/Info.plist" <<HERE4
</dict>
</plist>
HERE4
fi

echo -n 'AAPL????' > "$TOPDIR/$1.app/PkgInfo"

if [ -f Icon.png ]; then
    cp Icon.png "$TOPDIR/$1.app"
fi
if [ -f Default.png ]; then
    cp Default.png "$TOPDIR/$1.app"
fi

# Get location of Xcode, otherwise use default
if [ -f runsim.xcloc ]; then
    XCLOC="$(cat runsim.xcloc)"
else
    XCLOC=/Applications/Xcode.app
fi

open "$XCLOC"/Contents/\
Developer/Platforms/iPhoneSimulator.platform/\
Developer/Applications/iPhone\ Simulator.app
