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