# Makefile - GoSign Desktop MitM PoC
DEBNAME := gosigndesktop_6.6.6_amd64.deb
PKGDIR := deb-src
OUTDIR := .
DEBPATH := $(OUTDIR)/$(DEBNAME)
VENV_DIR := .venv
PYTHON3 := python3
VENV_PYTHON := $(VENV_DIR)/bin/python
PROXY_PORT := 8666

.PHONY: all build deb venv run unproxy clean

all: build
build: deb venv

deb:
	@echo "Building .deb from $(PKGDIR) -> $(DEBPATH)"
	@if [ ! -d "$(PKGDIR)" ]; then \
	  echo "ERROR: directory '$(PKGDIR)' not found"; exit 1; \
	fi
	dpkg-deb --build "$(PKGDIR)" "$(DEBPATH)"

venv:
	@echo "Creating virtualenv in $(VENV_DIR)"
	@if [ ! -d "$(VENV_DIR)" ]; then \
		$(PYTHON3) -m venv "$(VENV_DIR)"; \
	else \
		echo "Virtualenv already exists"; \
	fi
	$(VENV_PYTHON) -m pip install -r requirements.txt

run:
	@echo "Starting mitmdump with gosigndesktop_mitm_poc.py on port ${PROXY_PORT}"
	"$(VENV_DIR)"/bin/mitmdump -s gosigndesktop_mitm_poc.py --listen-port ${PROXY_PORT}

unproxy:
	@echo "Removing proxy settings from GoSign Desktop config (~/.gosign/dike.conf)"
	@sed -i 's/^use=.*/use=NOPROXY/; s/^address=.*/address=/; s/^port=.*/port=/' ~/.gosign/dike.conf > /dev/null

clean: unproxy
	@echo "Cleaning generated .deb and virtualenv"
	@rm -f "$(DEBPATH)"
	@rm -rf "$(VENV_DIR)"
	@rm -rf __pycache__
