#!/usr/bin/make -f

# PyKaraoke ships two flavours of the pygame application in one
# source: the Python 3 port at the top level, and the pristine
# Python 2 code under python2/.  Pick whichever interpreter can
# actually import pygame (Debian 9 only has python-pygame; Debian 10
# and everything newer only need python3).
PY3_OK := $(shell python3 -c 'import pygame, numpy' >/dev/null 2>&1 && echo 1)
PY2_OK := $(shell python -c 'import pygame, numpy' >/dev/null 2>&1 && echo 1)

ifeq ($(PY3_OK)$(PY2_OK),)
$(error neither python3 nor python can import pygame and numpy)
endif

ifeq ($(PY3_OK),1)
SRC_DIR := .
INTERP := python3
else
SRC_DIR := python2
INTERP := python
endif

%:
	dh $@

override_dh_auto_build:
	# Pure Python application; nothing to build.

override_dh_auto_test:
ifeq ($(PY3_OK),1)
	python3 pykaraoke-check.py
else
	# Python 2 flavour: the sources are upstream's own 0.7.5 code;
	# syntax-check them with the interpreter that will run them.
	cd $(SRC_DIR) && python -m py_compile pycdg.py pycdgAux.py \
		pykaraoke_mini.py pykar.py pykconstants.py pykdb.py \
		pykenv.py pykmanager.py pykplayer.py pykversion.py pympg.py
endif

override_dh_auto_install:
	install -dm 0755 debian/pykaraoke/usr/share/pykaraoke
	install -m 0644 $(SRC_DIR)/pycdg.py $(SRC_DIR)/pycdgAux.py \
		$(SRC_DIR)/pykaraoke_mini.py $(SRC_DIR)/pykar.py \
		$(SRC_DIR)/pykconstants.py $(SRC_DIR)/pykdb.py $(SRC_DIR)/pykenv.py \
		$(SRC_DIR)/pykmanager.py $(SRC_DIR)/pykplayer.py \
		$(SRC_DIR)/pykversion.py $(SRC_DIR)/pympg.py \
		debian/pykaraoke/usr/share/pykaraoke/
	install -dm 0755 debian/pykaraoke/usr/share/pykaraoke/fonts
	install -m 0644 fonts/*.ttf debian/pykaraoke/usr/share/pykaraoke/fonts/
	install -dm 0755 debian/pykaraoke/usr/share/pykaraoke/icons
	install -m 0644 icons/* debian/pykaraoke/usr/share/pykaraoke/icons/
	install -dm 0755 debian/pykaraoke/usr/bin
	for tool in pykaraoke_mini pycdg pykar pympg; do \
		printf '#!/bin/sh\nexec $(INTERP) /usr/share/pykaraoke/%s.py "$$@"\n' "$$tool" \
			> debian/pykaraoke/usr/bin/$$tool; \
		chmod 0755 debian/pykaraoke/usr/bin/$$tool; \
	done
	install -m 0755 install/cdg2mpg debian/pykaraoke/usr/bin/cdg2mpg
	install -Dm 0644 install/pykaraoke_mini.desktop \
		debian/pykaraoke/usr/share/applications/pykaraoke_mini.desktop
