kde-apps/kdenlive: x86 stable wrt bug #679994
[gentoo.git] / eclass / alternatives.eclass
1 # Copyright 1999-2013 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: alternatives.eclass
5 # @AUTHOR:
6 # Original author: Alastair Tse <liquidx@gentoo.org> (03 Oct 2003)
7 # @BLURB: Creates symlink to the latest version of multiple slotted packages.
8 # @DESCRIPTION:
9 # When a package is SLOT'ed, very often we need to have a symlink to the
10 # latest version. However, depending on the order the user has merged them,
11 # more often than not, the symlink maybe clobbered by the older versions.
12 #
13 # This eclass provides a convenience function that needs to be given a
14 # list of alternatives (descending order of recent-ness) and the symlink.
15 # It will choose the latest version it can find installed and create
16 # the desired symlink.
17 #
18 # There are two ways to use this eclass. First is by declaring two variables
19 # $SOURCE and $ALTERNATIVES where $SOURCE is the symlink to be created and
20 # $ALTERNATIVES is a list of alternatives. Second way is the use the function
21 # alternatives_makesym() like the example below.
22 # @EXAMPLE:
23 # pkg_postinst() {
24 #     alternatives_makesym "/usr/bin/python" "/usr/bin/python2.3" "/usr/bin/python2.2"
25 # }
26 #
27 # The above example will create a symlink at /usr/bin/python to either
28 # /usr/bin/python2.3 or /usr/bin/python2.2. It will choose python2.3 over
29 # python2.2 if both exist.
30 #
31 # Alternatively, you can use this function:
32 #
33 # pkg_postinst() {
34 #    alternatives_auto_makesym "/usr/bin/python" "/usr/bin/python[0-9].[0-9]"
35 # }
36 #
37 # This will use bash pathname expansion to fill a list of alternatives it can
38 # link to. It is probably more robust against version upgrades. You should
39 # consider using this unless you are want to do something special.
40
41 # @ECLASS-VARIABLE: SOURCE
42 # @DEFAULT_UNSET
43 # @DESCRIPTION:
44 # The symlink to be created
45
46 # @ECLASS-VARIABLE: ALTERNATIVES
47 # @DEFAULT_UNSET
48 # @DESCRIPTION:
49 # The list of alternatives
50
51 # @FUNCTION: alternatives_auto_makesym
52 # @DESCRIPTION:
53 # automatic deduction based on a symlink and a regex mask
54 alternatives_auto_makesym() {
55         has "${EAPI:-0}" 0 1 2 && ! use prefix && EROOT="${ROOT}"
56         local SYMLINK REGEX ALT myregex
57         SYMLINK=$1
58         REGEX=$2
59         if [ "${REGEX:0:1}" != "/" ]
60         then
61                 #not an absolute path:
62                 #inherit the root directory of our main link path for our regex search
63                 myregex="${SYMLINK%/*}/${REGEX}"
64         else
65                 myregex=${REGEX}
66         fi
67
68         # sort a space delimited string by converting it to a multiline list
69         # and then run sort -r over it.
70         # make sure we use ${EROOT} because otherwise stage-building will break
71         ALT="$(for i in $(echo ${EROOT}${myregex}); do echo ${i#${EROOT}}; done | sort -r)"
72         alternatives_makesym ${SYMLINK} ${ALT}
73 }
74
75 alternatives_makesym() {
76         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
77         local ALTERNATIVES=""
78         local SYMLINK=""
79         local alt pref
80
81         # usage: alternatives_makesym <resulting symlink> [alternative targets..]
82         # make sure it is in the prefix, allow it already to be in the prefix
83         SYMLINK=${EPREFIX}/${1#${EPREFIX}}
84         # this trick removes the trailing / from ${ROOT}
85         pref=${ROOT%/}
86         shift
87         ALTERNATIVES=$@
88
89         # step through given alternatives from first to last
90         # and if one exists, link it and finish.
91
92         for alt in ${ALTERNATIVES}; do
93                 alt=${EPREFIX}/${alt#${EPREFIX}}
94                 if [ -f "${pref}${alt}" ]; then
95                         #are files in same directory?
96                         if [ "${alt%/*}" = "${SYMLINK%/*}" ]
97                         then
98                                 #yes; strip leading dirname from alt to create relative symlink
99                                 einfo "Linking ${alt} to ${pref}${SYMLINK} (relative)"
100                                 ln -sf ${alt##*/} ${pref}${SYMLINK}
101                         else
102                                 #no; keep absolute path
103                                 einfo "Linking ${alt} to ${pref}${SYMLINK} (absolute)"
104                                 ln -sf ${pref}${alt} ${pref}${SYMLINK}
105                         fi
106                         break
107                 fi
108         done
109
110         # report any errors
111         if [ ! -L ${pref}${SYMLINK} ]; then
112                 ewarn "Unable to establish ${pref}${SYMLINK} symlink"
113         else
114                 # we need to check for either the target being in relative path form
115                 # or absolute path form
116                 if [ ! -f "`dirname ${pref}${SYMLINK}`/`readlink ${pref}${SYMLINK}`" -a \
117                          ! -f "`readlink ${pref}${SYMLINK}`" ]; then
118                         ewarn "Removing dead symlink ${pref}${SYMLINK}"
119                         rm -f ${pref}${SYMLINK}
120                 fi
121         fi
122 }
123
124 # @FUNCTION: alernatives-pkg_postinst
125 # @DESCRIPTION:
126 # The alternatives pkg_postinst, this function will be exported
127 alternatives_pkg_postinst() {
128         if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then
129                 alternatives_makesym ${SOURCE} ${ALTERNATIVES}
130         fi
131 }
132
133 # @FUNCTION: alternatives_pkg_postrm
134 # @DESCRIPTION:
135 # The alternatives pkg_postrm, this function will be exported
136 alternatives_pkg_postrm() {
137         if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then
138                 alternatives_makesym ${SOURCE} ${ALTERNATIVES}
139         fi
140 }
141
142 EXPORT_FUNCTIONS pkg_postinst pkg_postrm