target_dir := "target"
project_name := "fundamentum-edge-daemon"

# Build the default variant of this package, its doc and perform all available
# checks
default: build doc check

# Build all variants of this package, its doc and perform all available checks
all: build-all doc check

# Clean all products of this justfile
clean: clean-build clean-init

#----------
# Building
#----------

# Build all variants of this package.
build-all: build-debug

# Build the default variant of this package.
build: build-debug

# Clean any products of the 'build' task
clean-build:
    cargo clean

# Build the debug version of this package.
build-debug:
    cargo build

#----------
# Checks
#----------

# Run all available checks
check: check-static check-test

# Run cargo test with all features enabled
check-test:
    cargo test --all-features

# Run cargo test
check-test-quick:
    cargo test

# Run all static checks (i.e.: all checks but without any tests).
check-static: check-formatting check-clippy check-doc-readme

# Check the formatting
check-formatting:
    cargo fmt --all -- --check

# Check clippy
check-clippy:
    cargo clippy --all

#------
# Docs
#------

# Build this package's documentation
doc: doc-package doc-readme

# Clean any doc related build products
clean-doc: clean-doc-package

# Generates this package's full doc (including that of all its
# dependencies)
doc-package:
    cargo doc --all-features

# Generates this package's full doc and launch a browser to it
doc-package-preview:
    cargo doc --all-features --open

# Generates only this package's doc (without any dependencies)
doc-package-only:
    cargo doc --all-features --no-deps

# Clean this package's doc
clean-doc-package:
    cargo clean --doc

#----------------------
# README.md generation
#----------------------

# Generate README.md for a single crate
doc-readme: _build-readme
    #!/usr/bin/env bash
    set -euo pipefail
    cp "{{target_dir}}/README.md" "README.md"

# Check README.md for a single crate
check-doc-readme: _build-readme
    #!/usr/bin/env bash
    set -euo pipefail
    diff -q "{{target_dir}}/README.md" "README.md" || ( \
        echo -e "\033[1;31mError:\033[0m README.md for {{project_name}} needs to be regenerated."; \
        echo -e "       Run 'just doc-readme' to regenerate.\n"; \
        exit 1 \
    )

# Builds README.md for a single crate
_build-readme:
    #!/usr/bin/env bash
    set -e -o pipefail
    mkdir -p {{target_dir}}
    echo "Building README.md for {{project_name}}"
    cargo readme > {{target_dir}}/README.md

#--------------
# Initial setup
#--------------

# Perform the required initial setup
init: init-secrets

# Clean any products of the 'init' task.
clean-init: clean-init-secrets

# Perform the required initial secrets setup
init-secrets: init-secrets-keypair

# Clean any products of the 'init-secrets' task
clean-init-secrets: clean-init-secrets-keypair

# Generate the required RSA keypair
init-secrets-keypair:
    openssl genpkey -algorithm RSA -out rsa_private.pem -pkeyopt rsa_keygen_bits:2048
    openssl rsa -in rsa_private.pem -pubout -out rsa_public.pem

# Clean the RSA keypair
clean-init-secrets-keypair:
    rm -f "./rsa_private.pem"
    rm -f "./rsa_public.pem"

#----------
# Publish
#----------

# Publish this package to crates.io
publish:
    echo ${CARGO_REGISTRY_TOKEN} | cargo login
    cargo publish

#---------------
# Misc actions
#---------------

# Format the rust code base
format:
    cargo fmt --all

# Run the edge daemon exposing its gRPC interface at '127.0.0.1:8080'.
run:
    cargo run -- --grpc-tcp 127.0.0.1:8080

# Run the edge daemon exposing its gRPC interface at './grpc_socket'.
run-uds:
    cargo run -- --grpc-uds ./grpc_socket