#!/bin/bash

SELF=`basename $0`
SELF_DIR=`dirname $0`

#
# Run the gaia unit test agent server and with firefox.
#
# Basic usage:
#
#   gaia-test <gaia location> <firefox binary>
#
# The both parameters are optional.
#
# The gaia directory to test is found using the following heuristic:
#   1) If the gaia location specified on the command line is a directory,
#      then use it.
#   2) If the gaia location is not specified, use the $GAIADIR environment
#      variable.
#   2) If the environment variable is not specified either, then use
#      the scripts location to determine the gaia directory.  This assumes
#      the script is found in gaia/bin.
#
# The firefox binary is found using the following heuristic:
#   1) If the firefox binary is specified on the command line, then attempt
#      to find it in the path if its relative or just use it if its absolute.
#   2) If the firefox binary is not specified, then use the $FIREFOX
#      environment variable to find the executable in the path.
#   3) If the environment is not specified either, then look for 'firefox'
#      in the path.
#

usage() {
  echo "$SELF [ -d ] [ -f ] [ -g ] [ -h ] [ <gaia dir> [ <firefox/B2G binary> ] ]"
  echo "  -d: runs in B2G Desktop"
  echo "  -f: forces the profile generation -- will delete an existing profile directory"
  echo "  -g: runs in gdb"
  echo "  -h: displays this help"
  echo
  echo "Use the environment variable FIREFOX to point to a specific firefox binary."
  echo "Use the environment variable B2G to point to a specific B2G Desktop binary."
  echo "Use the environment variable GAIADIR to point to a specific Gaia directory."
  echo -n "Use the environment variable PROFILE_FOLDER to change the profile directory."
  echo " It should be a subdirectory of the Gaia directory."
  echo "Use the environment variable GDB_ARGS to pass arguments to gdb."
  exit 1
}

handle_options() {
  mode="firefox"
  force_profile=
  runs_in_gdb=
  while getopts dfgh f; do
    case $f in
      d)
        mode="b2g"
        ;;
      f)
        force_profile=1
        ;;
      g)
        runs_in_gdb="gdb $GDB_ARGS --args"
        ;;
      h|\?)
        usage
        ;;
    esac
  done
}

configure_gaia_location() {
  #
  # Configure gaia location
  #
  if [ ! -z "$1" ] ; then
    GAIADIR="$1"
  fi

  if [ -z "$GAIADIR" ] ; then
    GAIADIR="$SELF_DIR/.."
  fi

  if [ ! -d "$GAIADIR" ] ; then
    echo "$SELF: '$GAIADIR' is not a directory!"
    exit 1
  fi

  GAIADIR=$( cd "$GAIADIR"; pwd)
  echo "$SELF: Using gaia at '$GAIADIR'."
}

configure_firefox_location() {
  #
  # Configure firefox test binary
  #
  if [ ! -z "$1" ] ; then
    FIREFOX="$1"
  fi

  if [ -z "$FIREFOX" ] ; then
    FIREFOX='firefox'
  fi

  BASE_FIREFOX="$FIREFOX"
  FIREFOX=`which $BASE_FIREFOX`
  if [ ! -x "$FIREFOX" ] ; then
    echo "$SELF: Unable to find an executable '$BASE_FIREFOX' in path!"
    exit 1
  fi

  #
  # Verify firefox version is new enough
  #
  VERSION=`$FIREFOX --version | awk '{print $3}'`
  REQ_VERSION='22'
  if expr "$VERSION" \< "$REQ_VERSION" >/dev/null ; then
    echo "$SELF: Firefox is only version $VERSION; use $REQ_VERSION or newer."
    exit 1
  fi

  echo "$SELF: Using Firefox $VERSION at '$FIREFOX'."
}

configure_b2g_location() {
  #
  # Configure mulet test binary
  #
  if [ ! -z "$1" ] ; then
    B2G=`which $1`
    if [ ! -x "$B2G" ] ; then
      echo "$SELF: Unable to find an executable '$1' in path!"
      exit 1
    fi
  fi

  if [ -z "$B2G" ] ; then
    make mulet
    B2G="./firefox/firefox-bin"
    if [ ! -x "$B2G" ] ; then
      # set the path for Mac OS X
      B2G="./firefox/Contents/MacOS/firefox-bin"
    fi
  fi

  # if we still don't have it, fail
  if [ ! -x "$B2G" ] ; then
    echo "$SELF: Couldn't download a suitable executable for B2G Desktop"
    exit 1
  fi

  echo "$SELF: Using B2G Desktop at '$B2G'."
}

configure_profile_folder() {
  if [ -z "$PROFILE_FOLDER" ] ; then
    PROFILE_FOLDER="profile-gaia-test-$mode"
  fi
  export PROFILE_FOLDER

  echo "$SELF: Using profile at '$PROFILE_FOLDER'"
}

generate_profile_folder_if_needed() {
  if [ -n "$force_profile" ] ; then
    rm -rf "$PROFILE_FOLDER"
  fi

  if [ ! -d "$PROFILE_FOLDER" ] ; then
    echo "$SELF: Building the debug profile."
    NO_LOCK_SCREEN=1 DEBUG=1 DESKTOP=0 make
  fi

}

launch_test_runner_in_firefox() {
  #
  # Launch firefox
  #
  $runs_in_gdb $FIREFOX --no-remote -profile "$GAIADIR/$PROFILE_FOLDER" app://test-agent.gaiamobile.org/
}

launch_test_runner_in_b2g() {
  $runs_in_gdb $B2G -profile "$GAIADIR/$PROFILE_FOLDER" --runapp 'Test Agent'
}

prepare_npm_modules() {
  make node_modules
}


handle_options "$@" && shift `expr $OPTIND - 1`
configure_gaia_location "$1"
configure_${mode}_location "$2"

#
# Verify additional dependencies are installed
#
NODE=`which nodejs`
NPM=`which npm`
if [ ! -x "$NODE" ] ; then
  NODE=`which node`
  if [ ! -x "$NODE" ] ; then
    echo "$SELF: Unable to find node; please install node.js."
    exit 1
  fi
elif [ ! -x "$NPM" ] ; then
  echo "$SELF: Unable to find npm; please install npm."
  exit 1
fi

cd $GAIADIR

configure_profile_folder
generate_profile_folder_if_needed
prepare_npm_modules

cleanup() {
  echo "$SELF: Stopping the test runner on exit."
  kill "%1"
}
trap cleanup EXIT

#
# Launch the test agent
#
make test-agent-server &

launch_test_runner_in_${mode}
