#!/bin/bash

# This script generates .proto files for ftrace events from the /format files
# in src/traced/probes/ftrace/test/data/*/events/.
# Only the events in the whitelist are translated.

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ "$BUILDDIR" == "" ]; then
  BUILDDIR="$DIR/../out/ftrace_proto_gen_build"
  "$DIR/gn" gen "$BUILDDIR" --args="is_debug=false"
else
  BUILDDIR="$(realpath $BUILDDIR)"
  if [ ! -d "$BUILDDIR" ]; then
    echo "Directory does not exist: $BUILDDIR"
    exit 1
  fi
fi

DESCRIPTOR='gen/protos/perfetto/trace/ftrace.descriptor'
"$DIR/ninja" -C "$BUILDDIR" ftrace_proto_gen $DESCRIPTOR

# FIXME(fmayer): make ftrace_proto_gen independent of cwd.
cd "$DIR/.."

"$BUILDDIR/ftrace_proto_gen" \
  --whitelist_path "$DIR/ftrace_proto_gen/event_whitelist" \
  --output_dir "$DIR/../protos/perfetto/trace/ftrace/" \
  --proto_descriptor "$BUILDDIR/$DESCRIPTOR" \
  --update_build_files \
  "$@" \
  "$DIR"/../src/traced/probes/ftrace/test/data/*/events/