# Perl puro: no hay configure ni codigo nativo que compilar.
PREFIX ?= /usr/local
DESTDIR ?=
PERL ?= perl
PROVE ?= prove
PRUEBA ?= 0
PARA ?= fuentes
CANAL ?= estable
DESCARGAS ?=
LISTA_PAQUETES ?= ../subir.lista
SUMMAPACK_BIN ?=

.PHONY: all check-deps check test install uninstall distcheck subir bajar clean help
all: check-deps

check-deps:
	$(PERL) -MPath::Class -MCapture::Tiny -MJSON::PP -e 1
	@command -v gnuplot >/dev/null || { echo 'Falta gnuplot (>= 5)' >&2; exit 1; }
	@command -v d2datdinc >/dev/null || { echo 'Falta d2datdinc (>= 1.0-25)' >&2; exit 1; }
	@command -v interpola >/dev/null || { echo 'Falta interpola de tabular (>= 2.0-16)' >&2; exit 1; }
	$(PERL) -Ilib -c scripts/simplot.pl

check test: all
	$(PROVE) -Ilib tests/*.t

# No ejecutar pruebas ni descargar dependencias al instalar como root.
install:
	$(PERL) scripts/instalar.pl "$(PREFIX)" "$(DESTDIR)"

uninstall:
	$(PERL) scripts/instalar.pl --uninstall "$(PREFIX)" "$(DESTDIR)"

distcheck:
	+$(PERL) scripts/distcheck.pl "$(MAKE)"

# No compilar, instalar ni promover a estable implicitamente.
subir:
	$(PERL) scripts/paquetes.pl subir --prueba="$(PRUEBA)" --lista="$(LISTA_PAQUETES)" --bin="$(SUMMAPACK_BIN)"

bajar:
	$(PERL) scripts/paquetes.pl bajar --prueba="$(PRUEBA)" --para="$(PARA)" --canal="$(CANAL)" --destino="$(DESCARGAS)" --bin="$(SUMMAPACK_BIN)"

# No se generan objetos: no borrar resultados ni archivos del usuario.
clean:
	@echo 'Simplot no genera objetos de compilacion.'

help:
	@echo 'make; make check; sudo make install'
	@echo 'Sin sudo: make install PREFIX="$$HOME/.local"'
	@echo 'Empaquetar: make install PREFIX=/usr DESTDIR=/ruta/de/preparacion'
	@echo 'Desinstalar: make uninstall con los mismos PREFIX y DESTDIR'
	@echo 'Distribucion: make distcheck (copia temporal, sin sudo)'
	@echo 'Servidor: make subir PRUEBA=1; make subir (publica como inestable)'
	@echo 'Descargar sin instalar: make bajar PARA=deb CANAL=inestable'
