################################################################################
#                                                                              #
#  Convenience Makefile for old fogies who can't imagine its lack.             #
#                                                                              #
################################################################################

.PHONY: docs doc

VERSION := $(shell grep 'VERSION =' enlighten/common.py | awk '{print $$3}' | tr -d '"')
RPI_ID := $(shell test -f /etc/os-release && grep '^ID=' /etc/os-release | awk -F= '{print $$2}' | tr -d '"')
RPI_CODENAME := $(shell test -f /etc/os-release && grep '^VERSION_CODENAME=' /etc/os-release | awk -F= '{print $$2}' | tr -d '"')
PLATYPUS_BIN := /Applications/Platypus.app/Contents/Resources/platypus_clt

help:
	@echo "To build and run Enlighten, see README.md"
	@echo
	@echo "Supported targets:"
	@echo
	@echo "  run             - runs Enlighten from source"
	@echo
	@echo "  clean           - purge build artifacts (must re-run 'make deps')"
	@echo "  designer        - runs Qt Designer with main GUI"
	@echo "  deps            - rebuilds Qt artifacts after editing with Designer"
	@echo
	@echo "  cloc            - count SLOC in Python, JSON and .ini files"
	@echo "  doc             - render documentation"
	@echo "  linux-installer - build a Linux binary using pyinstaller"
	@echo "  rpi-installer   - build a Raspberry Pi binary using pyinstaller"
	@echo "  mac-installer   - build a Mac application using pyinstaller and platypus"
	@echo 
	@echo "VERSION = $(VERSION)"

cloc:
	@cloc --include-lang=Python,JSON,INI --exclude-dir=uic_qrc .

