#!/bin/bash
# ############################################################################
# run-test.sh: script to run multidomain test.
# ############################################################################

## Please dont name a domain "fresh"
if [ $ARG_SIZE == 0 ] ; then
  echo "Must call with arguments, as follows:"
  echo "  ./run-test.sh fresh"
  echo "  ./run-test.sh fresh client-domain-name server-domain-name"
  echo "  ./run-test.sh run  client-domain-name server-domain-name"
  exit
fi

echo " NOT WORKING YET"
exit


# if [[ $ARG_SIZE != 1 && $ARG_SIZE != 2 ]] ; then
  # echo "Wrong number of arguments"
  # echo "Must call, as follows:"
  # echo "  ./run-test.sh fresh"
  # echo "  ./run-test.sh fresh domain-name"
  # echo "  ./run-test.sh run"
  # echo "  ./run-test.sh run domain-name"
  # exit
# fi

# if [[ $ARG_SIZE == 1  && $1 == "fresh" ]] ; then
  # DOMAIN_NAME="datica-test"
# fi
# if [[ $ARG_SIZE == 2  && $1 == "fresh" ]] ; then
  # DOMAIN_NAME=$2
# fi
# if [[ $ARG_SIZE == 1  && $1 == "run" ]] ; then
  # DOMAIN_NAME="datica-test"
# fi
# if [[ $ARG_SIZE == 2  && $1 == "run" ]] ; then
  # DOMAIN_NAME=$2
# fi

# echo "domain name: $DOMAIN_NAME"

# POLICY_KEY_FILE_NAME="policy_key_file.$DOMAIN_NAME"
# POLICY_CERT_FILE_NAME="policy_cert_file.$DOMAIN_NAME"
# echo "policy key file name: $POLICY_KEY_FILE_NAME"
# echo "policy cert file name: $POLICY_CERT_FILE_NAME"

# POLICY_STORE_NAME="policy_store.$DOMAIN_NAME"
# echo "policy store name 1: ./app1_data/$POLICY_STORE_NAME
# echo "policy store name 2: ./app2_data/$POLICY_STORE_NAME
# 
function cleanup_stale_procs() {
  echo "cleanup_stale_procs"

  # Find and kill simpleserver processes that may be running.
  echo " "
  set +e
  certifier_pid=$(ps -ef | grep -E "simpleserver" | grep -v -w -E 'grep|vi|vim' | awk '{print $2}')
  set -e
  if [[ $certifier_pid != "" ]] ; then
    kill -9 $certifier_pid
    echo "killed certifier_service, pid $certifier_pid"
  else
    echo "no certifier_service running"
  fi

  # Find and kill simpleserver processes that app serverrunning.
  echo " "
  set +e
  app_pid=$(ps -ef | grep -E "example_app" | grep -v -w -E 'grep|vi|vim' | awk '{print $2}')
  set -e

  if [[ $app_pid != "" ]] ; then
    kill -9 $app_pid
    echo "killed app server, pid: $app_pid"
  else
    echo "no app server running"
  fi

  echo "cleanup_stale_procs done"
}

#function do-fresh() {
  #echo "do-fresh"
#
#  pushd $EXAMPLE_DIR
#    rm ./app1_data/$POLICY_STORE_NAME
#    rm ./app2_data/$POLICY_STORE_NAME
#  popd
#
#  echo "Done"
#  exit
#}

#server_app="multidomain_server_app.exe"
#client_app="multidomain_client_app.exe"

