net-irc/limnoria: use HTTPS for GitHub and HOMEPAGE
[gentoo.git] / eclass / myspell.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # Author: Kevin F. Quinn <kevquinn@gentoo.org>
5 # Packages: app-dicts/myspell-*
6 # Maintainer: maintainer-needed@gentoo.org
7
8 inherit multilib
9
10 EXPORT_FUNCTIONS src_install pkg_preinst pkg_postinst
11
12 IUSE=""
13
14 SLOT="0"
15
16 # tar, gzip, bzip2 are included in the base profile, but not unzip
17 DEPEND="app-arch/unzip"
18
19 # Dictionaries don't have any runtime dependencies
20 # Myspell dictionaries can be used by hunspell, openoffice and others
21 RDEPEND=""
22
23 # The destination directory for myspell dictionaries
24 MYSPELL_DICTBASE="/usr/share/myspell"
25
26 # Legacy variable for dictionaries installed before eselect-oodict existed
27 # so has to remain for binpkg support.  This variable is unmaintained -
28 # if you have a problem with it, emerge app-eselect/eselect-oodict.
29 # The location for openoffice softlinks
30 MYSPELL_OOOBASE="/usr/lib/openoffice/share/dict/ooo"
31
32
33 # set array "fields" to the elements of $1, separated by $2.
34 # This saves having to muck about with IFS all over the place.
35 set_fields() {
36         local old_IFS
37         old_IFS="${IFS}"
38         IFS=$2
39         fields=($1)
40         IFS="${old_IFS}"
41 }
42
43 # language is the second element of the ebuild name
44 # myspell-<lang>-<version>
45 get_myspell_lang() {
46         local fields
47         set_fields "${P}" "-"
48         echo ${fields[1]}
49 }
50
51 get_myspell_suffixes() {
52         case $1 in
53                 DICT) echo ".aff .dic" ;;
54                 HYPH) echo ".dic" ;;
55                 THES) echo ".dat .idx" ;;
56         esac
57 }
58
59 # OOo dictionary files are held on the mirrors, rather than
60 # being fetched direct from the OOo site as upstream doesn't
61 # change the name when they rebuild the dictionaries.
62 # <lang>-<country>.zip becomes myspell-<lang>-<country>-version.zip
63 get_myspell_ooo_uri() {
64         local files fields newfile filestem srcfile dict uris
65         files=()
66         uris=""
67         for dict in \
68                         "${MYSPELL_SPELLING_DICTIONARIES[@]}" \
69                         "${MYSPELL_HYPHENATION_DICTIONARIES[@]}" \
70                         "${MYSPELL_THESAURUS_DICTIONARIES[@]}"; do
71                 set_fields "${dict}" ","
72                 newfile=${fields[4]// }
73                 for file in "${files[@]}"; do
74                         [[ ${file} == ${newfile} ]] && continue 2
75                 done
76                 filestem=${newfile/.zip}
77                 files=("${files[@]}" "${newfile}")
78                 srcfile="myspell-${filestem}-${PV}.zip"
79                 [[ -z ${uris} ]] &&
80                         uris="mirror://gentoo/${srcfile}" ||
81                         uris="${uris} mirror://gentoo/${srcfile}"
82         done
83         echo "${uris}"
84 }
85
86
87 [[ -z ${SRC_URI} ]] && SRC_URI=$(get_myspell_ooo_uri)
88
89 # Format of dictionary.lst files (from OOo standard
90 # dictionary.lst file):
91 #
92 # List of All Dictionaries to be Loaded by OpenOffice
93 # ---------------------------------------------------
94 # Each Entry in the list have the following space delimited fields
95 #
96 # Field 0: Entry Type "DICT" - spellchecking dictionary
97 #                     "HYPH" - hyphenation dictionary
98 #                     "THES" - thesaurus files
99 #
100 # Field 1: Language code from Locale "en" or "de" or "pt" ...
101 #
102 # Field 2: Country Code from Locale "US" or "GB" or "PT"
103 #
104 # Field 3: Root name of file(s) "en_US" or "hyph_de" or "th_en_US"
105 #          (do not add extensions to the name)
106
107 # Format of MYSPELL_[SPELLING|HYPHENATION|THESAURUS]_DICTIONARIES:
108 #
109 # Field 0: Language code
110 # Field 1: Country code
111 # Field 2: Root name of dictionary files
112 # Field 3: Description
113 # Field 4: Archive filename
114 #
115 # This format is from the available.lst, hyphavail.lst and
116 # thesavail.lst files on the openoffice.org repository.
117
118 myspell_src_install() {
119         local filen fields entry dictlst
120         cd "${WORKDIR}"
121         # Install the dictionary, hyphenation and thesaurus files.
122         # Create dictionary.lst.<lang> file containing the parts of
123         # OOo's dictionary.lst file for this language, indicating
124         # which dictionaries are relevant for each country variant
125         # of the language.
126         insinto ${MYSPELL_DICTBASE}
127         dictlst="dictionary.lst.$(get_myspell_lang)"
128         echo "# Autogenerated by ${CATEGORY}/${P}" > ${dictlst}
129         for entry in "${MYSPELL_SPELLING_DICTIONARIES[@]}"; do
130                 set_fields "${entry}" ","
131                 echo "DICT ${fields[0]} ${fields[1]} ${fields[2]}" >> ${dictlst}
132                 doins ${fields[2]}.aff || die "Missing ${fields[2]}.aff"
133                 doins ${fields[2]}.dic || die "Missing ${fields[2]}.dic"
134         done
135         for entry in "${MYSPELL_HYPHENATION_DICTIONARIES[@]}"; do
136                 set_fields "${entry}" ","
137                 echo "HYPH ${fields[0]} ${fields[1]} ${fields[2]}" >> ${dictlst}
138                 doins ${fields[2]}.dic || die "Missing ${fields[2]}.dic"
139         done
140         for entry in "${MYSPELL_THESAURUS_DICTIONARIES[@]}"; do
141                 set_fields "${entry}" ","
142                 echo "THES ${fields[0]} ${fields[1]} ${fields[2]}" >> ${dictlst}
143                 doins ${fields[2]}.dat || die "Missing ${fields[2]}.dat"
144                 doins ${fields[2]}.idx || die "Missing ${fields[2]}.idx"
145         done
146         doins ${dictlst} || die "Failed to install ${dictlst}"
147         # Install any txt files (usually README.txt) as documentation
148         for filen in *.txt; do
149                 [[ -s ${filen} ]] && dodoc ${filen}
150         done
151 }
152
153
154 # Add entries in dictionary.lst.<lang> to OOo dictionary.lst
155 # and create softlinks indicated by dictionary.lst.<lang>
156 myspell_pkg_postinst() {
157         # Update for known applications
158         if has_version ">=app-eselect/eselect-oodict-20060706"; then
159                 if has_version app-office/openoffice; then
160                         eselect oodict set myspell-$(get_myspell_lang)
161                 fi
162                 if has_version app-office/openoffice-bin; then
163                         # On AMD64, openoffice-bin is 32-bit so force ABI
164                         has_multilib_profile && ABI=x86
165                         eselect oodict set myspell-$(get_myspell_lang) --libdir $(get_libdir)
166                 fi
167                 return
168         fi
169         if has_version app-eselect/eselect-oodict; then
170                 eselect oodict set myspell-$(get_myspell_lang)
171                 return
172         fi
173
174         # Legacy code for dictionaries installed before eselect-oodict existed
175         # so has to remain for binpkg support.  This code is unmaintained -
176         # if you have a problem with it, emerge app-eselect/eselect-oodict.
177         [[ -d ${MYSPELL_OOOBASE} ]] || return
178         # This stuff is here, not in src_install, as the softlinks are
179         # deliberately _not_ listed in the package database.
180         local dictlst entry fields prefix suffix suffixes filen
181         # Note; can only reach this point if ${MYSPELL_DICTBASE}/${dictlst}
182         # was successfully installed
183         dictlst="dictionary.lst.$(get_myspell_lang)"
184         while read entry; do
185                 fields=(${entry})
186                 [[ ${fields[0]:0:1} == "#" ]] && continue
187                 [[ -f ${MYSPELL_OOOBASE}/dictionary.lst ]] || \
188                         touch ${MYSPELL_OOOBASE}/dictionary.lst
189                 grep "^${fields[0]} ${fields[1]} ${fields[2]} " \
190                         ${MYSPELL_OOOBASE}/dictionary.lst > /dev/null 2>&1 ||
191                                 echo "${entry}" >> ${MYSPELL_OOOBASE}/dictionary.lst
192                 for suffix in $(get_myspell_suffixes ${fields[0]}); do
193                         filen="${fields[3]}${suffix}"
194                         [[ -h ${MYSPELL_OOOBASE}/${filen} ]] &&
195                                 rm -f ${MYSPELL_OOOBASE}/${filen}
196                         [[ ! -f ${MYSPELL_OOOBASE}/${filen} ]] &&
197                                 ln -s ${MYSPELL_DICTBASE}/${filen} \
198                                         ${MYSPELL_OOOBASE}/${filen}
199                 done
200         done < ${MYSPELL_DICTBASE}/${dictlst}
201 }
202
203
204 # Remove softlinks and entries in dictionary.lst - uses
205 # dictionary.<lang>.lst from /usr/share/myspell
206 # Done in preinst (prerm happens after postinst, which overwrites
207 # the dictionary.<lang>.lst file)
208 myspell_pkg_preinst() {
209         # Update for known applications
210         if has_version ">=app-eselect/eselect-oodict-20060706"; then
211                 if has_version app-office/openoffice; then
212                         # When building from source, the default library path is correct
213                         eselect oodict unset myspell-$(get_myspell_lang)
214                 fi
215                 if has_version app-office/openoffice-bin; then
216                         # On AMD64, openoffice-bin is 32-bit, so get 32-bit library directory
217                         has_multilib_profile && ABI=x86
218                         eselect oodict unset myspell-$(get_myspell_lang) --libdir $(get_libdir)
219                 fi
220                 eselect oodict unset myspell-$(get_myspell_lang) --libdir $(get_libdir)
221                 return
222         fi
223         # Previous versions of eselect-oodict didn't cater for -bin on amd64
224         if has_version app-eselect/eselect-oodict; then
225                 eselect oodict unset myspell-$(get_myspell_lang)
226                 return
227         fi
228
229         # Legacy code for dictionaries installed before eselect-oodict existed
230         # Don't delete this; needed for uninstalls and binpkg support.
231         # This code is unmaintained - if you have a problem with it,
232         # emerge app-eselect/eselect-oodict.
233         local filen dictlst entry fields removeentry suffix
234         dictlst="dictionary.lst.$(get_myspell_lang)"
235         [[ -d ${MYSPELL_OOOBASE} ]] || return
236         [[ -f ${MYSPELL_DICTBASE}/${dictlst} ]] || return
237         while read entry; do
238                 fields=(${entry})
239                 [[ ${fields[0]:0:1} == "#" ]] && continue
240                 [[ ${fields[3]} == "" ]] && continue
241                 # Remove entry from dictionary.lst
242                 sed -i -e "/^${fields[0]} ${fields[1]} ${fields[2]} ${fields[3]}$/ { d }" \
243                         ${MYSPELL_OOOBASE}/dictionary.lst
244                 # See if any other entries in dictionary.lst match the current
245                 # dictionary type and filename
246                 grep "^${fields[0]} .* ${fields[3]}$" ${MYSPELL_OOOBASE}/dictionary.lst \
247                         2>&1 > /dev/null && continue
248                 # If no other entries match, remove relevant symlinks
249                 for suffix in $(get_myspell_suffixes ${fields[0]}); do
250                         filen="${fields[3]}${suffix}"
251                         ewarn "Removing entry ${MYSPELL_OOOBASE}/${filen}"
252                         [[ -h ${MYSPELL_OOOBASE}/${filen} ]] &&
253                                 rm -f ${MYSPELL_OOOBASE}/${filen}
254                 done
255         done < ${MYSPELL_DICTBASE}/${dictlst}
256 }