#***************************************************************************************************
#                                             Makefile                                             *
#                                            ----------                                            *
# Description : House-keeping for the GNU Spice GUI project LaTeX documentation and SVG drawings.  *
# Started     : 2020-08-16                                                                         *
# Updated     : 2020-10-11                                                                         *
# Copyright   : (C) 2020 by MSWaters                                                               *
# Note        : Enter "make help" to view available Makefile targets.                              *
#***************************************************************************************************

#***************************************************************************************************
#                                                                                                  *
#       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 3 of the License, or (at your option) any later version.                    *
#                                                                                                  *
#***************************************************************************************************

#***************************************************************************************************
# Any or all of the values in this section may be set below or on the command line when envoking
# make eg. :
#
#   make DESTDIR=/new/dest/dir install
#   make DESTDIR=/new/dest/dir uninstall
#
# Note : Values specified on the command line over-ride those specified below.
#***************************************************************************************************

# Destination (install) directory
DESTDIR = /usr/local

#***************************************************************************************************
# Specify string values
#***************************************************************************************************

# Root directory
ROOT := $(shell cd .. ; pwd)

#***************************************************************************************************
# Make these targets (the order matters, the images and icons must be built first)
#***************************************************************************************************

all : images icons pdf html

#***************************************************************************************************
# Build PDF documentation from LaTeX sources
#***************************************************************************************************

pdf :
	cd ./tex ; pdflatex -halt-on-error gspiceui-man.tex
	mv ./tex/gspiceui-man.pdf .

#***************************************************************************************************
# Build HTML documentation from LaTeX sources
# (This can be done using either : htlatex, make4ht or pandoc
#***************************************************************************************************

html :
	mkdir -p html
	cd ./tex ; htlatex gspiceui-man.tex "xhtml,html5,charset=utf-8" " -cunihtf -utf8"
#	cd ./tex ; make4ht gspiceui-man.tex  # This command is apparently the same as the above one
	cd ./tex ; mv gspiceui-man.html gspiceui-man.css ../html

#***************************************************************************************************
# Convert SVG drawing files to TEX image files (ie. SVG -> PDF -> PDF_TEX)
#***************************************************************************************************

IMAGES := $(patsubst ./svg/%.svg,./images/%.pdf,$(wildcard ./svg/*.svg))

images : $(IMAGES)

# Drawing conversion (from SVG to PDF) rules :
#   -D             defines the export area (the long option is --export-area-drawing)
#   -o             is the output file name (the long option is --export-filename=<FILENAME>)
#   $<             is the name of the first dependency (the SVG file)
#   $@             is the file name of the target      (the PDF file)

./images/%.pdf : ./svg/%.svg
	inkscape -D -o $@ $<

#***************************************************************************************************
# Convert XPM icon files to PNG image format
#***************************************************************************************************

ICONS = ./images/file-open.png   ./images/file-import.png ./images/file-reload.png  \
	      ./images/file-close.png  ./images/sim-create.png  ./images/sim-run.png      \
	      ./images/sim-stop.png    ./images/sim-edit.png    ./images/sim-plot.png     \
	      ./images/calculator.png  ./images/preferences.png ./images/help.png

icons : $(ICONS)

# sam2p Rules :
#   $<  is the name of the first dependency (XPM file)
#   $@  is the file name of the target      (PNG file)

./images/%.png : ../src/icons/%.xpm
	magick convert $< $@

#***************************************************************************************************
# Perform installation tasks
#***************************************************************************************************

install :
	mkdir -p $(DESTDIR)/share/gspiceui/doc
	cp $(ROOT)/gspiceui-man.pdf $(DESTDIR)/share/gspiceui/doc
	mkdir -p $(DESTDIR)/share/gspiceui/doc/html
	cp $(ROOT)/doc/html/* $(DESTDIR)/share/gspiceui/doc/html

#***************************************************************************************************
# Perform uninstall tasks
#***************************************************************************************************

uninstall :
	rm -fR $(DESTDIR)/share/gspiceui/doc

#***************************************************************************************************
# Remove automatically generated files, temporary files and backup files
#***************************************************************************************************

clean : clean_tex clean_images clean_icons

clean_tex :
	rm -f Makefile~
	cd ./tex ; rm -f gspiceui-man.aux gspiceui-man.log gspiceui-man.out gspiceui-man.toc
	cd ./tex ; rm -f gspiceui-man.4ct gspiceui-man.4tc gspiceui-man.aux gspiceui-man.dvi
	cd ./tex ; rm -f gspiceui-man.idv gspiceui-man.lg  gspiceui-man.log gspiceui-man.tmp
	cd ./tex ; rm -f gspiceui-man.xref texput.log
#	rm -f ./gspiceui-man.pdf
	rm -f ./html/gspiceui-man.html ./html/gspiceui-man.css

clean_images :
	rm -f $(IMAGES) ./images/*.pdf_tex

clean_icons :
	rm -f $(ICONS)

#***************************************************************************************************
# Display a help message
#***************************************************************************************************

help :
	@echo
	@echo -e "gSpiceUI documentation build system. "
	@echo
	@echo -e "Available make targets :"
	@echo
	@echo -e "  all          - Build all of the targets : pdf, html, images & icons (default)"
	@echo -e "  pdf          - Generate PDF  documentation from the LaTeX source files"
	@echo -e "  html         - Generate HTML documentation from the LaTeX source files"
	@echo -e "  images       - Generate PDF  image files   from SVG drawings"
	@echo -e "  icons        - Generate PNG  image files   from XPM toolbar icon files"
	@echo -e "  install      - Install   the PDF and HTML documentation files"
	@echo -e "  uninstall    - Uninstall the PDF and HTML documentation files"
	@echo -e "  clean        - Remove all automatically generated files"
	@echo -e "  clean_tex    - Remove all file automatically generated by LaTeX"
	@echo -e "  clean_images - Remove all file automatically generated image files"
	@echo -e "  clean_icons  - Remove all file automatically generated icon  files"
	@echo -e "  help         - Display this message"
	@echo

#***************************************************************************************************
# Specify phony targets
#***************************************************************************************************

.PHONY : pdf html images icons install uninstall clean clean_tex clean_images clean_icons help

#***************************************************************************************************
