#!/bin/bash

# Usage: ./run_wasm_bench [-z] [niters]
# Use -z to benchmark the legacy zkp code; otherwise benchmark our sigma-rs code
# niters defaults to 10

# cd into the directory containing this script (from the bash faq 028)
if [[ $BASH_SOURCE = */* ]]; then
  cd -- "${BASH_SOURCE%/*}/" || exit
fi

# See if we're benchmarking the legacy (zkp) code or our (sigma-rs) code
if [ "$1" = "-z" ]; then
    shift
    loxdir="../application-lox-zkp"
else
    loxdir="../application-lox"
fi

if [ "$1" = "" ]; then
    niters=10
else
    niters="$1"
fi

cleanup() {
    echo "Cleaning up..."
    if [ "$rdsyspid" != "" ]; then
	kill $rdsyspid
    fi
    if [ "$webserverpid" != "" ]; then
	kill $webserverpid
    fi
    echo "Done"
}

trap cleanup EXIT SIGINT SIGTERM

# Start the rdsys backend
cd ../rdsys
./backend -config conf/config.json &
rdsyspid=$!

cd "$loxdir/crates/lox-wasm"
# Touch the wasm files so that the zkp ones aren't used for the sigma-rs
# benches or vice versa
touch index.js index.html pkg/*
# Start the web server, which will itself manage the lox distributor
../../../Scripts/wasm_server $niters &
webserverpid=$!

sleep 2
echo
echo "*******************"
echo "Please visit http://127.0.0.1:8000/ in a wasm-capable web browser"
echo "*******************"
echo

wait $webserverpid
webserverpid=""

../../../Scripts/wasm_parser
