#!/bin/bash

# Run an experiment using ssh to real hosts, not in dockers

# We need these environment variables set:

# ORAM_SSH_C_USERHOST: the user@host to ssh to for C
# ORAM_SSH_C_SSHOPTS: any options to ssh you need to ssh to C
# ORAM_SSH_C_DIR: the directory relative to the homedir where the prac
#     repo is checked out and built

# ORAM_SSH_D_USERHOST: the user@host to ssh to for D
# ORAM_SSH_D_SSHOPTS: any options to ssh you need to ssh to D
# ORAM_SSH_D_IP: the IP address D can listen for connections on
# ORAM_SSH_D_DIR: the directory relative to the homedir where the prac
#     repo is checked out and built

# ORAM_SSH_E_USERHOST: the user@host to ssh to for E
# ORAM_SSH_E_SSHOPTS: any options to ssh you need to ssh to E
# ORAM_SSH_E_IP: the IP address E can listen for connections on
# ORAM_SSH_E_DIR: the directory relative to the homedir where the prac
#     repo is checked out and built

# Get the IP addresses
Daddr=$ORAM_SSH_D_IP
Eaddr=$ORAM_SSH_E_IP

# 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
ssh -n $ORAM_SSH_C_SSHOPTS $ORAM_SSH_C_USERHOST "cd $ORAM_SSH_C_DIR && perl -lp -i -e 's/addrBits: (\d+)/addrBits: '$size'/; s/iters: (\d+)/iters: '$iters'/' config/config.yaml"
ssh -n $ORAM_SSH_D_SSHOPTS $ORAM_SSH_D_USERHOST "cd $ORAM_SSH_D_DIR && perl -lp -i -e 's/addrBits: (\d+)/addrBits: '$size'/; s/iters: (\d+)/iters: '$iters'/' config/config.yaml"
ssh -n $ORAM_SSH_E_SSHOPTS $ORAM_SSH_E_USERHOST "cd $ORAM_SSH_E_DIR && perl -lp -i -e 's/addrBits: (\d+)/addrBits: '$size'/; s/iters: (\d+)/iters: '$iters'/' config/config.yaml"
ssh -n $ORAM_SSH_E_SSHOPTS $ORAM_SSH_E_USERHOST "cd $ORAM_SSH_E_DIR && $ORAM_NUMA_E stdbuf -o 0 java -cp bin:lib/* ui/CLI -protocol pirrtv -debbie_ip $Daddr -eddie_ip $Eaddr eddie > $savefileE 2>&1" &
sleep 5
ssh -n $ORAM_SSH_D_SSHOPTS $ORAM_SSH_D_USERHOST "cd $ORAM_SSH_D_DIR && $ORAM_NUMA_D stdbuf -o 0 java -cp bin:lib/* ui/CLI -protocol pirrtv -debbie_ip $Daddr -eddie_ip $Eaddr debbie > $savefileD 2>&1" &
sleep 5
ssh -n $ORAM_SSH_C_SSHOPTS $ORAM_SSH_C_USERHOST "cd $ORAM_SSH_C_DIR && $ORAM_NUMA_C stdbuf -o 0 java -cp bin:lib/* ui/CLI -protocol pirrtv -debbie_ip $Daddr -eddie_ip $Eaddr charlie > $savefileC 2>&1" &
wait
echo ===== C output
ssh -n $ORAM_SSH_C_SSHOPTS $ORAM_SSH_C_USERHOST "cd $ORAM_SSH_C_DIR && cat $savefileC && rm -f $savefileC"
echo ===== D output
ssh -n $ORAM_SSH_D_SSHOPTS $ORAM_SSH_D_USERHOST "cd $ORAM_SSH_D_DIR && cat $savefileD && rm -f $savefileD"
echo ===== E output
ssh -n $ORAM_SSH_E_SSHOPTS $ORAM_SSH_E_USERHOST "cd $ORAM_SSH_E_DIR && cat $savefileE && rm -f $savefileE"
date "+===== End %s %F %T"