datasdir := $(HOME)/.make/
pio := $(HOME)/.platformio/penv/bin/pio
ttydown := /dev/ttyUSB0
webdev := http://esp-mppt_000000.lan/update

all : $(datasdir) $(datasdir)PlatformIO/esp32 .pio/libdeps
	$(pio) run
.pio/libdeps : 
	cp -rf lib/ .pio/
update : $(datasdir) $(datasdir)PlatformIO/esp32 .pio/libdeps
	$(pio) run
	curl -#lF 'firmware=@./.pio/build/esp32dev/firmware.bin'  $(webdev) | tee /dev/null
install : $(datasdir) $(datasdir)PlatformIO/esp32 .pio/libdeps
	$(pio) run --target upload --upload-port $(ttydown)
clean : $(datasdir) $(datasdir)PlatformIO/esp32
	$(pio) run --target clean
$(datasdir) :
	mkdir $(datasdir)
$(datasdir)udev : 
	sudo print
	curl -#fL https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/system/99-platformio-udev.rules -o 99-platformio-udev.rules
	sudo mv 99-platformio-udev.rules /etc/udev/rules.d/99-platformio-udev.rules
	sudo service udev restart
	echo OK >$(datasdir)udev

$(datasdir)PlatformIO.info : 
	sudo print
	curl -#fL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py -o get-platformio.py
	sudo apt install -y python3.8-venv
	python3 get-platformio.py
	mkdir $(datasdir)PlatformIO
	echo OK >$(datasdir)PlatformIO.info

$(datasdir)PlatformIO/esp32 : $(datasdir)udev $(datasdir)PlatformIO.info
	$(pio) platform install espressif32
	echo OK >$(datasdir)PlatformIO/esp32