#/***************************************************************************
# tdsToQgis
#
# Makefile to deploy and package the plugin.
#                            -------------------
#       begin               : 2022-10-08
#       git sha             : $Format:%H$
#       copyright           : (C) 2022 by Pedro Ceccopieri
#       email               : pedroceccopieri@gmail.com
# ***************************************************************************/
#
#/***************************************************************************
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU General Public License as published by  *
# *   the Free Software Foundation; either version 2 of the License, or     *
# *   (at your option) any later version.                                   *
# *                                                                         *
# ***************************************************************************/
#
# Works on Linux, macOS and Windows. It needs GNU make, a POSIX shell (sh, cp,
# mkdir, rm), Python 3 and git:
#   * Linux: install make with the package manager (e.g. sudo apt install make).
#   * macOS: xcode-select --install (installs make and git).
#   * Windows: run it from Git Bash (installed with Git for Windows), after
#     installing make (e.g. winget install ezwinports.make).
#
# Targets:
#   make deploy                  copy the plugin to the QGIS plugins folder
#   make derase                  remove the plugin from the QGIS plugins folder
#   make zip                     create tds2qgis.zip from the current files (Install from ZIP)
#   make package VERSION=v2.0    create tds2qgis.zip from a git tag or commit
#   make clean                   remove the files created by zip/package
#   make info                    show the folders used
#
# Options (e.g. make deploy QGIS_PROFILE=work):
#   QGIS_PROFILE        QGIS user profile (default: default)
#   QGIS_PROFILES_DIR   folder of the QGIS profiles (default: the standard folder of the system)
#   PYTHON              Python 3 command (default: python3, or python on Windows)

#################################################
# Plugin files
#################################################

PLUGINNAME = tds2qgis

PY_FILES = \
    __init__.py \
    tds2qgis.py utils.py \
    windowControl.py windowModel.py windowView.py \
    infoWindow.py addDatasetDialog.py \
    addDialogView.py removeDialogView.py saveDialogView.py

UI_FILES = infoWindow_dockwidget.ui addDatasetDialog.ui

EXTRAS = metadata.txt icon.png LICENSE

# subdirectories of the plugin folder that must be deployed (none for now)
EXTRA_DIRS =

PLUGIN_FILES = $(PY_FILES) $(UI_FILES) $(EXTRAS)

#################################################
# QGIS plugins folder of each operating system
#################################################

QGIS_PROFILE ?= default

ifeq ($(OS),Windows_NT)
    # %APPDATA%\QGIS\QGIS3\profiles (backslashes converted for the shell)
    QGIS_PROFILES_DIR ?= $(subst \,/,$(APPDATA))/QGIS/QGIS3/profiles
    PYTHON ?= python
else
    UNAME_S ?= $(shell uname -s)
    ifeq ($(UNAME_S),Darwin)
        QGIS_PROFILES_DIR ?= $(HOME)/Library/Application Support/QGIS/QGIS3/profiles
    else
        QGIS_PROFILES_DIR ?= $(HOME)/.local/share/QGIS/QGIS3/profiles
    endif
    PYTHON ?= python3
endif

# the paths are always quoted in the commands: the macOS folder has a space
PLUGINS_DIR = $(QGIS_PROFILES_DIR)/$(QGIS_PROFILE)/python/plugins
PLUGIN_DIR = $(PLUGINS_DIR)/$(PLUGINNAME)

# folder where the zip file is prepared
BUILD_DIR = build

#################################################
# Targets
#################################################

.PHONY: default info deploy derase zip package clean

default:
	@echo "Usage: make <target>   (see the comments at the top of the Makefile)"
	@echo "  deploy, derase, zip, package VERSION=<tag>, clean, info"

info:
	@echo "Plugin:          $(PLUGINNAME)"
	@echo "QGIS profile:    $(QGIS_PROFILE)"
	@echo "Plugins folder:  $(PLUGINS_DIR)"
	@echo "Deploy folder:   $(PLUGIN_DIR)"
	@echo "Python:          $(PYTHON)"

deploy:
	@echo "Deploying the plugin to: $(PLUGIN_DIR)"
	mkdir -p "$(PLUGIN_DIR)"
	cp -f $(PLUGIN_FILES) "$(PLUGIN_DIR)"
	$(foreach dir,$(EXTRA_DIRS),cp -R "$(dir)" "$(PLUGIN_DIR)";)
	@echo "Done. Restart QGIS (or reload the plugin) to use the new version."

derase:
	@echo "Removing the plugin from: $(PLUGIN_DIR)"
	rm -rf "$(PLUGIN_DIR)"

# zip with only the plugin files (no test datasets), with the folder named $(PLUGINNAME)
zip:
	rm -rf "$(BUILD_DIR)" "$(PLUGINNAME).zip"
	mkdir -p "$(BUILD_DIR)/$(PLUGINNAME)"
	cp -f $(PLUGIN_FILES) "$(BUILD_DIR)/$(PLUGINNAME)"
	$(foreach dir,$(EXTRA_DIRS),cp -R "$(dir)" "$(BUILD_DIR)/$(PLUGINNAME)";)
	cd "$(BUILD_DIR)" && $(PYTHON) -m zipfile -c "../$(PLUGINNAME).zip" "$(PLUGINNAME)"
	rm -rf "$(BUILD_DIR)"
	@echo "Created $(PLUGINNAME).zip"

# zip of a git tag or commit (only committed files), with the folder named $(PLUGINNAME)
package:
	@if [ -z "$(VERSION)" ]; then echo "Usage: make package VERSION=<git tag or commit>, e.g. make package VERSION=v2.0"; exit 1; fi
	rm -f "$(PLUGINNAME).zip"
	git archive --prefix="$(PLUGINNAME)/" -o "$(PLUGINNAME).zip" "$(VERSION)"
	@echo "Created $(PLUGINNAME).zip from $(VERSION)"

clean:
	rm -rf "$(BUILD_DIR)" "$(PLUGINNAME).zip"
