#!/usr/bin/env bash

set -e

# function to print help message
print_help() {
    echo "Options:"
    echo "  --no-host-tool          Disable installation of host tools"
    echo "  --no-bootstrap          Disable sourcing connectedhomeip's scripts/bootstrap.sh,"
    echo "                          This can be helpful if there's already present connectedhomeip setup"
    echo "  --build-python          Build Python environment for running Python test scripts"
    echo "  --ninja-jobs            Number of jobs to use for ninja (default: $(get_nproc))"
    echo "                          This is used to build the host tools."
    echo "                          This can be helpful in case of slow build machines/docker containers,"
    echo "                          or to speed up the build process on faster machines."
    echo "  --help                  Display this help message"
}

echo_log() {
  echo $@
  echo ""
}

# Get the number of available processing units to optimize build performance
# It tries nproc -> getconf -> sysctl and defaults to 1 if all fail assuming
# a single processing unit is available.
get_nproc() {
  nproc 2>/dev/null \
  || getconf _NPROCESSORS_ONLN 2>/dev/null \
  || sysctl -n hw.ncpu 2>/dev/null \
  || echo 1
}

# Parse command-line arguments
NO_HOST_TOOL=false
NO_BOOTSTRAP=false
BUILD_PYTHON=false
NINJA_JOBS=$(get_nproc)

while [[ "$#" -gt 0 ]]; do
  case $1 in
         --no-host-tool)
            NO_HOST_TOOL=true
            ;;
        --no-bootstrap)
            NO_BOOTSTRAP=true
            ;;
        --build-python)
            BUILD_PYTHON=true
            ;;
        --ninja-jobs)
            NINJA_JOBS=${2:-$(get_nproc)}
            shift
            ;;
        --help)
            print_help
            exit 1
            ;;
         *)
            print_help
            exit 1
            ;;
  esac
  shift
done

basedir=$(dirname "$0")
ESP_MATTER_PATH=$(cd "${basedir}"; pwd)
MATTER_PATH=${ESP_MATTER_PATH}/connectedhomeip/connectedhomeip
export ZAP_INSTALL_PATH=${MATTER_PATH}/.environment/cipd/packages/zap


if [ $NO_BOOTSTRAP = false ]; then
  echo_log "Running Matter Setup"
  cd ${MATTER_PATH}
  source ${MATTER_PATH}/scripts/bootstrap.sh -p all,esp32
  cd ${ESP_MATTER_PATH}
else
  echo_log "Skipping Matter Setup"
fi

if [ $NO_HOST_TOOL = false ]; then
  echo_log "Building host tools"
  gn --root="${MATTER_PATH}" gen ${MATTER_PATH}/out/host --args='chip_inet_config_enable_ipv4=false'
  ninja -j $NINJA_JOBS -C ${MATTER_PATH}/out/host chip-cert chip-tool
  echo_log "Host tools built at: ${MATTER_PATH}/out/host"
else
  echo_log "Skip building host tools"
fi

if [ $NO_BOOTSTRAP = false ]; then
  echo_log "Exit Matter environment"
  deactivate
fi

echo_log "Installing python dependencies for Matter"

echo_log "Installing requirements from requirements.txt"
python3 -m pip install -r ${ESP_MATTER_PATH}/requirements.txt > /dev/null

if [ $BUILD_PYTHON = true ]; then
  echo_log "Building Python testing environment"
  cd ${MATTER_PATH}
  ./scripts/build_python.sh --enable_ble true --chip_detail_logging true -i out/py_env
  if [ $? -ne 0 ]; then
    echo_log "Failed to build Python testing environment"
  fi
  cd ${ESP_MATTER_PATH}
fi

echo_log "All done! You can now run:"
echo_log "  . ${basedir}/export.sh"
