kde5.eclass: Add KDE_PO_DIRS
[gentoo.git] / eclass / savedconfig.eclass
1 # Copyright 1999-2011 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: savedconfig.eclass
5 # @MAINTAINER:
6 # base-system@gentoo.org
7 # @BLURB: common API for saving/restoring complex configuration files
8 # @DESCRIPTION:
9 # It is not uncommon to come across a package which has a very fine
10 # grained level of configuration options that go way beyond what
11 # USE flags can properly describe.  For this purpose, a common API
12 # of saving and restoring the configuration files was developed
13 # so users can modify these config files and the ebuild will take it
14 # into account as needed.
15 #
16 # @ROFF .nr step 1 1
17 # Typically you can create your own configuration files quickly by
18 # doing:
19 # @ROFF .IP \n[step] 3
20 # Build the package with FEATURES=noclean USE=savedconfig.
21 # @ROFF .IP \n+[step]
22 # Go into the build dir and edit the relevant configuration system
23 # (e.g. `make menuconfig` or `nano config-header.h`).  You can look
24 # at the files in /etc/portage/savedconfig/ to see what files get
25 # loaded/restored.
26 # @ROFF .IP \n+[step]
27 # Copy the modified configuration files out of the workdir and to
28 # the paths in /etc/portage/savedconfig/.
29 # @ROFF .IP \n+[step]
30 # Emerge the package with just USE=savedconfig to get the custom build.
31
32 inherit portability
33
34 IUSE="savedconfig"
35
36 # @FUNCTION: save_config
37 # @USAGE: <config files to save>
38 # @DESCRIPTION:
39 # Use this function to save the package's configuration file into the
40 # right location.  You may specify any number of configuration files,
41 # but just make sure you call save_config with all of them at the same
42 # time in order for things to work properly.
43 save_config() {
44         if [[ ${EBUILD_PHASE} != "install" ]]; then
45                 die "Bad package!  save_config only for use in src_install functions!"
46         fi
47         [[ $# -eq 0 ]] && die "Usage: save_config <files>"
48
49         # Be lazy in our EAPI compat
50         : ${ED:=${D}}
51
52         local dest="/etc/portage/savedconfig/${CATEGORY}"
53         if [[ $# -eq 1 && -f $1 ]] ; then
54                 # Just one file, so have the ${PF} be that config file
55                 dodir "${dest}"
56                 cp "$@" "${ED}/${dest}/${PF}" || die "failed to save $*"
57         else
58                 # A dir, or multiple files, so have the ${PF} be a dir
59                 # with all the saved stuff below it
60                 dodir "${dest}/${PF}"
61                 treecopy "$@" "${ED}/${dest}/${PF}" || die "failed to save $*"
62         fi
63
64         elog "Your configuration for ${CATEGORY}/${PF} has been saved in "
65         elog "/etc/portage/savedconfig/${CATEGORY}/${PF} for your editing pleasure."
66         elog "You can edit these files by hand and remerge this package with"
67         elog "USE=savedconfig to customise the configuration."
68         elog "You can rename this file/directory to one of the following for"
69         elog "its configuration to apply to multiple versions:"
70         elog '${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/'
71         elog '[${CTARGET}|${CHOST}|""]/${CATEGORY}/[${PF}|${P}|${PN}]'
72 }
73
74 # @FUNCTION: restore_config
75 # @USAGE: <config files to restore>
76 # @DESCRIPTION:
77 # Restores the configuation saved ebuild previously potentially with user edits.
78 # You can restore a single file or a whole bunch, just make sure you call
79 # restore_config with all of the files to restore at the same time.
80 #
81 # Config files can be laid out as:
82 # @CODE
83 # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PF}
84 # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PF}
85 # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PF}
86 # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${P}
87 # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${P}
88 # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${P}
89 # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CTARGET}/${CATEGORY}/${PN}
90 # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CHOST}/${CATEGORY}/${PN}
91 # ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN}
92 # @CODE
93 restore_config() {
94         case ${EBUILD_PHASE} in
95                 unpack|compile|configure|prepare) ;;
96                 *) die "Bad package!  restore_config only for use in src_{unpack,compile,configure,prepare} functions!" ;;
97         esac
98
99         use savedconfig || return
100
101         local found check configfile
102         local base=${PORTAGE_CONFIGROOT}/etc/portage/savedconfig
103         for check in {${CATEGORY}/${PF},${CATEGORY}/${P},${CATEGORY}/${PN}}; do
104                 configfile=${base}/${CTARGET}/${check}
105                 [[ -r ${configfile} ]] || configfile=${base}/${CHOST}/${check}
106                 [[ -r ${configfile} ]] || configfile=${base}/${check}
107                 einfo "Checking existence of ${configfile} ..."
108                 if [[ -r "${configfile}" ]]; then
109                         einfo "found ${configfile}"
110                         found=${configfile};
111                         break;
112                 fi
113         done
114         if [[ -f ${found} ]]; then
115                 elog "Building using saved configfile ${found}"
116                 if [ $# -gt 0 ]; then
117                         cp -pPR "${found}" "$1" || die "Failed to restore ${found} to $1"
118                 else
119                         die "need to know the restoration filename"
120                 fi
121         elif [[ -d ${found} ]]; then
122                 elog "Building using saved config directory ${found}"
123                 local dest=${PWD}
124                 pushd "${found}" > /dev/null
125                 treecopy . "${dest}" || die "Failed to restore ${found} to $1"
126                 popd > /dev/null
127         else
128                 # maybe the user is screwing around with perms they shouldnt #289168
129                 if [[ ! -r ${base} ]] ; then
130                         eerror "Unable to read ${base} -- please check its permissions."
131                         die "Reading config files failed"
132                 fi
133                 ewarn "No saved config to restore - please remove USE=savedconfig or"
134                 ewarn "provide a configuration file in ${PORTAGE_CONFIGROOT}/etc/portage/savedconfig/${CATEGORY}/${PN}"
135                 ewarn "Your config file(s) will not be used this time"
136         fi
137 }
138
139 savedconfig_pkg_postinst() {
140         # If the user has USE=savedconfig, then chances are they
141         # are modifying these files, so keep them around.  #396169
142         # This might lead to cruft build up, but the alternatives
143         # are worse :/.
144
145         if use savedconfig ; then
146                 # Be lazy in our EAPI compat
147                 : ${EROOT:=${ROOT}}
148
149                 find "${EROOT}/etc/portage/savedconfig/${CATEGORY}/${PF}" \
150                         -exec touch {} + 2>/dev/null
151         fi
152 }
153
154 EXPORT_FUNCTIONS pkg_postinst