#!/usr/bin/env bash 
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Valid values for "--target" (case insensitive):
#     "iPhone (Retina 3.5-inch)" (default)
#     "iPhone (Retina 4-inch)"
#     "iPhone"
#     "iPad"
#     "iPad (Retina)"

OPTION_RESULT=()
TARGET="iPhone (Retina 3.5-inch)"
TARGET_FLAG='--family iphone --retina'

# separates "key=value", sets an array with 0th index as key, 1st index as value
_parseOption() 
{
    local ARG=$@
    OIFS=$IFS
    IFS='='

    OPTION_RESULT=()
    for i in ${ARG[@]}
    do
      OPTION_RESULT+=($i)
    done

    IFS=$OIFS
}

# parses key=value arguments
_parseArgs()
{
  for arg in "$@"
  do
    _parseOption ${arg}
    case "${OPTION_RESULT[0]}" in
      "--target")
        TARGET=${OPTION_RESULT[1]}
        ;;
    esac
  done
}

_parseArgs "$@"

# lowercase the target string
TARGET_LC=`echo $TARGET | tr '[:upper:]' '[:lower:]'` 

# remember the comparison is lowercase
case "$TARGET_LC" in
  "iphone (retina 3.5-inch)")
    TARGET_FLAG="--family iphone --retina"
    ;;
    "iphone (retina 4-inch)")
    TARGET_FLAG="--family iphone --retina --tall"
    ;;
    "iphone")
    TARGET_FLAG="--family iphone"
    ;;
    "ipad")
    TARGET_FLAG="--family ipad"
    ;;
    "ipad (retina)")
    TARGET_FLAG="--family ipad --retina"
    ;;
esac

XCODE_VER=$(xcodebuild -version | head -n 1 | sed -e 's/Xcode //')
XCODE_MIN_VERSION="4.5"

if [[ "$XCODE_VER" < "$XCODE_MIN_VERSION" ]]; then
	echo "Cordova can only run in Xcode version $XCODE_MIN_VERSION or greater."
	exit 1
fi

LIB_PATH=$( cd "$( dirname "$0" )" && pwd -P)
CORDOVA_PATH="$(dirname "$LIB_PATH")"
PROJECT_PATH="$(dirname "$CORDOVA_PATH")"
XCODEPROJ=$( ls "$PROJECT_PATH" | grep .xcodeproj  )
PROJECT_NAME=$(basename "$XCODEPROJ" .xcodeproj)

APP_PATH="$PROJECT_PATH/build/$PROJECT_NAME.app"

if [ ! -d "$APP_PATH" ]; then
	echo "Project '$APP_PATH' is not built."
    exit 1
fi

if [ ! -d "$APP_PATH" ]; then
	echo "$APP_PATH not found to emulate."
	exit 1
fi

IOS_SIM_MIN_VERSION="1.5"
IOS_SIM_LOCATION=$(which ios-sim)
if [ $? != 0 ]; then
    echo -e "\033[31mError: ios-sim was not found. Please download, build and install version $IOS_SIM_MIN_VERSION or greater from https://github.com/phonegap/ios-sim into your path. Or 'brew install ios-sim' using homebrew: http://mxcl.github.com/homebrew/\033[m"; exit 1;
	exit 1
fi

IOS_SIM_VERSION=$(ios-sim --version)

if [[ "$IOS_SIM_VERSION" < "$IOS_SIM_MIN_VERSION" ]]; then
	echo "Cordova needs ios-sim version $IOS_SIM_MIN_VERSION or greater, you have version $IOS_SIM_VERSION."
	exit 1
fi

# launch using ios-sim
ios-sim launch "$APP_PATH" --stderr "$CORDOVA_PATH/console.log" --stdout "$CORDOVA_PATH/console.log" $TARGET_FLAG --exit


