# 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=$(current_dir)

pwd=$(CURDIR)

topDir=".."

# srcDir will be the full path to massxpert or minexpert.
srcDir=${topDir}/${project}

okularPid=${srcDir}/okular.pid

pdfFilePath=${srcDir}/build/${project}-user-manual
pdfFilePathName=${pdfFilePath}/${project}-user-manual_color_en.pdf 
htmlDirPath=${srcDir}/build/${project}-user-manual/html/minexpert-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 ${srcDir}/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 ${srcDir}/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 ${srcDir} && 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 ${srcDir} && 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 ${srcDir}/DC-${project}-user-manual --verbosity=3 html --static

html_f: html
	daps --force -d ${srcDir}/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 ${srcDir}/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	

