summaryrefslogtreecommitdiff
path: root/eclass/office-ext-r1.eclass
blob: 7ac6145342a8b0e197250c5cee7696da5a33bfbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Copyright 1999-2019 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: office-ext-r1.eclass
# @MAINTAINER:
# The office team <office@gentoo.org>
# @AUTHOR:
# Tomáš Chvátal <scarabeus@gentoo.org>
# @SUPPORTED_EAPIS: 5 6 7
# @BLURB: Eclass for installing libreoffice/openoffice extensions
# @DESCRIPTION:
# Eclass for easing maintenance of libreoffice/openoffice extensions.

case "${EAPI:-0}" in
	5|6) inherit eutils multilib ;;
	7) ;;
	*) die "EAPI=${EAPI} is not supported" ;;
esac

OEXT_EXPORTED_FUNCTIONS="src_unpack src_install pkg_postinst pkg_prerm"

# @ECLASS-VARIABLE: OFFICE_REQ_USE
# @DESCRIPTION:
# Useflags required on office implementation for the extension.
#
# Example:
# @CODE
# OFFICE_REQ_USE="java,jemalloc(-)?"
# @CODE
if [[ ${OFFICE_REQ_USE} ]]; then
	# Append the brackets for the depend bellow
	OFFICE_REQ_USE="[${OFFICE_REQ_USE}]"
fi

# @ECLASS-VARIABLE: OFFICE_IMPLEMENTATIONS
# @DESCRIPTION:
# List of implementations supported by the extension.
# Some work only for libreoffice and vice versa.
# Default value is all implementations.
#
# Example:
# @CODE
# OFFICE_IMPLEMENTATIONS=( "libreoffice" "openoffice" )
# @CODE
[[ -z ${OFFICE_IMPLEMENTATIONS} ]] && OFFICE_IMPLEMENTATIONS=( "libreoffice" "openoffice" )

# @ECLASS-VARIABLE: OFFICE_EXTENSIONS
# @REQUIRED
# @DESCRIPTION:
# Array containing list of extensions to install.
#
# Example:
# @CODE
# OFFICE_EXTENSIONS=( ${PN}_${PV}.oxt )
# @CODE
[[ -z ${OFFICE_EXTENSIONS} ]] && die "OFFICE_EXTENSIONS variable is unset."
if [[ "$(declare -p OFFICE_EXTENSIONS 2>/dev/null 2>&1)" != "declare -a"* ]]; then
	die "OFFICE_EXTENSIONS variable is not an array."
fi

# @ECLASS-VARIABLE: OFFICE_EXTENSIONS_LOCATION
# @DESCRIPTION:
# Path to the extensions location. Defaults to ${DISTDIR}.
#
# Example:
# @CODE
# OFFICE_EXTENSIONS_LOCATION="${S}/unpacked/"
# @CODE
: ${OFFICE_EXTENSIONS_LOCATION:=${DISTDIR}}

IUSE=""
RDEPEND=""

for i in ${OFFICE_IMPLEMENTATIONS[@]}; do
	IUSE+=" office_implementation_${i}"
	if [[ ${i} == "openoffice" ]]; then
		# special only binary
		RDEPEND+="
			office_implementation_openoffice? (
				app-office/openoffice-bin${OFFICE_REQ_USE}
			)
		"
	else
		RDEPEND+="
			office_implementation_${i}? (
				|| (
					app-office/${i}${OFFICE_REQ_USE}
					app-office/${i}-bin${OFFICE_REQ_USE}
				)
			)
		"
	fi
done

REQUIRED_USE="|| ( "
for i in ${OFFICE_IMPLEMENTATIONS[@]}; do
	REQUIRED_USE+=" office_implementation_${i} "
done
REQUIRED_USE+=" )"

DEPEND="${RDEPEND}
	app-arch/unzip
"

# Most projects actually do not provide any relevant sourcedir as they are oxt.
S="${WORKDIR}"

# @FUNCTION: office-ext-r1_src_unpack
# @DESCRIPTION:
# Flush the cache after removal of an extension.
office-ext-r1_src_unpack() {
	debug-print-function ${FUNCNAME} "$@"
	local i

	default

	for i in ${OFFICE_EXTENSIONS[@]}; do
		# Unpack the extensions where required and add case for oxt
		# which should be most common case for the extensions.
		if [[ -f "${OFFICE_EXTENSIONS_LOCATION}/${i}" ]] ; then
			case ${i} in
				*.oxt)
					mkdir -p "${WORKDIR}/${i}/"
					pushd "${WORKDIR}/${i}/" > /dev/null
					echo ">>> Unpacking "${OFFICE_EXTENSIONS_LOCATION}/${i}" to ${PWD}"
					unzip -qo ${OFFICE_EXTENSIONS_LOCATION}/${i}
					assert "failed unpacking ${OFFICE_EXTENSIONS_LOCATION}/${i}"
					popd > /dev/null
					;;
				*) unpack ${i} ;;
			esac
		fi
	done
}