# You may need to do this if you're sharing a sandbox between Linux, Windows, Mac
# etc to force .pyc rebuilds
clean:
	@rm -rfv scripts/built-dist/Enlighten* \
             scripts/windows_installer/ \
             scripts/work-path/Enlighten \
             enlighten/assets/uic_qrc/*.py \
             build \
             build-{linux,mac}* \
             ENLIGHTEN-*.app \
             ENLIGHTEN-linux-*.tgz \
             ENLIGHTEN-MacOS-*.zip \
             ENLIGHTEN-*.exe \
             mac.bundled \
             docs/doxygen \
             enlighten.{err,out} \
             build.{err,out} \
             doxygen.{err,out}
	@find . -name __pycache__ -exec rm -rfv {} \;
	@find . -name \*.pyc -exec rm -fv {} \;

deps:
	@dos2unix scripts/rebuild_resources.sh
	@scripts/rebuild_resources.sh

designer:
	@./designer.sh

doc: docs

docs:
	@# Render Doxygen
	@#
	@rm -rf docs/doxygen/html
	@test -L wasatch || ln -s ../Wasatch.PY/wasatch
	doxygen 1>doxygen.out 2>doxygen.err
	@test -L wasatch && rm wasatch
	@#
	@# Convert Changelog to HTML
	@#
	@echo "<!-- This file is generated by enlighten/Makefile 'make docs' and uploaded by enlighten/scripts/deploy.  Do not hand-edit. -->" > docs/ENLIGHTEN_CHANGELOG.html
	pandoc --metadata title:ENLIGHTEN-Changelog --toc --embed-resources --standalone --from gfm --to html CHANGELOG.md >> docs/ENLIGHTEN_CHANGELOG.html
	@#
	@# done
	@#
	@echo
	@echo "View via:" 
	@echo
	@echo "    docs/doxygen/html/index.html"

# We don't seem to have to do this "EnlightenGUI" stuff on Windows, because the 
# executable is named enlighten.exe and doesn't conflict with the enlighten/ directory;
# we don't have to do it on Mac, because pyinstaller doesn't support bundles and
# we have to roll all that in via platypus externally.
linux-installer-base:
	rm -rf build-linux*
	mkdir -p build-linux
	pyinstaller \
        --distpath="build-linux" \
        --workpath="build-linux-work" \
        --noconfirm \
        --clean \
        --windowed \
        --paths="../Wasatch.PY" \
        --paths="plugins" \
        --hidden-import="scipy._lib.messagestream" \
        --hidden-import="scipy.special.cython_special" \
        --icon "../enlighten/assets/uic_qrc/images/EnlightenIcon.ico" \
        --specpath="scripts" \
        --name EnlightenGUI \
        scripts/Enlighten.py 2>&1 \
        | sed 's/^/pyinstaller: /'
	mkdir -p                             build-linux/EnlightenGUI/enlighten/assets
	cp -rv enlighten/assets/stylesheets  build-linux/EnlightenGUI/enlighten/assets
	cp -rv enlighten/assets/example_data build-linux/EnlightenGUI/enlighten/assets
	cp -rv ../Wasatch.PY/udev            build-linux/EnlightenGUI/
	rm -fv build-linux/EnlightenGUI/_internal/libusb-1.0.so.0
	mv -v  build-linux/EnlightenGUI build-linux/ENLIGHTEN-$(VERSION)
	( cd build-linux && tar zcvf ../ENLIGHTEN-linux-$(VERSION).tgz ENLIGHTEN-$(VERSION) | sed 's/^/compressing: /' )

linux-installer: linux-installer-base
	@echo 
	@echo "ENLIGHTEN-$(VERSION) for Linux packaged in ENLIGHTEN-linux-$(VERSION).tgz"

rpi-installer: linux-installer-base
	@echo 
	@FILENAME="ENLIGHTEN-${RPI_ID}-${RPI_CODENAME}-$(VERSION).tgz" && \
        mv ENLIGHTEN-linux-$(VERSION).tgz $$FILENAME && \
        echo "ENLIGHTEN-$(VERSION) for Raspberry Pi packaged in $$FILENAME"

mac-installer:
	@rm -rf build-mac build-mac-work
	@mkdir -p build-mac
	@echo "Running PyInstaller..." # note capitalization
	@python3.11 -m PyInstaller \
        --distpath="build-mac" \
        --workpath="build-mac-work" \
        --noconfirm \
        --clean \
        --windowed \
        --paths="../Wasatch.PY" \
        --paths="pluginExamples" \
        --hidden-import="scipy._lib.messagestream" \
        --hidden-import="scipy.special.cython_special" \
        --icon "../enlighten/assets/uic_qrc/images/EnlightenIcon.ico" \
        --specpath="scripts" \
        --name EnlightenGUI \
        scripts/Enlighten.py 2>&1 \
        | sed 's/^/pyinstaller: /'
	@mkdir -p                             build-mac/EnlightenGUI/enlighten/assets
	@cp -rv enlighten/assets/stylesheets  build-mac/EnlightenGUI/enlighten/assets
	@cp -rv enlighten/assets/example_data build-mac/EnlightenGUI/enlighten/assets
	@echo "Now run:"
	@echo "    make mac-platypus"

# install with "brew install platypus"
mac-platypus:
	@echo "Running Platypus..."
	rm -rf ENLIGHTEN-$(VERSION).app
	DIR=build-mac/EnlightenGUI ; ls $$DIR | grep -v EnlightenGUI | sed "s!^!-f $$DIR/!" > mac.bundled
	$(PLATYPUS_BIN) \
        --name "ENLIGHTEN™ $(VERSION)" \
        --interface-type 'None' \
        --interpreter /usr/bin/env \
        --app-icon enlighten/assets/uic_qrc/images/EnlightenIcon.icns \
        --app-version $(VERSION) \
        --author "Wasatch Photonics" \
        --bundle-identifier "com.wasatchphotonics.Enlighten" \
        --quit-after-execution \
        --overwrite \
        --optimize-nib \
        `cat mac.bundled` \
        build-mac/EnlightenGUI/EnlightenGUI \
        ENLIGHTEN-$(VERSION) 2>&1 \
        | sed 's/^/platypus: /'
	zip -r ENLIGHTEN-MacOS-$(VERSION).zip ENLIGHTEN-$(VERSION).app | sed 's/^/compressing: /'
	rm -rf ENLIGHTEN-$(VERSION).app
	@echo 
	@echo "ENLIGHTEN-$(VERSION).app for MacOS packaged in ENLIGHTEN-MacOS-$(VERSION).zip"

# Run ENLIGHTEN with debug logging 
run:
	@python scripts/Enlighten.py --log-level debug 1>enlighten.out 2>enlighten.err

pylint:
	@pylint enlighten plugins

flake8:
	@find enlighten plugins -name \*.py | sort | grep -v uic_qrc | xargs flake8 | tee flake8.out 
	@echo && echo Counts:
	@awk -F": " '{print $$2}' < flake8.out | sort | uniq -c | sort -n
	@rm -f flake8.out
