app-portage/mgorny-dev-scripts: Bump to v3
[gentoo.git] / eclass / office-ext-r1.eclass
1 # Copyright 1999-2019 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: office-ext-r1.eclass
5 # @MAINTAINER:
6 # The office team <office@gentoo.org>
7 # @AUTHOR:
8 # Tomáš Chvátal <scarabeus@gentoo.org>
9 # @SUPPORTED_EAPIS: 5 6 7
10 # @BLURB: Eclass for installing libreoffice/openoffice extensions
11 # @DESCRIPTION:
12 # Eclass for easing maintenance of libreoffice/openoffice extensions.
13
14 case "${EAPI:-0}" in
15         5|6) inherit eutils multilib ;;
16         7) ;;
17         *) die "EAPI=${EAPI} is not supported" ;;
18 esac
19
20 OEXT_EXPORTED_FUNCTIONS="src_unpack src_install pkg_postinst pkg_prerm"
21
22 # @ECLASS-VARIABLE: OFFICE_REQ_USE
23 # @DESCRIPTION:
24 # Useflags required on office implementation for the extension.
25 #
26 # Example:
27 # @CODE
28 # OFFICE_REQ_USE="java,jemalloc(-)?"
29 # @CODE
30 if [[ ${OFFICE_REQ_USE} ]]; then
31         # Append the brackets for the depend bellow
32         OFFICE_REQ_USE="[${OFFICE_REQ_USE}]"
33 fi
34
35 # @ECLASS-VARIABLE: OFFICE_IMPLEMENTATIONS
36 # @DESCRIPTION:
37 # List of implementations supported by the extension.
38 # Some work only for libreoffice and vice versa.
39 # Default value is all implementations.
40 #
41 # Example:
42 # @CODE
43 # OFFICE_IMPLEMENTATIONS=( "libreoffice" "openoffice" )
44 # @CODE
45 [[ -z ${OFFICE_IMPLEMENTATIONS} ]] && OFFICE_IMPLEMENTATIONS=( "libreoffice" "openoffice" )
46
47 # @ECLASS-VARIABLE: OFFICE_EXTENSIONS
48 # @REQUIRED
49 # @DESCRIPTION:
50 # Array containing list of extensions to install.
51 #
52 # Example:
53 # @CODE
54 # OFFICE_EXTENSIONS=( ${PN}_${PV}.oxt )
55 # @CODE
56 [[ -z ${OFFICE_EXTENSIONS} ]] && die "OFFICE_EXTENSIONS variable is unset."
57 if [[ "$(declare -p OFFICE_EXTENSIONS 2>/dev/null 2>&1)" != "declare -a"* ]]; then
58         die "OFFICE_EXTENSIONS variable is not an array."
59 fi
60
61 # @ECLASS-VARIABLE: OFFICE_EXTENSIONS_LOCATION
62 # @DESCRIPTION:
63 # Path to the extensions location. Defaults to ${DISTDIR}.
64 #
65 # Example:
66 # @CODE
67 # OFFICE_EXTENSIONS_LOCATION="${S}/unpacked/"
68 # @CODE
69 : ${OFFICE_EXTENSIONS_LOCATION:=${DISTDIR}}
70
71 IUSE=""
72 RDEPEND=""
73
74 for i in ${OFFICE_IMPLEMENTATIONS[@]}; do
75         IUSE+=" office_implementation_${i}"
76         if [[ ${i} == "openoffice" ]]; then
77                 # special only binary
78                 RDEPEND+="
79                         office_implementation_openoffice? (
80                                 app-office/openoffice-bin${OFFICE_REQ_USE}
81                         )
82                 "
83         else
84                 RDEPEND+="
85                         office_implementation_${i}? (
86                                 || (
87                                         app-office/${i}${OFFICE_REQ_USE}
88                                         app-office/${i}-bin${OFFICE_REQ_USE}
89                                 )
90                         )
91                 "
92         fi
93 done
94
95 REQUIRED_USE="|| ( "
96 for i in ${OFFICE_IMPLEMENTATIONS[@]}; do
97         REQUIRED_USE+=" office_implementation_${i} "
98 done
99 REQUIRED_USE+=" )"
100
101 DEPEND="${RDEPEND}
102         app-arch/unzip
103 "
104
105 # Most projects actually do not provide any relevant sourcedir as they are oxt.
106 S="${WORKDIR}"
107
108 # @FUNCTION: office-ext-r1_src_unpack
109 # @DESCRIPTION:
110 # Flush the cache after removal of an extension.
111 office-ext-r1_src_unpack() {
112         debug-print-function ${FUNCNAME} "$@"
113         local i
114
115         default
116
117         for i in ${OFFICE_EXTENSIONS[@]}; do
118                 # Unpack the extensions where required and add case for oxt
119                 # which should be most common case for the extensions.
120                 if [[ -f "${OFFICE_EXTENSIONS_LOCATION}/${i}" ]] ; then
121                         case ${i} in
122                                 *.oxt)
123                                         mkdir -p "${WORKDIR}/${i}/"
124                                         pushd "${WORKDIR}/${i}/" > /dev/null
125                                         echo ">>> Unpacking "${OFFICE_EXTENSIONS_LOCATION}/${i}" to ${PWD}"
126                                         unzip -qo ${OFFICE_EXTENSIONS_LOCATION}/${i}
127                                         assert "failed unpacking ${OFFICE_EXTENSIONS_LOCATION}/${i}"
128                                         popd > /dev/null
129                                         ;;
130                                 *) unpack ${i} ;;
131                         esac
132                 fi
133         done
134 }
135
136 # @FUNCTION: office-ext-r1_src_install
137 # @DESCRIPTION:
138 # Install the extension source to the proper location.
139 office-ext-r1_src_install() {
140         debug-print-function ${FUNCNAME} "$@"
141         debug-print "Extensions: ${OFFICE_EXTENSIONS[@]}"
142
143         local i j
144
145         for i in ${OFFICE_IMPLEMENTATIONS[@]}; do
146                 if use office_implementation_${i}; then
147                         if [[ ${i} == openoffice ]]; then
148                                 # OOO needs to use uno because direct deployment segfaults.
149                                 # This is bug by their side, but i don't want to waste time
150                                 # fixing it myself.
151                                 insinto /usr/$(get_libdir)/${i}/share/extension/install
152                                 for j in ${OFFICE_EXTENSIONS[@]}; do
153                                         doins ${OFFICE_EXTENSIONS_LOCATION}/${j}
154                                 done
155                         else
156                                 for j in ${OFFICE_EXTENSIONS[@]}; do
157                                         pushd "${WORKDIR}/${j}/" > /dev/null
158                                         insinto /usr/$(get_libdir)/${i}/share/extensions/${j/.oxt/}
159                                         doins -r *
160                                         popd > /dev/null
161                                 done
162                         fi
163                 fi
164         done
165 }
166
167 #### OPENOFFICE COMPAT CODE
168
169 UNOPKG_BINARY="/usr/lib64/openoffice/program/unopkg"
170
171 # @FUNCTION: office-ext-r1_add_extension
172 # @DESCRIPTION:
173 # Install the extension into the libreoffice/openoffice.
174 office-ext-r1_add_extension() {
175         debug-print-function ${FUNCNAME} "$@"
176         local ext=$1
177         local tmpdir=$(emktemp -d)
178
179         debug-print "${FUNCNAME}: ${UNOPKG_BINARY} add --shared \"${ext}\""
180         ebegin "Adding office extension: \"${ext}\""
181         ${UNOPKG_BINARY} add --suppress-license \
182                 --shared "${ext}" \
183                 "-env:UserInstallation=file:///${tmpdir}" \
184                 "-env:JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY=1"
185         eend $?
186         ${UNOPKG_BINARY} list --shared > /dev/null
187         rm -r "${tmpdir}" || dir "failed to clean up"
188 }
189
190 # @FUNCTION: office-ext-r1_remove_extension
191 # @DESCRIPTION:
192 # Remove the extension from the libreoffice/openoffice.
193 office-ext-r1_remove_extension() {
194         debug-print-function ${FUNCNAME} "$@"
195         local ext=$1
196         local tmpdir=$(mktemp -d --tmpdir="${T}")
197
198         debug-print "${FUNCNAME}: ${UNOPKG_BINARY} remove --shared \"${ext}\""
199         ebegin "Removing office extension: \"${ext}\""
200         ${UNOPKG_BINARY} remove --suppress-license \
201                 --shared "${ext}" \
202                 "-env:UserInstallation=file:///${tmpdir}" \
203                 "-env:JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY=1"
204         eend $?
205         ${UNOPKG_BINARY} list --shared > /dev/null
206         rm -r "${tmpdir}" || dir "failed to clean up"
207 }
208
209 # @FUNCTION: office-ext-r1_pkg_postinst
210 # @DESCRIPTION:
211 # Add the extensions to the openoffice.
212 office-ext-r1_pkg_postinst() {
213         if in_iuse office_implementation_openoffice && use office_implementation_openoffice; then
214                 debug-print-function ${FUNCNAME} "$@"
215                 debug-print "Extensions: ${OFFICE_EXTENSIONS[@]}"
216                 local i
217
218                 for i in ${OFFICE_EXTENSIONS[@]}; do
219                         office-ext-r1_add_extension "/usr/lib64/openoffice/share/extension/install/${i}"
220                 done
221         fi
222 }
223
224 # @FUNCTION: office-ext-r1_pkg_prerm
225 # @DESCRIPTION:
226 # Remove the extensions from the openoffice.
227 office-ext-r1_pkg_prerm() {
228         if in_iuse office_implementation_openoffice && use office_implementation_openoffice; then
229                 debug-print-function ${FUNCNAME} "$@"
230                 debug-print "Extensions: ${OFFICE_EXTENSIONS[@]}"
231                 local i
232
233                 for i in ${OFFICE_EXTENSIONS[@]}; do
234                         office-ext-r1_remove_extension "${i}"
235                 done
236         fi
237 }
238
239 EXPORT_FUNCTIONS ${OEXT_EXPORTED_FUNCTIONS}
240 unset OEXT_EXPORTED_FUNCTIONS