#!/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 retina (4-inch 64-bit)")
    TARGET_FLAG="--family iphone --retina --tall --64bit"
    ;;
    "iphone")
    TARGET_FLAG="--family iphone"
    ;;
    "ipad")
    TARGET_FLAG="--family ipad"
    ;;
    "ipad retina")
    TARGET_FLAG="--family ipad --retina"
    ;;
esac

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)

SIMULATOR_APP_PATH="$PROJECT_PATH/build/emulator/$PROJECT_NAME.app"

source "$CORDOVA_PATH/check_reqs"

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

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

IOS_SIM_MIN_VERSION="1.7"
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 'npm install -g ios-sim' using node.js: http://nodejs.org/\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 "$SIMULATOR_APP_PATH" --stderr "$CORDOVA_PATH/console.log" --stdout "$CORDOVA_PATH/console.log" $TARGET_FLAG --exit


