#!/bin/bash
#
# Run a test with the WebRTC Chromium build.
# Should work on any machine with a camera.
#
# Method:
# - Start server
# - Start 2 browser tabs
# - Browser tab 1 captures camera
# - Both browsers sign in
# - Browser 1 calls browser 2
# - Browser 2 displays camera feed from browser 1
#

# Feel free to tweak this locally if your chrome build is somewhere else.
# The default assumes that it is in a folder called chromium two levels
# up from the project root ('trunk').

set -e

if [ ! -x run_sanity_check ]; then
  echo "Error: This script must run from its own directory"
  exit 1
fi

URLBASE=127.0.0.1:3000
CALLER=$URLBASE/call_client.html
CALLEE=$URLBASE/call_responder.html
FLAGS="--enable-media-stream --enable-p2papi"
PROJECT_ROOT=../..
CHROME_BINARY=$PROJECT_ROOT/../../chromium/src/out/Debug/chrome

if which lighttpd; then
  LOCAL_WEB_SERVER_BINARY=$(which lighttpd)
else
  echo "Error: You must install lighttpd first (sudo apt-get install lighttpd)"
  exit 1
fi

SERVER_BINARY=${PROJECT_ROOT}/out/Debug/peerconnection_server
if [ ! -e "$SERVER_BINARY" ]; then
  echo "Error: You must build peerconnection_server first."
  exit 1
fi

CHROME_BINARY=$PROJECT_ROOT/../../chromium/src/out/Debug/chrome
if [ ! -e "$CHROME_BINARY" ]; then
  echo "Error: You must build chrome (could not open $CHROME_BINARY)."
  exit 1
fi

# Launch the web server and make it serve the local www/html directory
${LOCAL_WEB_SERVER_BINARY} -D -f lighttpd.conf &
LOCAL_WEB_SERVER=$!

${SERVER_BINARY} &
SERVER=$!
echo "Started server as $SERVER"

# We can make 2 browsers
#${CHROME_BINARY} $CALLER $FLAGS --user-data-dir=temp/user1 &
#USER1=$!
#echo "Started first user as $USER1"
#${CHROME_BINARY} $CALLEE $FLAGS --user-data-dir=temp/user2 

# But it also works with separate tabs in one browser.
${CHROME_BINARY} $CALLER $CALLEE $FLAGS --user-data-dir=temp/user1

echo "Test finished, cleaning up"

kill $SERVER || echo "No server"
kill $LOCAL_WEB_SERVER || echo "No local web server"
