# This path represents the local file even if it is a symbolic link
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
project=massxpert

pwd=$(CURDIR)

topDir=${pwd}

okularPid=${topDir}/okular.pid

pdfFilePath=${topDir}/build/${project}-user-manual
pdfFilePathName=${pdfFilePath}/${project}-user-manual_color_en.pdf 
htmlDirPath=${topDir}/build/${project}-user-manual/html/${project}-user-manual

default: pdf_r

all: pdf html
	echo "mkfile_path: $(mkfile_path)"
	echo "current_dir: $(current_dir)"	

all_f: pdf_f html_f

all_f_r: pdf_f_r html_f_r

.PHONY: reader
reader:
	okular --geometry 1268x1099-0-34 $(pdfFilePathName) & echo $$! > okular.pid

# Make the pdf file without forcing without reload
pdf:
	daps -d ${topDir}/DC-${project}-user-manual --verbosity=3 pdf
	cp -fv ${pdfFilePathName} ${pdfFilePath}/${project}-doc.pdf
	cp -fv ${pdfFilePathName} ${topDir}/${project}-doc.pdf

# Make the pdf file with forcing remaking all the targets and without reload
pdf_f:
	daps --force -d ${topDir}/DC-${project}-user-manual --verbosity=3 pdf
	cp -fv ${pdfFilePathName} ${pdfFilePath}/${project}-doc.pdf
	cp -fv ${pdfFilePathName} ${topDir}/${project}-doc.pdf

# Reload with okular variant
pdf_r: pdf
	(cd ${topDir} && qdbus org.kde.okular-$(shell cat ${okularPid}) /okular reload)
	cp -fv ${pdfFilePathName} ${pdfFilePath}/${project}-doc.pdf
	cp -fv ${pdfFilePathName} ${topDir}/${project}-doc.pdf

# Reload with okular variant
pdf_f_r: pdf_f
	(cd ${topDir} && qdbus org.kde.okular-$(shell cat ${okularPid}) /okular reload)
	cp -fv ${pdfFilePathName} ${pdfFilePath}/${project}-doc.pdf
	cp -fv ${pdfFilePathName} ${topDir}/${project}-doc.pdf

html:
	daps -d ${topDir}/DC-${project}-user-manual --verbosity=3 html --static

html_f: html
	daps --force -d ${topDir}/DC-${project}-user-manual --verbosity=3 html --static

html_r: html
	firefox-reload-page.sh

html_f_r: html_f
	firefox-reload-page.sh

package_html:
	daps -d ${topDir}/DC-${project}-user-manual package-html

check_dir:
	echo "mkfile_path: $(mkfile_path)"
	echo "current_dir: $(current_dir)"	

	if [ -d "xml" ];\
	 	then \
		echo "Not in a user manual top source directory.";\
		exit 1;\
		fi	

