#!/bin/bash

set -e
cd $(dirname $0)

if [[ ! -x node_modules/.bin/node-inspector ]]; then
  npm install node-inspector@0.12.5
  for patchfile in patch/*.patch; do
    patch -b -N -p1 -i $patchfile
    test $? -le 1 || exit 1
  done
fi

echo Forwarding 5858 port to device
function cleanup {
  echo Removing 5858 port forward
  ( set -x; adb forward --remove tcp:5858 )
}
trap cleanup EXIT
( set -x; adb forward tcp:5858 tcp:5858 )

# Exit cleanly on ^C
trap exit SIGINT
trap cleanup SIGHUP

node_modules/.bin/node-inspector "$@"
