#!/bin/bash

# Project Calico BPF dataplane build scripts.
# Copyright (c) 2020-2024 Tigera, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

filename=$1 # Example: from_wep_host_drop_fib_debug.o
args=()

if [[ "${filename}" =~ .*co-re.* ]]; then
  args+=("-DBPF_CORE_SUPPORTED")
fi
if [[ "${filename}" =~ .*debug.* ]]; then
  args+=("-DCALI_LOG_LEVEL=CALI_LOG_LEVEL_DEBUG")
elif [[ "${filename}" =~ .*no_log.* ]]; then
  args+=("-DCALI_LOG_LEVEL=CALI_LOG_LEVEL_OFF")
else
  echo "No log level in filename"
  exit 1
fi

if [[ "${filename}" =~ .*host_drop.* ]]; then
  args+=("-DCALI_DROP_WORKLOAD_TO_HOST=true")
else
  args+=("-DCALI_DROP_WORKLOAD_TO_HOST=false")
fi

if [[ "${filename}" =~ .*fib.* ]]; then
  args+=("-DCALI_FIB_LOOKUP_ENABLED=true")
else
  args+=("-DCALI_FIB_LOOKUP_ENABLED=false")
fi

if [[ "${filename}" =~ test_.* ]]; then
  args+=("-DUNITTEST")
  args+=("-DBPF_CORE_SUPPORTED")
fi

if [[ "${filename}" =~ .*_v6.ll ]]; then
  args+=("-DIPVER6")
fi

flags=0

# WARNING: these constants must be kept in sync with bpf.h.
((CALI_TC_HOST_EP = 1 << 0))
((CALI_TC_INGRESS = 1 << 1))
((CALI_TC_IPIP = 1 << 2))
((CALI_CGROUP = 1 << 3))
((CALI_TC_DSR = 1 << 4))
((CALI_TC_L3_DEV = 1 << 5))
((CALI_XDP_PROG = 1 << 6))
((CALI_TC_NAT_IF = 1 << 7))
((CALI_TC_LO = 1 << 8))
((CALI_CT_CLEANUP = 1 << 9))
((CALI_TC_VXLAN = 1 << 10))

if [[ "${filename}" =~ .*hep.* ]]; then
  # Host endpoint.
  ((flags |= CALI_TC_HOST_EP))
  ep_type="host"
elif [[ "${filename}" =~ .*ipip.* ]]; then
  # IPIP.
  ((flags |= CALI_TC_IPIP | CALI_TC_HOST_EP))
  ep_type="ipip"
elif [[ "${filename}" =~ .*l3.* ]]; then
  # Any l3 device.
  ((flags |= CALI_TC_L3_DEV | CALI_TC_HOST_EP))
  ep_type="l3dev"
elif [[ "${filename}" =~ .*vxlan.* ]]; then
  # Any vxlan device.
  ((flags |= CALI_TC_VXLAN | CALI_TC_HOST_EP))
  ep_type="vxlan"
elif [[ "${filename}" =~ .*connect.* ]]; then
  # Connect-time load balancer (CGROUP attached).
  ((flags |= CALI_CGROUP))
elif [[ "${filename}" =~ .*conntrack_cleanup.* ]]; then
  ((flags |= CALI_CT_CLEANUP))
elif [[ "${filename}" =~ .*wep.* ]]; then
  # Workload endpoint; recognised by CALI_TC_HOST_EP bit being 0.
  ep_type="workload"
elif [[ "${filename}" =~ .*xdp.* ]]; then
  # XDP, so host endpoint.
  ((flags |= CALI_TC_HOST_EP))
  ((flags |= CALI_XDP_PROG))
  ep_type="host"
elif [[ "${filename}" =~ .*nat.* ]]; then
  ((flags |= CALI_TC_HOST_EP))
  ((flags |= CALI_TC_NAT_IF))
  ep_type="nat"
elif [[ "${filename}" =~ .*lo.* ]]; then
  # loopback, so host endpoint.
  ((flags |= CALI_TC_HOST_EP))
  ((flags |= CALI_TC_LO))
  ep_type="lo"
fi

if [[ "${filename}" =~ to.* ]]; then
  if ! ((flags & CALI_TC_HOST_EP)); then
    # Workload endpoint.  Host's "to endpoint" is the endpoints ingress hook.
    ((flags |= CALI_TC_INGRESS))
  fi
  from_or_to="to"
elif [[ "${filename}" =~ (from|xdp).* ]]; then
  if ((flags & CALI_TC_HOST_EP)); then
    # Host endpoint.
    ((flags |= CALI_TC_INGRESS))
  fi
  from_or_to="from"
fi

if [[ "${filename}" =~ _dsr.* ]]; then
  ((flags |= CALI_TC_DSR))
fi

args+=("-DCALI_COMPILE_FLAGS=${flags}")

echo "Flags: ${args[*]}" 1>&2
echo "${args[*]}"
