# Local build helpers for the Proteus Mythic Payload Type container.
#
# In a production install, `mythic-cli install folder /path/to/Proteus`
# materialises the agent_code/ loader_code/ layout_manifest_code/ symlinks
# into real directories before running `docker build`, so the symlinks
# pointing at `../../../crates/*` get resolved transparently.
#
# When building locally (no mythic-cli) we have to do the same dereferencing
# ourselves — `docker build` won't follow symlinks that escape the build
# context. The `tar -czh` pipe below does exactly that: `-h` tells tar to
# dereference symlinks while producing the context stream that docker reads
# from stdin.

IMAGE ?= proteus-mythic:test

.PHONY: build run shell clean

build:
	tar -czh \
	    --exclude='proteus/agent_code/target' \
	    --exclude='proteus/loader_code/target' \
	    --exclude='proteus/layout_manifest_code/target' \
	    --exclude='proteus/agent_code/Rustic64.bin*' \
	    --exclude='proteus/agent_code/Linker.shuffled.ld' \
	    --exclude='proteus/loader_code/Linker.shuffled.ld' \
	    --exclude='**/.git' --exclude='**/__pycache__' \
	    -f - . | docker build -t $(IMAGE) -

shell: build
	docker run --rm -it $(IMAGE) bash

# One-shot smoke test: stage the three crates inside the container and
# run `cargo make shuffle` to prove the toolchain + sources are wired.
smoke: build
	docker run --rm $(IMAGE) bash -c '\
	    set -eu; \
	    mkdir -p /tmp/staged/crates; \
	    cp -r /Mythic/proteus/agent_code           /tmp/staged/crates/rustic64; \
	    cp -r /Mythic/proteus/loader_code          /tmp/staged/crates/loaders; \
	    cp -r /Mythic/proteus/layout_manifest_code /tmp/staged/crates/layout-manifest; \
	    cd /tmp/staged/crates/rustic64; \
	    PROTEUS_CONFIG_JSON='"'"'{"uuid":"deadbeef-0000-0000-0000-0000000000ff","callback_host":"http://10.0.0.1:8443","get_uri":"/x","post_uri":"/x","user_agent":"M","sleep_seconds":42,"jitter_percent":10,"kill_date_unix":0,"aes_key_hex":"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff","hmac_key_hex":"ffeeddccbbaa99887766554433221100ffeeddccbbaa99887766554433221100"}'"'"' \
	      cargo make shuffle; \
	    ls -la Rustic64.bin; \
	'

clean:
	docker rmi -f $(IMAGE) 2>/dev/null || true