# @FUNCTION: office-ext-r1_src_install
# @DESCRIPTION:
# Install the extension source to the proper location.
office-ext-r1_src_install() {
	debug-print-function ${FUNCNAME} "$@"
	debug-print "Extensions: ${OFFICE_EXTENSIONS[@]}"

	local i j

	for i in ${OFFICE_IMPLEMENTATIONS[@]}; do
		if use office_implementation_${i}; then
			if [[ ${i} == openoffice ]]; then
				# OOO needs to use uno because direct deployment segfaults.
				# This is bug by their side, but i don't want to waste time
				# fixing it myself.
				insinto /usr/$(get_libdir)/${i}/share/extension/install
				for j in ${OFFICE_EXTENSIONS[@]}; do
					doins ${OFFICE_EXTENSIONS_LOCATION}/${j}
				done
			else
				for j in ${OFFICE_EXTENSIONS[@]}; do
					pushd "${WORKDIR}/${j}/" > /dev/null
					insinto /usr/$(get_libdir)/${i}/share/extensions/${j/.oxt/}
					doins -r *
					popd > /dev/null
				done
			fi
		fi
	done
}

#### OPENOFFICE COMPAT CODE

UNOPKG_BINARY="/usr/lib64/openoffice/program/unopkg"

# @FUNCTION: office-ext-r1_add_extension
# @DESCRIPTION:
# Install the extension into the libreoffice/openoffice.
office-ext-r1_add_extension() {
	debug-print-function ${FUNCNAME} "$@"
	local ext=$1
	local tmpdir=$(emktemp -d)

	debug-print "${FUNCNAME}: ${UNOPKG_BINARY} add --shared \"${ext}\""
	ebegin "Adding office extension: \"${ext}\""
	${UNOPKG_BINARY} add --suppress-license \
		--shared "${ext}" \
		"-env:UserInstallation=file:///${tmpdir}" \
		"-env:JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY=1"
	eend $?
	${UNOPKG_BINARY} list --shared > /dev/null
	rm -r "${tmpdir}" || dir "failed to clean up"
}

# @FUNCTION: office-ext-r1_remove_extension
# @DESCRIPTION:
# Remove the extension from the libreoffice/openoffice.
office-ext-r1_remove_extension() {
	debug-print-function ${FUNCNAME} "$@"
	local ext=$1
	local tmpdir=$(mktemp -d --tmpdir="${T}")

	debug-print "${FUNCNAME}: ${UNOPKG_BINARY} remove --shared \"${ext}\""
	ebegin "Removing office extension: \"${ext}\""
	${UNOPKG_BINARY} remove --suppress-license \
		--shared "${ext}" \
		"-env:UserInstallation=file:///${tmpdir}" \
		"-env:JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY=1"
	eend $?
	${UNOPKG_BINARY} list --shared > /dev/null
	rm -r "${tmpdir}" || dir "failed to clean up"
}

# @FUNCTION: office-ext-r1_pkg_postinst
# @DESCRIPTION:
# Add the extensions to the openoffice.
office-ext-r1_pkg_postinst() {
	if in_iuse office_implementation_openoffice && use office_implementation_openoffice; then
		debug-print-function ${FUNCNAME} "$@"
		debug-print "Extensions: ${OFFICE_EXTENSIONS[@]}"
		local i

		for i in ${OFFICE_EXTENSIONS[@]}; do
			office-ext-r1_add_extension "/usr/lib64/openoffice/share/extension/install/${i}"
		done
	fi
}

# @FUNCTION: office-ext-r1_pkg_prerm
# @DESCRIPTION:
# Remove the extensions from the openoffice.
office-ext-r1_pkg_prerm() {
	if in_iuse office_implementation_openoffice && use office_implementation_openoffice; then
		debug-print-function ${FUNCNAME} "$@"
		debug-print "Extensions: ${OFFICE_EXTENSIONS[@]}"
		local i

		for i in ${OFFICE_EXTENSIONS[@]}; do
			office-ext-r1_remove_extension "${i}"
		done
	fi
}

EXPORT_FUNCTIONS ${OEXT_EXPORTED_FUNCTIONS}
unset OEXT_EXPORTED_FUNCTIONS