# Makefile per Bitcoin P2PK Bruteforce

CC = g++
CFLAGS = -O3 -march=native -mtune=native -flto -pthread -Wall -Wextra
LIBS = -lsecp256k1 -lgmp

TARGET = p2pk_bruteforce
SOURCE = p2pk_bruteforce.cpp

# Percorsi di default per libsecp256k1
# Modifica se necessario in base alla tua installazione
INCLUDE_PATH = -I/usr/local/include -I/usr/include
LIB_PATH = -L/usr/local/lib -L/usr/lib

all: build-if-needed compile

build-if-needed:
	@if [ ! -d "secp256k1" ]; then \
		echo "========================================"; \
		echo "  PRIMA COMPILAZIONE: Setup Automatico"; \
		echo "========================================"; \
		echo ""; \
		echo "Compilazione libsecp256k1..."; \
		echo "Questo richiederà ~5 minuti (solo la prima volta)"; \
		echo ""; \
		$(MAKE) build-optimized-secp256k1; \
	fi

compile: build-if-needed
	@if [ -d "secp256k1" ]; then \
		echo "[+] Compilazione con libsecp256k1..."; \
		$(CC) $(CFLAGS) \
			-I./secp256k1/include \
			-L./secp256k1/lib \
			-Wl,-rpath,$(shell pwd)/secp256k1/lib \
			-o $(TARGET) $(SOURCE) $(LIBS); \
		echo "[+] Compilazione completata!"; \
		echo "[!] Performance attese: ~300K keys/sec"; \
	else \
		echo "[+] Compilazione standard..."; \
		$(CC) $(CFLAGS) $(INCLUDE_PATH) $(LIB_PATH) -o $(TARGET) $(SOURCE) $(LIBS); \
		echo "[+] Compilazione completata!"; \
		echo "[!] Performance attese: ~250K keys/sec"; \
	fi
	@echo "[!] Eseguibile: ./$(TARGET)"

standard: $(SOURCE)
	@echo "[+] Compilazione STANDARD (senza libreria ottimizzata)..."
	$(CC) $(CFLAGS) $(INCLUDE_PATH) $(LIB_PATH) -o $(TARGET) $(SOURCE) $(LIBS)
	@echo "[+] Compilazione completata!"
	@echo "[!] Eseguibile: ./$(TARGET)"
	@echo "[!] Performance attese: ~250K keys/sec"

optimized: $(SOURCE)
	@echo "[+] Compilazione con ottimizzazioni estreme (PGO)..."
	$(CC) $(CFLAGS) -fprofile-generate $(INCLUDE_PATH) $(LIB_PATH) -o $(TARGET) $(SOURCE) $(LIBS)
	@echo "[+] Esegui il programma per generare profilo..."
	@echo "[!] Poi esegui 'make pgo-use' per ricompilare"

pgo-use: $(SOURCE)
	@echo "[+] Ricompilazione con Profile-Guided Optimization..."
	$(CC) $(CFLAGS) -fprofile-use $(INCLUDE_PATH) $(LIB_PATH) -o $(TARGET) $(SOURCE) $(LIBS)
	@echo "[+] Compilazione PGO completata!"

static: $(SOURCE)
	@echo "[+] Compilazione statica..."
	$(CC) $(CFLAGS) -static $(INCLUDE_PATH) $(LIB_PATH) -o $(TARGET) $(SOURCE) -l:libsecp256k1.a -l:libgmp.a
	@echo "[+] Compilazione statica completata!"

debug: $(SOURCE)
	@echo "[+] Compilazione in modalità debug..."
	$(CC) -g -pthread -Wall -Wextra $(INCLUDE_PATH) $(LIB_PATH) -o $(TARGET)_debug $(SOURCE) $(LIBS)
	@echo "[+] Compilazione debug completata!"

test: $(TARGET)
	@echo "[+] Test rapido del programma..."
	./$(TARGET) --help || echo "Test completato"

clean:
	@echo "[+] Pulizia file compilati..."
	rm -f $(TARGET) $(TARGET)_debug
	rm -f *.o *.gcda *.gcno
	@echo "[+] Pulizia completata!"

clean-all: clean
	@echo "[+] Pulizia completa (include libreria secp256k1)..."
	rm -rf secp256k1_build secp256k1
	@echo "[+] Pulizia completa terminata!"

install-deps:
	@echo "[+] Installazione dipendenze..."
	@echo "[!] Questo installerà: build-essential, libsecp256k1-dev, libgmp-dev"
	@echo "[!] Premi CTRL+C per annullare, ENTER per continuare..."
	@read dummy
	sudo apt-get update
	sudo apt-get install -y build-essential libsecp256k1-dev libgmp-dev git autoconf libtool pkg-config
	@echo "[+] Dipendenze installate!"

install-secp256k1:
	@echo "[+] Compilazione e installazione libsecp256k1 da sorgente..."
	git clone https://github.com/bitcoin-core/secp256k1.git /tmp/secp256k1
	cd /tmp/secp256k1 && ./autogen.sh && ./configure && make && sudo make install
	sudo ldconfig
	@echo "[+] libsecp256k1 installata!"

build-optimized-secp256k1:
	@echo "[+] Compilazione libsecp256k1..."
	@./build_secp256k1.sh

with-optimized-lib: $(SOURCE)
	@echo "[+] Compilazione con libsecp256k1..."
	@if [ ! -d "secp256k1" ]; then \
		echo "[ERROR] Directory secp256k1 non trovata!"; \
		echo "[!] Esegui prima: make build-optimized-secp256k1"; \
		exit 1; \
	fi
	$(CC) $(CFLAGS) \
		-I./secp256k1/include \
		-L./secp256k1/lib \
		-Wl,-rpath,$(shell pwd)/secp256k1/lib \
		-o $(TARGET) $(SOURCE) $(LIBS)
	@echo "[+] Compilazione completata!"
	@echo "[!] Eseguibile: ./$(TARGET)"

help:
	@echo "==================================================="
	@echo "  Bitcoin P2PK Bruteforce - Makefile"
	@echo "==================================================="
	@echo ""
	@echo "Target disponibili:"
	@echo "  make                        - Compila il programma"
	@echo "  make build-optimized-secp256k1 - Compila libsecp256k1"
	@echo "  make with-optimized-lib     - Compila con libsecp256k1"
	@echo "  make optimized              - Compila con PGO step 1"
	@echo "  make pgo-use                - Compila con PGO step 2"
	@echo "  make static                 - Compila versione statica"
	@echo "  make debug                  - Compila versione debug"
	@echo "  make test                   - Test rapido"
	@echo "  make clean                  - Rimuove file compilati"
	@echo "  make clean-all              - Pulizia completa (include secp256k1)"
	@echo "  make install-deps           - Installa dipendenze"
	@echo "  make install-secp256k1      - Compila secp256k1 da sorgente"
	@echo ""
	@echo "Uso:"
	@echo "  ./$(TARGET) [file_chiavi.txt]"
	@echo ""
	@echo "==================================================="

.PHONY: all optimized pgo-use static debug test clean clean-all install-deps install-secp256k1 build-optimized-secp256k1 with-optimized-lib help
