#!/bin/bash

if [ -z $1 ]
then
	echo "Missing commandline argument!"
	echo "Usage: $(basename $0) <p4-program-name>"
	echo "Example: $(basename $0) l2switch"
	exit -1
fi

DEFAULT_PROG=$1
T4P4SDIR="/root/t4p4s"
P4RTDIR="${T4P4SDIR}/examples/p4rt_files"
SRC=$(find -L "${T4P4SDIR}/examples" -name "${DEFAULT_PROG}.p4")

if [ ! -f ${SRC} ]
then
	echo "P4 source file does not exist."
	echo "Missing file: ${SRC}"
	exit 1
fi

echo "Generating P4Runtime files..."

if [ ! -d "$P4RTDIR" ]
then
	echo "Creating the directory for P4Runtime files: $P4RTDIR"
	mkdir $P4RTDIR
fi

pushd ${P4RTDIR}
p4c-bm2-ss --p4runtime-files "${DEFAULT_PROG}.p4runtime.txt" --toJSON "${DEFAULT_PROG}.json" "${SRC}"
popd

echo "Launching P4Runtime-shell..."
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python python3 -m p4runtime_sh --grpc-addr localhost:50051 --device-id 1 --election-id 0,1 --config "${P4RTDIR}/${DEFAULT_PROG}.p4runtime.txt,${P4RTDIR}/${DEFAULT_PROG}.json"

