#!/bin/bash

# Get the IP addresses
Daddr=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' oram_D)
Eaddr=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' oram_E)

# The ORAM size
size=26
iters=128
if [ "$1" != "" ]; then
    size="$1"
fi
if [ "$2" != "" ]; then
    iters="$2"
fi

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

# Run, saving the output
savefileC=$$.C.out
savefileD=$$.D.out
savefileE=$$.E.out
docker exec -w /root/oram oram_C bash -c "perl -lp -i -e 's/addrBits: (\d+)/addrBits: '$size'/; s/iters: (\d+)/iters: '$iters'/' config/config.yaml"
docker exec -w /root/oram oram_D bash -c "perl -lp -i -e 's/addrBits: (\d+)/addrBits: '$size'/; s/iters: (\d+)/iters: '$iters'/' config/config.yaml"
docker exec -w /root/oram oram_E bash -c "perl -lp -i -e 's/addrBits: (\d+)/addrBits: '$size'/; s/iters: (\d+)/iters: '$iters'/' config/config.yaml"
docker exec -w /root/oram oram_E bash -c "$ORAM_NUMA_E stdbuf -o 0 java -cp bin:lib/* ui/CLI -protocol pirrtv -debbie_ip $Daddr -eddie_ip $Eaddr eddie > $savefileE" &
sleep 2
docker exec -w /root/oram oram_D bash -c "$ORAM_NUMA_D stdbuf -o 0 java -cp bin:lib/* ui/CLI -protocol pirrtv -debbie_ip $Daddr -eddie_ip $Eaddr debbie > $savefileD" &
sleep 2
docker exec -w /root/oram oram_C bash -c "$ORAM_NUMA_C stdbuf -o 0 java -cp bin:lib/* ui/CLI -protocol pirrtv -debbie_ip $Daddr -eddie_ip $Eaddr charlie > $savefileC" &
wait
echo ===== C output
docker exec -w /root/oram oram_C cat $savefileC
docker exec -w /root/oram oram_C rm -f $savefileC
echo ===== D output
docker exec -w /root/oram oram_D cat $savefileD
docker exec -w /root/oram oram_D rm -f $savefileD
echo ===== E output
docker exec -w /root/oram oram_E cat $savefileE
docker exec -w /root/oram oram_E rm -f $savefileE
date "+===== End %s %F %T"
