1# - Try to find LyX, and define some custom commands to export from LyX
2#
3# Once done, this will define:
4#  LYX_FOUND - system has LyX
5#  LYX_COMMAND - the command to run
6#
7# and the following new functions:
8#  lyx_export(<format> <extension-without-leading-dot> <output-variable>
9#    INPUT <lyx-file> [...]
10#    [OUTPUT_TO_SOURCE_DIR]
11#    [ EXTRA_DEPS <bibtex-or-other-file> [...] ]) - the base function
12#
13# These shortcut functions all have the same syntax:
14#  lyx_export_to_XXX(<output-variable>
15#    INPUT <lyx-file> [...]
16#    [OUTPUT_TO_SOURCE_DIR]
17#    [ EXTRA_DEPS <bibtex-or-other-file> [...] ])
18#
19# Available shortcuts:
20#  lyx_export_to_docbook_xml
21#  lyx_export_to_docbook
22#  lyx_export_to_pdf
23#  lyx_export_to_pdf_via_pdflatex
24#  lyx_export_to_pdf_via_dvi
25#
26# Useful configuration variables you might want to add to your cache:
27#  LYX_ROOT_DIR - A directory prefix to search
28#
29# Original Author:
30# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
31# http://academic.cleardefinition.com
32# Iowa State University HCI Graduate Program/VRAC
33#
34# Copyright Iowa State University 2009-2010.
35# Distributed under the Boost Software License, Version 1.0.
36# (See accompanying file LICENSE_1_0.txt or copy at
37# http://www.boost.org/LICENSE_1_0.txt)
38
39
40set(LYX_ROOT_DIR
41	"${LYX_ROOT_DIR}"
42	CACHE
43	PATH
44	"Directory to start our search in")
45
46find_program(LYX_COMMAND
47	NAMES
48	lyx
49	HINTS
50	"${LYX_ROOT_DIR}"
51	PATH_SUFFIXES
52	bin)
53
54# handle the QUIETLY and REQUIRED arguments and set xxx_FOUND to TRUE if
55# all listed variables are TRUE
56include(FindPackageHandleStandardArgs)
57find_package_handle_standard_args(LyX DEFAULT_MSG LYX_COMMAND)
58
59if(LYX_FOUND)
60	mark_as_advanced(LYX_ROOT_DIR)
61endif()
62
63mark_as_advanced(LYX_COMMAND)
64
65function(lyx_export _format _extension _outvar)
66	set(_nowhere)
67	set(_curdest _nowhere)
68	set(_val_args EXTRA_DEPS INPUT)
69	set(_bool_args OUTPUT_TO_SOURCE_DIR)
70	foreach(_arg ${_val_args} ${_bool_args})
71		set(${_arg})
72	endforeach()
73	foreach(_element ${ARGN})
74		list(FIND _val_args "${_element}" _val_arg_find)
75		list(FIND _bool_args "${_element}" _bool_arg_find)
76		if("${_val_arg_find}" GREATER "-1")
77			set(_curdest "${_element}")
78		elseif("${_bool_arg_find}" GREATER "-1")
79			set("${_element}" ON)
80			set(_curdest _nowhere)
81		else()
82			list(APPEND ${_curdest} "${_element}")
83		endif()
84	endforeach()
85
86	if(_nowhere)
87		message(FATAL_ERROR "Syntax error in use of a lyx_export command!")
88	endif()
89
90	set(_out)
91	set(_outname)
92	foreach(_file ${INPUT})
93		get_filename_component(_base "${_file}" NAME_WE)
94
95		if(NOT OUTPUT_TO_SOURCE_DIR)
96			set(_outname "${CMAKE_CURRENT_BINARY_DIR}/${_base}.${_extension}")
97		else()
98			set(_outname "${CMAKE_CURRENT_SOURCE_DIR}/${_base}.${_extension}")
99		endif()
100
101		list(APPEND _out "${_outname}")
102		if(LYX_COMMAND)
103			add_custom_command(OUTPUT "${_outname}"
104				COMMAND ${CMAKE_COMMAND} -E remove "${_outname}"
105				#COMMAND ${LYX_COMMAND} "${_file}" --export ${_format}
106				COMMAND ${LYX_COMMAND} "${_file}"
107				--execute
108				"buffer-export-custom ${_format} ${CMAKE_COMMAND} -E copy '$$$$FName' '${_outname}'"
109				--execute
110				"lyx-quit"
111				MAIN_DEPENDENCY "${_file}"
112				WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
113				DEPENDS "${_file}" ${EXTRA_DEPS}
114				COMMENT "Exporting ${_file} to ${_format}...")
115		endif()
116	endforeach()
117
118	set(${_outvar} ${_out} PARENT_SCOPE)
119endfunction()
120
121function(lyx_export_to_docbook_xml _outvar)
122	lyx_export(docbook-xml xml ${_outvar} ${ARGN})
123	set(${_outvar} ${${_outvar}} PARENT_SCOPE)
124endfunction()
125
126function(lyx_export_to_docbook _outvar)
127	lyx_export(docbook sgml ${_outvar} ${ARGN})
128	set(${_outvar} ${${_outvar}} PARENT_SCOPE)
129endfunction()
130
131function(lyx_export_to_pdf _outvar)
132	lyx_export(pdf pdf ${_outvar} ${ARGN})
133	set(${_outvar} ${${_outvar}} PARENT_SCOPE)
134endfunction()
135
136function(lyx_export_to_pdf_via_pdflatex _outvar)
137	lyx_export(pdf2 pdf ${_outvar} ${ARGN})
138	set(${_outvar} ${${_outvar}} PARENT_SCOPE)
139endfunction()
140
141function(lyx_export_to_pdf_via_dvi _outvar)
142	lyx_export(pdf3 pdf ${_outvar} ${ARGN})
143	set(${_outvar} ${${_outvar}} PARENT_SCOPE)
144endfunction()
145