dev-db/rqlite: remove old versions
[gentoo.git] / dev-db / postgresql / postgresql-9.2.21.ebuild
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI="5"
5
6 PYTHON_COMPAT=( python{2_7,3_4} )
7
8 inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
9                 systemd user versionator
10
11 KEYWORDS="alpha amd64 arm ~hppa ia64 ~mips ppc ppc64 ~s390 ~sh sparc x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
12
13 SLOT="$(get_version_component_range 1-2)"
14
15 SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
16
17 LICENSE="POSTGRESQL GPL-2"
18 DESCRIPTION="PostgreSQL RDBMS"
19 HOMEPAGE="http://www.postgresql.org/"
20
21 LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
22                  zh_CN zh_TW"
23 IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp python
24           +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
25
26 for lingua in ${LINGUAS}; do
27         IUSE+=" linguas_${lingua}"
28 done
29 REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
30
31 wanted_languages() {
32         local enable_langs
33
34         for lingua in ${LINGUAS} ; do
35                 use linguas_${lingua} && enable_langs+="${lingua} "
36         done
37
38         echo -n ${enable_langs}
39 }
40
41 CDEPEND="
42 >=app-eselect/eselect-postgresql-2.0
43 sys-apps/less
44 virtual/libintl
45 kerberos? ( virtual/krb5 )
46 ldap? ( net-nds/openldap )
47 pam? ( virtual/pam )
48 perl? ( >=dev-lang/perl-5.8:= )
49 python? ( ${PYTHON_DEPS} )
50 readline? ( sys-libs/readline:0= )
51 ssl? (
52         !libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
53         libressl? ( dev-libs/libressl:= )
54 )
55 tcl? ( >=dev-lang/tcl-8:0= )
56 uuid? ( dev-libs/ossp-uuid )
57 xml? ( dev-libs/libxml2 dev-libs/libxslt )
58 zlib? ( sys-libs/zlib )
59 "
60
61 DEPEND="${CDEPEND}
62 !!<sys-apps/sandbox-2.0
63 sys-devel/bison
64 sys-devel/flex
65 nls? ( sys-devel/gettext )
66 xml? ( virtual/pkgconfig )
67 "
68
69 RDEPEND="${CDEPEND}
70 !dev-db/postgresql-docs:${SLOT}
71 !dev-db/postgresql-base:${SLOT}
72 !dev-db/postgresql-server:${SLOT}
73 selinux? ( sec-policy/selinux-postgresql )
74 "
75
76 pkg_setup() {
77         use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
78
79         enewgroup postgres 70
80         enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
81
82         use python && python-single-r1_pkg_setup
83 }
84
85 src_prepare() {
86         # Work around PPC{,64} compilation bug where bool is already defined
87         sed '/#ifndef __cplusplus/a #undef bool' -i src/include/c.h || die
88
89         # Set proper run directory
90         sed "s|\(PGSOCKET_DIR\s\+\)\"/tmp\"|\1\"${EPREFIX}/run/postgresql\"|" \
91                 -i src/include/pg_config_manual.h || die
92
93         use server || epatch "${FILESDIR}/${PN}-${SLOT}-no-server.patch"
94
95         # Fix bug 486556 where the server would crash at start up because of
96         # an infinite loop caused by a self-referencing symlink.
97         epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
98
99         if use pam ; then
100                 sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
101                         -i src/backend/libpq/auth.c || \
102                         die 'PGSQL_PAM_SERVICE rename failed.'
103         fi
104
105         epatch_user
106 }
107
108 src_configure() {
109         case ${CHOST} in
110                 *-darwin*|*-solaris*)
111                         use nls && append-libs intl
112                         ;;
113         esac
114
115         export LDFLAGS_SL="${LDFLAGS}"
116         export LDFLAGS_EX="${LDFLAGS}"
117
118         local PO="${EPREFIX%/}"
119
120         econf \
121                 --prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
122                 --datadir="${PO}/usr/share/postgresql-${SLOT}" \
123                 --docdir="${PO}/usr/share/doc/${PF}" \
124                 --includedir="${PO}/usr/include/postgresql-${SLOT}" \
125                 --mandir="${PO}/usr/share/postgresql-${SLOT}/man" \
126                 --sysconfdir="${PO}/etc/postgresql-${SLOT}" \
127                 --with-system-tzdata="${PO}/usr/share/zoneinfo" \
128                 $(use_enable !pg_legacytimestamp integer-datetimes) \
129                 $(use_enable threads thread-safety) \
130                 $(use_with kerberos gssapi) \
131                 $(use_with kerberos krb5) \
132                 $(use_with ldap) \
133                 $(use_with pam) \
134                 $(use_with perl) \
135                 $(use_with python) \
136                 $(use_with readline) \
137                 $(use_with ssl openssl) \
138                 $(use_with tcl) \
139                 $(use_with uuid ossp-uuid) \
140                 $(use_with xml libxml) \
141                 $(use_with xml libxslt) \
142                 $(use_with zlib) \
143                 "$(use_enable nls nls "$(wanted_languages)")"
144 }
145
146 src_compile() {
147         emake
148         emake -C contrib
149 }
150
151 src_install() {
152         emake DESTDIR="${D}" install
153         emake DESTDIR="${D}" install -C contrib
154
155         dodoc README HISTORY doc/{TODO,bug.template}
156
157         # man pages are already built, but if we have the target make them,
158         # they'll be generated from source before being installed so we
159         # manually install man pages.
160         # We use ${SLOT} instead of doman for postgresql.eselect
161         insinto /usr/share/postgresql-${SLOT}/man/
162         doins -r doc/src/sgml/man{1,3,7}
163         if ! use server; then
164                 # Remove man pages for non-existent binaries
165                 for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
166                         rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
167                 done
168         fi
169         docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
170
171         insinto /etc/postgresql-${SLOT}
172         newins src/bin/psql/psqlrc.sample psqlrc
173
174         use static-libs || find "${ED}" -name '*.a' -delete
175
176         local f bn
177         for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
178                                         -mindepth 1 -maxdepth 1)
179         do
180                 bn=$(basename "${f}")
181                 # Temporarily tack on tmp to workaround a file collision
182                 # issue. This is only necessary for 9.7 and earlier. 10 never
183                 # had this issue.
184                 dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
185                           "/usr/bin/${bn}${SLOT/.}tmp"
186         done
187
188         local linkname mansec
189         for mansec in {1,3,7} ; do
190                 for f in "${ED}"/usr/share/postgresql-${SLOT}/man/man${mansec}/* ; do
191                         bn=$(basename "${f}")
192                         linkname=${bn/%.${mansec}/${SLOT/.}.${mansec}}
193                         dosym ../../postgresql-${SLOT}/man/man${mansec}/$bn \
194                                   /usr/share/man/man${mansec}/${linkname}
195                 done
196         done
197
198         if use doc ; then
199                 docinto html
200                 dodoc doc/src/sgml/html/*
201
202                 docinto sgml
203                 dodoc doc/src/sgml/*.{sgml,dsl}
204         fi
205
206         if use server; then
207                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
208                         "${FILESDIR}/${PN}.confd-9.2" | newconfd - ${PN}-${SLOT}
209
210                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
211                         "${FILESDIR}/${PN}.init-9.2" | newinitd - ${PN}-${SLOT}
212
213                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
214                         "${FILESDIR}/${PN}.service" | \
215                         systemd_newunit - ${PN}-${SLOT}.service
216
217                 newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
218
219                 use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
220
221                 if use prefix ; then
222                         keepdir /run/postgresql
223                         fperms 0775 /run/postgresql
224                 fi
225         fi
226 }
227
228 pkg_preinst() {
229         # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
230         # /usr/bin/psql96). They may have been created by the
231         # postgresql.eselect module, but they're handled within this ebuild
232         # now. It's alright if we momentarily delete /usr/bin/psql as it
233         # will be recreated by the eselect module in pkg_ppostinst(). This
234         # is only necessary for 9.7 and earlier. 10 and later were never
235         # handled in this manner.
236         local canonicalise
237         if type -p realpath > /dev/null; then
238                 canonicalise=realpath
239         elif type -p readlink > /dev/null; then
240                 canonicalise='readlink -f'
241         else
242                 # can't die, subshell
243                 die "No readlink nor realpath found, cannot canonicalise"
244         fi
245
246         local l
247         # First remove any symlinks in /usr/bin that may have been created
248         # by the old eselect
249         for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; do
250                 if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; then
251                         rm "${l}" || ewarn "Couldn't remove ${l}"
252                 fi
253         done
254
255         # Then move the symlinks created by the ebuild to their proper place.
256         for l in "${ED}"/usr/bin/*tmp ; do
257                 mv "${l}" "${l%tmp}" \
258                         || ewarn "Couldn't rename $(basename ${l}) to $(basename ${l%tmp})"
259         done
260 }
261
262 pkg_postinst() {
263         postgresql-config update
264
265         elog "If you need a global psqlrc-file, you can place it in:"
266         elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
267
268         if use server ; then
269                 elog
270                 elog "Gentoo specific documentation:"
271                 elog "https://wiki.gentoo.org/wiki/PostgreSQL"
272                 elog
273                 elog "Official documentation:"
274                 elog "http://www.postgresql.org/docs/${SLOT}/static/index.html"
275                 elog
276                 elog "The default location of the Unix-domain socket is:"
277                 elog "    ${EROOT%/}/run/postgresql/"
278                 elog
279                 elog "Before initializing the database, you may want to edit PG_INITDB_OPTS"
280                 elog "so that it contains your preferred locale in:"
281                 elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
282                 elog
283                 elog "Then, execute the following command to setup the initial database"
284                 elog "environment:"
285                 elog "    emerge --config =${CATEGORY}/${PF}"
286         fi
287 }
288
289 pkg_prerm() {
290         if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
291                 ewarn "Have you dumped and/or migrated the ${SLOT} database cluster?"
292                 ewarn "\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL"
293
294                 ebegin "Resuming removal in 10 seconds (Control-C to cancel)"
295                 sleep 10
296                 eend 0
297         fi
298 }
299
300 pkg_postrm() {
301         postgresql-config update
302 }
303
304 pkg_config() {
305         use server || die "USE flag 'server' not enabled. Nothing to configure."
306
307         [[ -f "${EROOT%/}/etc/conf.d/postgresql-${SLOT}" ]] \
308                 && source "${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
309         [[ -z "${PGDATA}" ]] && PGDATA="${EROOT%/}/etc/postgresql-${SLOT}/"
310         [[ -z "${DATA_DIR}" ]] \
311                 && DATA_DIR="${EROOT%/}/var/lib/postgresql/${SLOT}/data"
312
313         # environment.bz2 may not contain the same locale as the current system
314         # locale. Unset and source from the current system locale.
315         if [ -f "${EROOT%/}/etc/env.d/02locale" ]; then
316                 unset LANG
317                 unset LC_CTYPE
318                 unset LC_NUMERIC
319                 unset LC_TIME
320                 unset LC_COLLATE
321                 unset LC_MONETARY
322                 unset LC_MESSAGES
323                 unset LC_ALL
324                 source "${EROOT%/}/etc/env.d/02locale"
325                 [ -n "${LANG}" ] && export LANG
326                 [ -n "${LC_CTYPE}" ] && export LC_CTYPE
327                 [ -n "${LC_NUMERIC}" ] && export LC_NUMERIC
328                 [ -n "${LC_TIME}" ] && export LC_TIME
329                 [ -n "${LC_COLLATE}" ] && export LC_COLLATE
330                 [ -n "${LC_MONETARY}" ] && export LC_MONETARY
331                 [ -n "${LC_MESSAGES}" ] && export LC_MESSAGES
332                 [ -n "${LC_ALL}" ] && export LC_ALL
333         fi
334
335         einfo "You can modify the paths and options passed to initdb by editing:"
336         einfo "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
337         einfo
338         einfo "Information on options that can be passed to initdb are found at:"
339         einfo "    http://www.postgresql.org/docs/${SLOT}/static/creating-cluster.html"
340         einfo "    http://www.postgresql.org/docs/${SLOT}/static/app-initdb.html"
341         einfo
342         einfo "PG_INITDB_OPTS is currently set to:"
343         if [[ -z "${PG_INITDB_OPTS}" ]] ; then
344                 einfo "    (none)"
345         else
346                 einfo "    ${PG_INITDB_OPTS}"
347         fi
348         einfo
349         einfo "Configuration files will be installed to:"
350         einfo "    ${PGDATA}"
351         einfo
352         einfo "The database cluster will be created in:"
353         einfo "    ${DATA_DIR}"
354         einfo
355
356         ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
357         sleep 5
358         eend 0
359
360         if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
361                 eerror "The given directory, '${DATA_DIR}', is not empty."
362                 eerror "Modify DATA_DIR to point to an empty directory."
363                 die "${DATA_DIR} is not empty."
364         fi
365
366         einfo "Creating the data directory ..."
367         if [[ ${EUID} == 0 ]] ; then
368                 mkdir -p "${DATA_DIR}"
369                 chown -Rf postgres:postgres "${DATA_DIR}"
370                 chmod 0700 "${DATA_DIR}"
371         fi
372
373         einfo "Initializing the database ..."
374
375         if [[ ${EUID} == 0 ]] ; then
376                 su postgres -c "${EROOT%/}/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -D \"${DATA_DIR}\" ${PG_INITDB_OPTS}"
377         else
378                 "${EROOT%/}"/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -U postgres -D "${DATA_DIR}" ${PG_INITDB_OPTS}
379         fi
380
381         if [[ "${DATA_DIR%/}" != "${PGDATA%/}" ]] ; then
382                 mv "${DATA_DIR%/}"/{pg_{hba,ident},postgresql}.conf "${PGDATA}"
383                 ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf "${DATA_DIR%/}"
384         fi
385
386         # unix_socket_directory has no effect in postgresql.conf as it's
387         # overridden in the initscript
388         sed '/^#unix_socket_directory/d' -i "${PGDATA%/}"/postgresql.conf
389
390         cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
391                 # This is here because of https://bugs.gentoo.org/show_bug.cgi?id=518522
392                 # On the off-chance that you might need to work with UTF-8 encoded
393                 # characters in PL/Perl
394                 plperl.on_init = 'use utf8; use re; package utf8; require "utf8_heavy.pl";'
395         EOF
396
397         einfo "The autovacuum function, which was in contrib, has been moved to the main"
398         einfo "PostgreSQL functions starting with 8.1, and starting with 8.4 is now enabled"
399         einfo "by default. You can disable it in the cluster's:"
400         einfo "    ${PGDATA%/}/postgresql.conf"
401         einfo
402         einfo "The PostgreSQL server, by default, will log events to:"
403         einfo "    ${DATA_DIR%/}/postmaster.log"
404         einfo
405         if use prefix ; then
406                 einfo "The location of the configuration files have moved to:"
407                 einfo "    ${PGDATA}"
408                 einfo "To start the server:"
409                 einfo "    pg_ctl start -D ${DATA_DIR} -o '-D ${PGDATA} --data-directory=${DATA_DIR}'"
410                 einfo "To stop:"
411                 einfo "    pg_ctl stop -D ${DATA_DIR}"
412                 einfo
413                 einfo "Or move the configuration files back:"
414                 einfo "mv ${PGDATA}*.conf ${DATA_DIR}"
415         else
416                 einfo "You should use the '${EROOT%/}/etc/init.d/postgresql-${SLOT}' script to run PostgreSQL"
417                 einfo "instead of 'pg_ctl'."
418         fi
419 }
420
421 src_test() {
422         if use server && [[ ${UID} -ne 0 ]] ; then
423                 emake check
424
425                 einfo "If you think other tests besides the regression tests are necessary, please"
426                 einfo "submit a bug including a patch for this ebuild to enable them."
427         else
428                 use server || \
429                         ewarn 'Tests cannot be run without the "server" use flag enabled.'
430                 [[ ${UID} -eq 0 ]] || \
431                         ewarn 'Tests cannot be run as root. Enable "userpriv" in FEATURES.'
432
433                 ewarn 'Skipping.'
434         fi
435 }