TARGET_USER := a2nr
TARGET_HOST := tritium-h5
TARGET_PATH := /home/$(TARGET_USER)/heartbeatmice/dashboard
TARGET_PATH_STR := \/home\/$(TARGET_USER)\/heartbeatmice\/dashboard
TARGET_PASS := 'a2nr'
PROBE_USER := a2nr
PROBE_HOST := mekatronika
PROBE_PATH := /home/$(PROBE_USER)/
PROBE_PASS := 'Bismillah...'

all: comp up run
debug: comp_debug up run_debug
init: install_dep install_flutter_pi
up: copy_target
install: clean comp up install_app
install_debug: comp_debug up install_app_debug
uninstall: uninstall_app

clean:
	rm -rf ./build/* && sshpass -p $(TARGET_PASS) ssh -t $(TARGET_USER)@$(TARGET_HOST) 'echo $(TARGET_PASS) | sudo -S rm -rf $(TARGET_PATH)/*'
comp_debug:
	flutterpi_tool build --arch=arm64
comp:
	flutterpi_tool build --arch=arm64 --release
copy_target: 
	sshpass -p $(TARGET_PASS) ssh -t $(TARGET_USER)@$(TARGET_HOST) 'mkdir -p $(TARGET_PATH)' \
	&& sshpass -p $(TARGET_PASS) rsync -a --info=progress2 ./build/flutter_assets $(TARGET_USER)@$(TARGET_HOST):$(TARGET_PATH)
run:
	sshpass -p $(TARGET_PASS) ssh -t $(TARGET_USER)@$(TARGET_HOST) 'echo $(TARGET_PASS) | sudo -S flutter-pi -r 270 --release $(TARGET_PATH)/flutter_assets'
run_debug:
	sshpass -p $(TARGET_PASS) ssh -t $(TARGET_USER)@$(TARGET_HOST) 'echo $(TARGET_PASS) | sudo -S flutter-pi -o landscape_left $(TARGET_PATH)/flutter_assets'
install_app:
	sshpass -p $(TARGET_PASS) rsync -a --info=progress2 ./heartbeatmice.service $(TARGET_USER)@$(TARGET_HOST):$(TARGET_PATH) \
	&& sshpass -p $(TARGET_PASS) ssh -t $(TARGET_USER)@$(TARGET_HOST) 'sed -i -e "s/@thepath/$(TARGET_PATH_STR)/g" $(TARGET_PATH)/heartbeatmice.service && echo $(TARGET_PASS) | sudo -S cp $(TARGET_PATH)/heartbeatmice.service /lib/systemd/system/ && echo $(TARGET_PASS) | sudo -S systemctl enable heartbeatmice.service  && echo $(TARGET_PASS) | sudo -S systemctl start heartbeatmice.service'
install_app_debug:
	sshpass -p $(TARGET_PASS) rsync -a --info=progress2 ./heartbeatmiceDebug.service $(TARGET_USER)@$(TARGET_HOST):$(TARGET_PATH) \
	&& sshpass -p $(TARGET_PASS) ssh -t $(TARGET_USER)@$(TARGET_HOST) 'sed -i -e "s/@thepath/$(TARGET_PATH_STR)/g" $(TARGET_PATH)/heartbeatmiceDebug.service && echo $(TARGET_PASS) | sudo -S cp $(TARGET_PATH)/heartbeatmiceDebug.service /lib/systemd/system/ && echo $(TARGET_PASS) | sudo -S systemctl enable heartbeatmiceDebug.service  && echo $(TARGET_PASS) | sudo -S systemctl start heartbeatmiceDebug.service'
uninstall_app:
	sshpass -p $(TARGET_PASS) ssh -t $(TARGET_USER)@$(TARGET_HOST) 'echo $(TARGET_PASS) | sudo -S systemctl stop heartbeatmice.service  &&	echo $(TARGET_PASS) | sudo -S systemctl disable heartbeatmice.service  &&	echo $(TARGET_PASS) | sudo -S rm /lib/systemd/system/heartbeatmice.service'
install_dep:
	sshpass -p $(TARGET_PASS) ssh -t $(TARGET_USER)@$(TARGET_HOST) 'echo $(TARGET_PASS) | sudo -S apt-get -y install cmake libgl1-mesa-dev libgles2-mesa-dev libegl1-mesa-dev libdrm-dev libgbm-dev ttf-mscorefonts-installer fontconfig libsystemd-dev libinput-dev libudev-dev  libxkbcommon-dev \
	libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-plugins-bad gstreamer1.0-libav gstreamer1.0-alsa'
install_flutter_pi:
	sshpass -p $(TARGET_PASS) ssh -t $(TARGET_USER)@$(TARGET_HOST) 'cd ~/Documents && git clone --recursive https://github.com/ardera/flutter-pi || cd flutter-pi && mkdir build || cd build && cmake .. && make -j`nproc` && echo $(TARGET_PASS) | sudo -S make install'
probe:
	sshpass -p $(PROBE_PASS) ssh -t $(PROBE_USER)@$(PROBE_HOST) 'echo $(PROBE_PASS) | sudo -S sigrok-cli -d fx2lafw -o $(PROBE_PATH)/test.sr -w -t D3=f -M spi --config samplerate=1M --samples 2m --time 5s' \
	&& sshpass -p $(PROBE_PASS) rsync -a --info=progress2  $(PROBE_USER)@$(PROBE_HOST):$(PROBE_PATH)/test.sr ./build/ \
	&& pulseview -i ./build/test.sr -s ./pulseview_session_setup.pvs

