1# Makefile for the Stellarium User Guide (SUG)
2# Required tools:
3# WINDOWS
4# ========
5# It is possible to run make from the Windows command shell or from Linux-like environments: CygWin, Ubuntu shell or at least the Git shell.
6# If cygwin or Ubuntu are your favorite environments and you have already installed TeXlive there,
7# you won't need MiKTeX, but can use the existing installations. Else install these tools:
8# * make and touch from GnuWin32
9# * MiKTeX from http://miktex.org. Configure it to auto-download packages as required.
10# * pdftk.exe from https://www.pdflabs.com/tools/pdftk-server/ or the pdftk.exe in the program folder of the "private" GUI version.
11# * GhostScript from https://www.ghostscript.com. Make sure to have the distribution's bin directory in PATH.
12# GIT shell: Install these tools and just make sure they are found in PATH!
13# CygWin: To build SUG with native tools, install the relevant packages from the Cygwin repository; alternatively, the Windows tools will be called
14# Ubuntu: apt-get install make texlive-full gs pdftk. On Windows' Ubuntu shell, the native Windows tools can be used if available in PATH.
15#
16# LINUX
17# =======
18# Debian, Ubuntu: apt-get install make texlive-full gs pdftk.
19# Others: make sure to install the abovementioned packages for your distribution.
20#
21# MAC OS X
22# ==========
23# * TODO: please add instructions here.
24
25
26
27# We try to detect Windows or other system to configure the tool names properly.
28# Problem: Ubuntu shell on Windows does not know environment variable OS.
29# https://stackoverflow.com/questions/38086185/how-to-check-if-a-program-is-run-in-bash-on-ubuntu-on-windows-and-not-just-plain
30#
31# On Windows with git shell, Ubuntu bash or cygwin, use rm and mv, not del and move.
32# Note that make resets SHELL to sh.exe.
33# Cygwin/Ubuntu/MinGW/Git shell's version includes full path name with forward-slash, signalling bash use.
34# So far we can also assume that we are on pure (poor) Windows commandline if SHELL=sh.exe
35
36PDFLATEX=pdflatex
37HTLATEX=htlatex
38BIBER=biber
39MAKEINDEX=makeindex
40MAKE4HT=make4ht
41
42ifeq ($(OS),Windows_NT) # variable OS not set under Win10/Ubuntu!
43	ifeq ($(findstring /sh, $(SHELL)),/sh)
44		RM=rm -fr
45		MV=mv
46		ifeq ($(findstring gs,$(shell which gs 2>/dev/null)),gs)
47			GS=gs
48		else
49			ifeq ($(findstring gswin64c,$(shell which gswin64c 2>/dev/null)),gswin64c)
50				GS=gswin64c.exe
51			else
52				GS=gswin32c.exe
53			endif
54			MAKE4HT=make4ht.exe
55		endif
56		PDFTK=pdftk
57	else # cmd shell. TODO: hide error message of "where".
58		RM=del /Q
59		MV=rename
60		ifeq ($(findstring gswin64c,$(shell where gswin64c)),gswin64c)
61				GS=gswin64c.exe
62			else
63				GS=gswin32c.exe
64			endif
65		PDFTK=pdftk.exe
66		MAKE4HT=make4ht.exe
67	endif
68else
69	# OS not Windows on most Non-Windows environments, but may still be Ubuntu bash on Windows!
70	# In essence, if tool not found, fall back to that tools' names from the Windows environment.
71	# It is assumed that at least the Windows tools are configured to lie in PATH.
72	ifneq ($(findstring pdflatex, $(shell which pdflatex)),pdflatex)
73		PDFLATEX=pdflatex.exe
74	endif
75	ifneq ($(findstring htlatex, $(shell which htlatex)),htlatex)
76		HTLATEX=htlatex.exe
77	endif
78	ifneq ($(findstring biber, $(shell which biber)),biber)
79		BIBER=biber.exe
80	endif
81	ifneq ($(findstring makeindex, $(shell which makeindex)),makeindex)
82		MAKEINDEX=makeindex.exe
83	endif
84	ifneq ($(findstring pdftk, $(shell which pdftk)),pdftk)
85		PDFTK=pdftk.exe
86	else
87		PDFTK=pdftk
88	endif
89	ifneq ($(findstring gs,$(shell which gs)),gs)
90		GS=gswin64c.exe
91	else
92		GS=gs
93	endif
94	RM=rm -fr
95	MV=mv
96	SHELL=/bin/bash
97endif
98
99# htlatex needs bounding box files.
100PICSpdf=$(wildcard pictures/*.pdf)
101PICSpng=$(wildcard pictures/*.png) $(subst .pdf,.png, ${PICSpdf})
102PICSjpg=$(wildcard pictures/*.jpg)
103PICSbbx=$(subst .png,.xbb, ${PICSpng}) $(subst .jpg,.xbb, ${PICSjpg})
104
105
106.DEFAULT:
107.PHONY: help diag clean distclean guide pdf manual SUG html
108
109.SUFFIXES:
110.SUFFIXES: .tex .aux .log .lot .lof .toc .bbl .ind .dvi .pdf .eps .jpg .png .html .ps
111
112# This is to keep intermediate files until explicitly removed with make clean.
113.SECONDARY:
114
115
116help:
117	@echo               Stellarium Manual Options
118	@echo               ==========================
119	@echo PDFLaTeX creates the prettiest PDF, but it is quite large.
120	@echo targets:
121	@echo clean: remove intermediate files.
122	@echo distclean: also remove the PDF targets SUG.pdf, guide.pdf
123	@echo pdf = guide = manual = guide.pdf :  Stellarium User Guide \(hi-res\)
124	@echo SUG = SUG.pdf : as final target applies compression via GhostScript \(slow!\)
125	@echo html: create online version. \(EXPERIMENTAL, WIP\)
126	@echo diag: print some diagnostics about tool names.
127
128clean:
129	${RM} *~ core *.aux *.log *.lof *.toc *.out *.blg *.bcf *.bbl *.idx *.ilg *.ind *.ptc
130	${RM} *.run.xml *.4ct *.4tc *.tmp *.dvi guide_meta.txt *.html *.lg *.idv *.css *.svg *.xref guide*.png
131
132distclean: clean
133	${RM} guide.pdf SUG.pdf ${PICSbbx} html
134
135manual: guide.pdf
136
137guide: guide.pdf
138
139pdf: guide.pdf
140
141dvi: guide.dvi
142
143html: guide.html
144
145# Installation notes for some tools:
146# Use any recent LaTeX distribution like TeXlive (Linux) or MiKTeX 2.9 or later.
147
148# The size reduction will need GhostScript and some add-on tools.
149# 2 options were explored on Windows 10's Ubuntu 14.04 LTS:
150# * install https://github.com/pts/pdfsizeopt
151#   sudo apt-get install default-jre
152#   download Multivalent.jar from https://sourceforge.net/projects/multivalent/?source=typ_redirect and put it into PATH
153#   The official project seems defunct. Found old version at  http://www.accountingenhancements.com/multivalent.sourceforge.net/files/Multivalent20060102.jar
154#   run target SUG.pdf
155# * optpdf. This removes the opening view settings from the PDF which have to be restored with pdfmark hacks.
156#   We also need pdftk for this.
157# --> make SUG.pdf
158
159# ================ GENERAL RULES ==================================
160
161# TODO: A rule that securely detects when guide.pdf is complete and no more action is required. Probably we need to make out own logfile grep for "Rerun Latex" and a stop file target.
162# Issue: bbl depends on bcf which is created on every run. Likewise, ind depends in idx. We should detect when pdflatex does not emit a warning about changed page numbers.
163# Currently, we assume page numbers have settled by the 3rd run, and artificially change the file ages with touch to convince make to stop.
164
165#%.tex : ;
166#Makefile: ;
167# we must call makeindex again just before final run to update page numbers. Not too intuitive, but seems necessary...
168guide.pdf : guide.ind guide.bbl
169	${PDFLATEX} '\PassOptionsToPackage{draft}{graphicx}\input guide.tex'
170	${MAKEINDEX} guide.idx -s StyleInd.ist
171	${PDFLATEX} '\PassOptionsToPackage{draft}{graphicx}\input guide.tex'
172	${PDFLATEX} guide.tex
173	@touch -c guide.ind
174	@touch -c guide.bbl
175	@touch -c $@
176
177guide.dvi : guide.tex
178	+make ${PICSpng}
179	+make ${PICSbbx}
180	latex $<
181
182#guide.ind: guide.idx guide.toc
183guide.ind: guide.idx guide.aux
184	${MAKEINDEX} guide.idx -s StyleInd.ist
185
186guide.bbl: guide.bcf guide.bib
187	${BIBER} guide
188
189guide.idx: guide.log
190
191guide.toc: guide.log
192
193guide.bcf: guide.log
194
195guide.aux: guide.log
196
197guide.log: $(wildcard *.tex)
198	${PDFLATEX} guide.tex
199
200SUG SUG.pdf: guide_opt.pdf
201	${MV} $< SUG.pdf
202
203%_meta.txt : %.pdf
204	${PDFTK} $< dump_data_utf8 > $@
205
206
207# Reduced file size by 65%. But we need to restore some meta info.
208%_opt.pdf : %.pdf %_meta.txt %_pdfmark.ps
209	${GS} -sDEVICE=pdfwrite -dCompatibilityLevel=1.6 -dNOPAUSE -dQUIET -dBATCH  -sOutputFile=$*_gswrite.pdf  $*.pdf $*_pdfmark.ps
210	${PDFTK} $*_gswrite.pdf update_info_utf8 $*_meta.txt output $@
211	${RM} $*_gswrite.pdf $*_meta.txt
212
213
214pdf2png: $(subst .pdf,.png, ${PICSpdf})
215
216pictures/%.png : pictures/%.pdf
217	convert $< $@
218
219%.xbb: %.png
220	ebb -x $<
221
222%.xbb: %.jpg
223	ebb -x $<
224
225## pdfsizeopt unfortunately hardly makes the SUG smaller :-(
226## But it is recommended to run Multivalent after that!
227## NOPE! It destroys images.
228#SUG.pdf : guide.pdf
229#	pdfsizeopt $< $@
230#	java -classpath Multivalent20060102.jar tool.pdf.Compress -jpeg $@
231#	mv SUG-o.pdf SUG.pdf
232
233htdia:
234	@echo Want to create ${PICSbbx}
235
236
237# HTML: Work in progress. I run into many issues that have been discussed in various TeX fora.
238# TODO: Whoever knows about htlatex/tex4ht is welcome to help in fixing formatting macros or properly
239# configuring the build of a friendly frame set.
240# it seems tex4ht needs more than just running htlatex, but we need some configuration file.
241#guide.html: guide.bbl guide.ind
242#
243# See https://github.com/michal-h21/helpers4ht/wiki/tex4ht-tutorial
244# htlatex filename "options for tex4ht.sty" "options for tex4ht" "options for t4ht" "LaTeX options"
245
246html-draft: $(wildcard *.tex) ${PICSbbx}
247	@echo Shell is $(SHELL)
248	make4ht --output-dir html --utf8 guide -m draft guide "htFixes"
249#	make4ht --output-dir html --utf8 guide -m draft guide "htFixes"
250
251
252# GZ we don't use the htlatex command script because it does not call biber or makeindex.
253# guide.mk4 is a config file for Stellarium HTML Guide to be processed by make4ht. See https://github.com/michal-h21/make4ht for details.
254
255guide.html: $(wildcard *.tex) guide.mk4 htFixes.cfg ${PICSbbx}
256	@echo Shell is $(SHELL)
257	${MAKE4HT} --output-dir html --utf8 guide "htFixes"
258#	make4ht --output-dir html --utf8 guide "htFixes" " -cunihtf -utf8"
259#	${HTLATEX} guide "htFixes" " -cunihtf -utf8"
260# Currently, everything after running biber fails. :-(
261#	${BIBER} guide
262#	tex '\def\filename{{guide}{idx}{4dx}{ind}} \input  idxmake.4ht'
263#	${MAKEINDEX} -o guide.ind guide.4dx
264#	${HTLATEX} guide "htFixes,html,1,next,index=3,fn-in,frames,mouseover,info"
265#	${HTLATEX} guide "htFixes,html,1,next,index=3,fn-in,frames,mouseover,info"
266
267
268
269# This throws an error by lua when processing the tex line. The currently used mk4 lacks the index related stuff.
270# Also, it seems ${filename} in the biber argument does not work properly. Should be target without extension.
271guide.mk4:
272	@echo if mode == "draft" then   > $@
273	@echo Make:htlatex()           >> $@
274	@echo else                     >> $@
275	@echo   Make:add("biber", "biber ${input}")                                                        >> $@
276	@echo   Make:add("texindex", "tex '\def\filename{{guide}{idx}{4dx}{ind}} \input  idxmake.4ht'") >> $@
277	@echo   Make:add("makeindex", "$makeindex ${filename}.idx -s StyleInd.ist")                     >> $@
278	@echo   local filter = require "make4ht-filter"                                                 >> $@
279	@echo   local process = filter{"cleanspan", "fixligatures", "hruletohr", "fix-links"}           >> $@
280	@echo   Make:htlatex()     >> $@
281	@echo   Make:biber()       >> $@
282	@echo   Make:texindex()    >> $@
283	@echo   Make:makeindex()   >> $@
284	@echo   Make:htlatex()     >> $@
285	@echo   Make:htlatex()     >> $@
286	@echo   Make:match("html$",process)                                 >> $@
287	@echo  Make:match("html$", "tidy -m -xml -utf8 -q -i ${filename}")  >> $@
288	@echo end                  >> $@
289
290
291
292# Totally non-tex4ht: Try pandoc? (start to collect arguments here, try "make pandoc")
293# Ubuntu on Windows only has pandoc 1.12. Maybe V2 would be more complete? In any case, tabu or some other package seems to be a problem.
294pandoc:
295		pandoc -f latex -t plain guide.tex
296
297
298# Hint on restoring initial view settings
299# from https://stackoverflow.com/questions/25544002/command-line-tool-to-mark-pdf-to-open-in-single-page-view
300#guide_pdfmark.ps: Makefile
301#		echo "[ /PageMode /UseOutlines  % Display bookmarks upon opening the doc\n\
302#             %/PageMode /UseThumbs    % Display thumbnails upon opening the doc\n\
303#              %/PageMode /FullScreen   % Open the document in fullscreen mode\n\
304#              %/PageMode /None         % Display neither bookmarks nor thumbnails upon opening\n\
305#              /Page 1                 % Open document with page 1\n\
306#              %/View [ /XYZ null null null ]\n\
307#                          % Go to specified page and retain same ... \n\
308#                          % ... horizontal/vertical offset+zoom as current page\n\
309#              /View [ /Fit ]           % Fit page to window\n\
310#              %/View /FitB             % Fit visible part of page to window\n\
311#              %/View [/FitH 220]       % Fit page width to window; 220 is distance ...\n\
312#                                       % ... of page origin from top of window\n\
313#                                         /DOCVIEW    pdfmark\n\
314#              \n\
315#              [ {Catalog} <<\n\
316#               /ViewerPreferences<</DisplayDocTitle true>>\n\
317#               %/PageLayout /SinglePage\n\
318#               %/PageLayout /OneColumn\n\
319#               /PageLayout /TwoPageRight\n\
320#               %/PageLayout /TwoColumnLeft\n\
321#               >>                        /PUT        pdfmark"\
322#			 > $@
323
324
325# Very hackish diagnostics. There are many ways to get a decent Linux-ish system even under Windows.
326# CygWin, MinGW and Git bash understand calling programs with .exe from the Windows environment.
327# Ubuntu needs .exe extension to be given explicitly. This makes the Makefile a bit ugly.
328diag:
329ifeq ($(SHELL),sh.exe)
330	@echo Looks like plain WINDOWS command shell.
331else
332 ifeq ($(findstring Git, $(SHELL)),Git)
333	@echo INFO: We are in MinGW/git shell, right?
334 else
335  ifeq ($(findstring Microsoft, $(shell cat /proc/version)), Microsoft)
336	@echo INFO: This looks like Ubuntu on Windows
337  else
338   ifeq ($(findstring CYGWIN, $(shell uname)),CYGWIN)
339	@echo INFO: We are on Cygwin, right?
340   else
341	@echo I assume $(shell uname) is a valid environment. Please enhance this line.
342   endif
343  endif
344 endif
345endif
346	@echo SHELL=${SHELL}
347	@echo PDFLATEX=${PDFLATEX}
348	@echo MAKEINDEX=${MAKEINDEX}
349	@echo BIBER=${BIBER}
350	@echo PDFTK=${PDFTK}
351	@echo GS=${GS}
352	@echo RM=${RM}
353	@echo MV=${MV}
354	@echo MAKE4HT=$(MAKE4HT)
355