#$EXAMPLE_DIR/$server_app                              \
#   --operation=cold-init                                   \
#   --data_dir="./${Server_app_data}/"                      \
#   --measurement_file="multidomain_server_app.measurement" \
#   --policy_store_file=policy_store                        \
#   --policy_port=8121                                      \
#   --print_all=true
#
# $EXAMPLE_DIR/$server_app                              \
#    --operation=get-certified                               \
#    --data_dir="./app2_data/"                      \
#    --measurement_file="multidomain_server_app.measurement" \
#    --policy_store_file=policy_store                        \
#    --policy_port=8121                                      \
#    --print_all=true
#
#
#  $EXAMPLE_DIR/$client_app                              \
#     --operation=cold-init                                   \
#     --data_dir="./app1_data/"                      \
#     --measurement_file="multidomain_client_app.measurement" \
#     --policy_store_file=policy_store                        \
#     --primary_policy_port=8122                              \
#     --secondary_policy_port=8121                            \
#     --print_all=true
#
#  $EXAMPLE_DIR/$client_app                              \
#    --operation=get-certified                               \
#    --data_dir="./app1_data/"                      \
#    --measurement_file="multidomain_client_app.measurement" \
#    --policy_store_file=policy_store                        \
#    --primary_policy_port=8122                              \
#    --secondary_policy_port=8121                            \
#    --print_all=true
#

#function do-run() {
  #echo "do-run"
  #echo " "
#
  #pushd $EXAMPLE_DIR/service
    #export LD_LIBRARY_PATH=/usr/local/lib
    #export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CERTIFIER_ROOT/certifier_service/teelib
    #export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CERTIFIER_ROOT/certifier_service/graminelib
    #export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CERTIFIER_ROOT/certifier_service/isletlib
    #export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CERTIFIER_ROOT/certifier_service/oelib
    #echo $LD_LIBRARY_PATH
    #sudo ldconfig
#
    #sleep 5
#
    #echo " "
    #echo "$CERTIFIER_ROOT/certifier_service/simpleserver  \
      #--policy_key_file=$POLICY_KEY_FILE_NAME --policy_cert_file=$POLICY_CERT_FILE_NAME \
      #--policyFile=policy.bin --readPolicy=true"
    #$CERTIFIER_ROOT/certifier_service/simpleserver  \
      #--policy_key_file=$POLICY_KEY_FILE_NAME --policy_cert_file=$POLICY_CERT_FILE_NAME \
      #--policyFile=policy.bin --readPolicy=true &
    #echo "simpleserver started"
    #echo " "
#
    #sleep 5
  #popd
#
  #pushd $EXAMPLE_DIR
    #echo " "
    #echo "certifying app1"
      #$EXAMPLE_DIR/example_app.exe --data_dir=./app1_data/  \
        #--domain_name=$DOMAIN_NAME \
        #--operation=fresh-start  --measurement_file="example_app.measurement" \
        #--policy_store_file=$POLICY_STORE_NAME --print_all=true
    #sleep 5
#
    #echo " "
    #echo "certifying app2"
    #$EXAMPLE_DIR/example_app.exe  --data_dir=./app2_data/ \
      #--domain_name=$DOMAIN_NAME \
      #--operation=fresh-start  --measurement_file="example_app.measurement" \
      #--policy_store_file=$POLICY_STORE_NAME --print_all=true
#
    #sleep 5
#
    #echo " "
    #echo "running app-as-server"
    #$EXAMPLE_DIR/example_app.exe \
      #--data_dir=./app2_data/ --operation="run-app-as-server" \
      #--domain_name=$DOMAIN_NAME \
      #--measurement_file="example_app.measurement" \
      #--policy_store_file=$POLICY_STORE_NAME  --print_all=true &
#
    #sleep 5
#
    #echo " "
    #echo "running app-as-client"
    #$EXAMPLE_DIR/example_app.exe \
      #--data_dir=./app1_data/ --operation="run-app-as-client"   \
      #--domain_name=$DOMAIN_NAME \
      #--measurement_file="example_app.measurement" \
      #--policy_store_file=$POLICY_STORE_NAME --print_all=true
  #popd
#
  #cleanup_stale_procs
#
  #echo "do-run done"
#}
#
#if [ "$1" == "fresh" ] ; then
  #do-fresh
  #exit
#fi
#
#if [ "$1" == "run" ] ; then
  #do-run
  #exit
#fi
#
#echo " "
#echo "Unknown option: $1"
