#!/bin/bash

# Get the IP addresses
p0addr=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' duoram_p0)
p1addr=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' duoram_p1)
p2addr=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' duoram_p2)

# The Duoram mode and size
# mode is read, write, readwrite, or init
duoramtype=2P
phase=online
mode=read
size=20
iters=128
modeval=0
if [ "$1" != "" ]; then
    mode="$1"
fi
if [ "$2" != "" ]; then
    size="$2"
fi
if [ "$3" != "" ]; then
    iters="$3"
fi
if [ "$4" != "" ]; then
    phase="$4"
fi

if [ "$5" != "" ]; then
    duoramtype="$5"
fi

if [ "$mode" = "read" ]; then
    modeval="0"
fi

if [ "$mode" = "write" ]; then
    modeval="0"
fi

if [ "$mode" = "readwrite" ]; then
    modeval="1"
fi

items=$((1<<size))

echo ===== Running duoram $mode $size $iters
date "+===== Start %s %F %T"

    if [ "$4" = "online" -a "$5" = "3P" ]; then
        # Run, saving the output
        savefilep2=$$.p2.out
        savefilep0=$$.p1.out
        savefilep1=$$.p0.out
        docker exec -w /root/duoram/duoram-online duoram_p2 bash -c "$DUORAM_NUMA_P2 stdbuf -o 0  ./p2 $p0addr $p1addr $size $iters $iters $iters 1 > $savefilep2 2>&1" &
        sleep 4
        docker exec -w /root/duoram/duoram-online duoram_p1 bash -c "$DUORAM_NUMA_P1 stdbuf -o 0  ./duoram1  $p0addr $p2addr $size $iters $iters $iters 1 > $savefilep1 2>&1" &
        sleep 2
        docker exec -w /root/duoram/duoram-online duoram_p0 bash -c "$DUORAM_NUMA_P0 stdbuf -o 0  ./duoram0  $p1addr $p2addr $size $iters $iters $iters 1 > $savefilep0 2>&1" &
        wait
        echo ===== P0 output
        docker exec -w /root/duoram/duoram-online duoram_p0 cat $savefilep0
        docker exec -w /root/duoram/duoram-online duoram_p0 rm -f $savefilep0
        echo ===== P1 output
        docker exec -w /root/duoram/duoram-online duoram_p1 cat $savefilep1
        docker exec -w /root/duoram/duoram-online duoram_p1 rm -f $savefilep1
        date "+===== End %s %F %T"
    fi



    if [ "$4" = "preproc" -a "$5" = "3P" ]; then
        # Run, saving the output
        savefilep2=$$.p2.out
        savefilep0=$$.p1.out
        savefilep1=$$.p0.out
        docker exec -w /root/duoram/preprocessing duoram_p2 bash -c "$DUORAM_NUMA_P2 stdbuf -o 0  ./p2preprocessing $p0addr $p1addr $iters $size $modeval > $savefilep2 2>&1" &
        sleep 4
        docker exec -w /root/duoram/preprocessing duoram_p1 bash -c "$DUORAM_NUMA_P1 stdbuf -o 0  ./preprocessing1  $p0addr $p2addr $iters $size $modeval > $savefilep1 2>&1" &
        sleep 2
        docker exec -w /root/duoram/preprocessing duoram_p0 bash -c "$DUORAM_NUMA_P0 stdbuf -o 0  ./preprocessing0  $p1addr $p2addr $iters $size $modeval > $savefilep0 2>&1" &
        wait
        echo ===== P0 output
        docker exec -w /root/duoram/preprocessing duoram_p0 cat $savefilep0
        docker exec -w /root/duoram/preprocessing duoram_p0 rm -f $savefilep0
        echo ===== P1 output
        docker exec -w /root/duoram/preprocessing duoram_p1 cat $savefilep1
        docker exec -w /root/duoram/preprocessing duoram_p1 rm -f $savefilep1
        date "+===== End %s %F %T"
    fi

     if [ "$1" = "read" -a "$5" = "2P" ]; then
        # Run, saving the output
        savefilep0=$$.p1.out
        savefilep1=$$.p0.out
        docker exec -w /root/duoram/cpir-read/cxx duoram_p1 bash -c "$DUORAM_NUMA_P1 stdbuf -o 0  ./spir_test1  $p0addr $size $iters $iters $iters > $savefilep1 2>&1" &
        echo "P0 launched"
        sleep 4
        docker exec -w /root/duoram/cpir-read/cxx duoram_p0 bash -c "$DUORAM_NUMA_P0 stdbuf -o 0  ./spir_test0  $p1addr $size $iters $iters $iters > $savefilep0 2>&1" &
        echo "P1 launched"
        wait
        echo ===== P0 output
        docker exec -w /root/duoram/cpir-read/cxx duoram_p0 cat $savefilep0
        docker exec -w /root/duoram/cpir-read/cxx duoram_p0 rm -f $savefilep0
        echo ===== P1 output
        docker exec -w /root/duoram/cpir-read/cxx duoram_p1 cat $savefilep1
        docker exec -w /root/duoram/cpir-read/cxx duoram_p1 rm -f $savefilep1
        date "+===== End %s %F %T"
    fi

    if [ "$1" = "write" -a "$5" = "2P" ]; then
        # Run, saving the output
        savefilep0=$$.p1.out
        savefilep1=$$.p0.out
        docker exec -w /root/duoram/2p-preprocessing duoram_p1 ./OT $p0addr $p1addr 1 128 &
        echo "P1 launched"
        sleep 1
        docker exec -w /root/duoram/2p-preprocessing duoram_p0 ./OT $p0addr $p1addr 0 128 &
        sleep 2
        docker exec -w /root/duoram/2p-preprocessing duoram_p1 bash -c "$DUORAM_NUMA_P1 stdbuf -o 0  ./preprocessing1  $p0addr $size $iters > $savefilep1 2>&1" &
        sleep 4
        docker exec -w /root/duoram/2p-preprocessing duoram_p0 bash -c "$DUORAM_NUMA_P0 stdbuf -o 0  ./preprocessing0 $p1addr $size $iters > $savefilep0 2>&1" &
        wait
        echo ===== P0 output
        docker exec -w /root/duoram/2p-preprocessing duoram_p0 cat $savefilep0
        docker exec -w /root/duoram/2p-preprocessing duoram_p0 rm -f $savefilep0
        echo ===== P1 output
        docker exec -w /root/duoram/2p-preprocessing duoram_p1 cat $savefilep1
        docker exec -w /root/duoram/2p-preprocessing duoram_p1 rm -f $savefilep1
        date "+===== End %s %F %T"
    fi