diff options
-rw-r--r-- | postgresql.eselect | 100 |
1 files changed, 68 insertions, 32 deletions
diff --git a/postgresql.eselect b/postgresql.eselect index 8d816dc..160e60e 100644 --- a/postgresql.eselect +++ b/postgresql.eselect @@ -13,6 +13,20 @@ B_PATH="${EROOT%/}/usr" E_PATH="${EROOT%/}/etc/eselect/postgresql" ENV_FILE="${EROOT%/}/etc/env.d/50postgresql" +# This list of files/directories are the symbolic link targets that need to be +# created when a slot is set. +# +# If you change this list, remember to change include_sources in do_set. And, +# they must be listed in the same order. +INCLUDE_TARGETS=( + "${B_PATH}"/include/postgresql + "${B_PATH}"/include/libpq-fe.h + "${B_PATH}"/include/pg_config_ext.h + "${B_PATH}"/include/pg_config_manual.h + "${B_PATH}"/include/libpq + "${B_PATH}"/include/postgres_ext.h +) + active_slot() { # ${B_PATH}/share/postgresql is a symlink. See if it's there, then # find out where it links to @@ -40,6 +54,8 @@ lib_dir() { fi } +### TODO: Refactor to move find into a separate function + ### Linker Function ### # Takes four arguments: # - Full source path (e.g. /usr/lib/postgresql-9.0/lib) @@ -75,10 +91,12 @@ linker() { # root if $ROOT is not "/". rel_source=$(relative_name "${link_source}" "${target_dir}") ln -s "${rel_source}" "${link_target}" || die -q "Unable to create link!" - echo "${link_target##${ROOT%/}/}" >> "${E_PATH}"/active.links${suffix} done } +### TODO: Refactor so that it searches source directories for removal targets, +### and move the old stuff into do_update + ### Unlinker Function ### # Takes one argument: # - Full path to active links file (e.g. /etc/eselect/postgresql/active.links) @@ -181,20 +199,31 @@ describe_set() { do_set() { local SLOT=$1 + if [[ ! -d ${B_PATH}/$(lib_dir)/postgresql-${SLOT} ]] ; then die -q "Not a valid slot." fi - echo "Setting ${SLOT} as the default installation..." + # If there's an active slot, unset that one first + if [[ "$(active_slot)" == "${SLOT}" ]] ; then + echo $(highlight "No work to do.") + echo "If you think the links need to be reset, use the reset action." + return 0 + elif [[ "$(active_slot)" != "(none)" ]] ; then + echo -ne "\tRemoving old links..." + do_unset $(active_slot) + echo "done." + fi - # Remove the active links to start a fresh list - echo -ne "\tRemoving old links..." - unlinker ${E_PATH}/active.links - echo "done." + echo "Setting ${SLOT} as the default installation..." echo -ne "\tGenerating new links..." - # Sources and targets for header files - local sources=( + # Sources for header files + # Targets are listed in the global variable INCLUDE_TARGETS. + # + # If you change this list, you must change the INCLUDE_TARGETS list, + # too. And, they must be listed in the same order. + local include_sources=( "${B_PATH}"/include/postgresql-${SLOT} "${B_PATH}"/include/postgresql-${SLOT}/libpq-fe.h "${B_PATH}"/include/postgresql-${SLOT}/pg_config_ext.h @@ -202,35 +231,20 @@ do_set() { "${B_PATH}"/include/postgresql-${SLOT}/libpq "${B_PATH}"/include/postgresql-${SLOT}/postgres_ext.h ) - local targets=( - "${B_PATH}"/include/postgresql - "${B_PATH}"/include/libpq-fe.h - "${B_PATH}"/include/pg_config_ext.h - "${B_PATH}"/include/pg_config_manual.h - "${B_PATH}"/include/libpq - "${B_PATH}"/include/postgres_ext.h - ) # The linker function cannot accomadate this special purpose. local rel_source local i - for (( i=0; $i < ${#sources[@]}; i++ )) ; do - # Remove target before creating the symlink - [[ -h ${targets[$i]} ]] && rm -f "${targets[$i]}" - - # Check if link_target still exists - [[ -e ${targets[$i]} ]] && \ - die -q "The target '${targets[$i]}' exists and could not be removed!" - + for (( i=0; $i < ${#include_sources[@]}; i++ )) ; do # Some headers are present only in specific versions of PostgreSQL - [[ -e ${sources[$i]} ]] || continue + [[ -e ${include_sources[$i]} ]] || continue # Create relative links so that they work both here and inside the new # root if $ROOT is not "/" - rel_source=$(relative_name "${sources[$i]}" "$(dirname "${targets[$i]}")") + rel_source=$(relative_name "${include_sources[$i]}" "$(dirname "${INCLUDE_TARGETS[$i]}")") - ln -s "$rel_source" "${targets[$i]}" || die -q "Unable to create link!" - echo "${targets[$i]##${ROOT%/}/}" >> "${E_PATH}"/active.links + ln -s "$rel_source" "${INCLUDE_TARGETS[$i]}" || \ + die -q "Unable to create link!" done # Link modules to /usr/lib{,32,64}/ @@ -240,8 +254,7 @@ do_set() { # 'linker' function doesn't work for linking directories. # Default lib path - create a relative link ln -s "postgresql-${SLOT}/${x}" "${B_PATH}/${x}/postgresql" - echo "${B_PATH##${ROOT%/}/}/${x}/postgresql" >> \ - "${E_PATH}"/active.links + # Linker works for files linker "${B_PATH}/${x}/postgresql-${SLOT}/${x}/" \ "-name lib*" "${B_PATH}/${x}" @@ -255,7 +268,6 @@ do_set() { # Default share path - use a relative link here by just specifying the # base name ln -s "postgresql-${SLOT}" "${B_PATH}/share/postgresql" - echo "${B_PATH##${ROOT%/}/}/share/postgresql" >> "${E_PATH}/active.links" echo "done." echo "Setting ${SLOT} as default was successful!" @@ -266,17 +278,41 @@ describe_unset() { echo "Remove symbolic links." } +# Undo everything done by do_set(). do_unset() { local SLOT=$1 if [[ ${SLOT} = $(active_slot) ]] ; then echo -n "Unsetting ${SLOT} as the default installation..." - unlinker "${E_PATH}/active.links" + echo "done." echo "Setting a new slot as the default." do_update else echo "Inactive slot selected. No work to do." fi + + local l + for l in ${INCLUDE_TARGETS[@]} "${B_PATH}/share/postgresql" ; do + # Remove target before creating the symlink + [[ -h ${l} ]] && rm -f "${l}" + + # Check if include target still exists + [[ -e ${l} ]] && \ + die -q "The target '${l}' exists and could not be removed!" + done + + for x in $(list_libdirs) ; do + if [[ -d ${B_PATH}/${x}/postgresql-${SLOT}/${x} ]] ; then + if [[ -h "${B_PATH}/${x}/postgresql" ]] ; then + rm -f "${B_PATH}/${x}/postgresql" + [[ -e "${B_PATH}/${x}/postgresql" ]] && \ + die -q "Unable to remove '${B_PATH}/${x}/postgresql'" + fi + + unlinker "${B_PATH}/${x}/postgresql-${SLOT}/${x}/" \ + "-name lib*" "${B_PATH}/${x}" + fi + done } ### Reset Action ### |