#!/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.
#

# support for this script on BB10 is dependent on vmware tools being on the path, or in the default install directory
# Valid values for "--target": path to 'vmwarevm' file

VMWARE_DEFAULT_PATH=/Applications/VMware\ Fusion.app/Contents/Library
VMWARE_TOOL=vmrun
OS_NAME=`uname`

if [ "$1" = "" -o ! -e "$1" ]
then
    echo "BlackBerry10: must provide path to valid vmwarevm image"
    exit 2
fi

if [ "$OS_NAME" = "Darwin" ]; then
    if [ "`which vmrun`" = "" ]; then
        echo "BlackBerry10: VMware Fusion 'vmrun' tool not found on path, trying default install path"
        runpath=$VMWARE_DEFAULT_PATH/$VMWARE_TOOL
    else
        runpath=`which vmrun`
    fi
    
    if [ -x "$runpath" ]; then
        echo $runpath start $1
        `$"$runpath" start "$1" > /dev/null`

        exit 0 
    else
        echo "BlackBerry10: could not find VMware Fusion 'vmrun' tool"
        exit 1 
    fi
else
    echo "BlackBerry10: currently only supports installing to emulator on OS X"
    exit 1
fi
