#!/bin/sh +x

# $1: device IP
# $2: emulator.log file path
# $3: sdb path


if [ "x$1" = "x" ] || [ "x$2" = "x" ] || [ "x$3" = "x" ];then
    echo "Incorrect arguments passed"
    #IP=192.168.7.2
    #EMULATOR_LOG_PATH=~/tizen-studio-data/emulator/vms/t-0531-1/logs/emulator.log
    #SDB_PATH=~/tizen-studio/tools/sdb
    exit 1
else
    IP=$1
    EMULATOR_LOG_PATH=$2
    SDB_PATH=$3
fi

SUBSTR="boot completed"

echo "Waiting for the emulator to boot up"
(timeout 120s tail -F $EMULATOR_LOG_PATH &) | grep -q "$SUBSTR"
#RETVALUE="$?"
#echo "Trying sdb connect $1"

#echo "Return value is $RETVALUE"
#if [ "$RETVALUE" = "124" ];then
#    echo "Emulator boot up taking longer than usual. Run the following command to connect to the emulator manually. sdb connect <IP>"
#    exit 1
#else
    echo "$SDB_PATH connect $IP"
    $SDB_PATH connect $IP
    exit 0
#fi
