From 28ab1374c65af7a67cc253902738dabdd6a0ecee Mon Sep 17 00:00:00 2001 From: Fabrizio Lungo Date: Sat, 29 Oct 2016 11:17:27 +0100 Subject: [PATCH] Added support for alternate install root The install root can now be specified through the `INSTALL_ROOT` environment variable or with the `-r` or `--root` flags. The install root must be a valid directory. Resolves #12. --- usr/local/bin/shared-config-init | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/usr/local/bin/shared-config-init b/usr/local/bin/shared-config-init index 8e70d62..22d14e3 100755 --- a/usr/local/bin/shared-config-init +++ b/usr/local/bin/shared-config-init @@ -63,6 +63,14 @@ set_config_dir() { CONFIG_DIR="$1" } +set_config_dir() { + if [ -z "$1" ]; then + log "${VERBOSITY_ERROR}" "--root requires an argument." + exit 1 + fi + INSTALL_ROOT="$1" +} + # Is debug enabled by environment variable? if [ "${DEBUG,,}" == 'true' ]; then enable_debug @@ -74,6 +82,7 @@ SCRIPT_DIR="$(dirname "$(dirname "$(dirname "$(dirname "${SCRIPT}")")")")" # Initialise configurable variables CONFIG_DIR="${CONFIG_DIR:-${SCRIPT_DIR}}" +INSTALL_ROOT="" # Handle command line options while [ $# -gt 0 ]; do @@ -91,6 +100,10 @@ while [ $# -gt 0 ]; do shift set_config_dir "$1" ;; + -r|--root) + shift + set_config_dir "$1" + ;; *) log "${VERBOSITY_ERROR}" "Unrecognised option: $1" exit 1 @@ -106,11 +119,24 @@ fi # Get the config directory and make that the working directory if [ ! -d "${CONFIG_DIR}" ]; then - log "${VERBOSITY_ERROR}" "Config directory should be a valid directory" + log "${VERBOSITY_ERROR}" "Config directory is not a valid directory: ${CONFIG_DIR}" exit 1 fi cd "${CONFIG_DIR}" +# Sanitise the INSTALL_ROOT +INSTALL_ROOT="${INSTALL_ROOT:-/}" +INSTALL_ROOT="$(readlink -f "${INSTALL_ROOT}")" +if [ ! -d "${INSTALL_ROOT}" ]; then + log "${VERBOSITY_ERROR}" "Install root is not a valid directory: ${INSTALL_ROOT}" + exit 1 +fi +if [ "${INSTALL_ROOT}" == '/' ]; then + # Root directory is the only directory that will have a trailing slash. + # We don't want this so get rid of it. + INSTALL_ROOT='' +fi + # Store the exit code of the script EXIT_CODE=0 @@ -158,7 +184,7 @@ while read -r SOURCE; do log "${VERBOSITY_ERROR}" "Unexpected first character of find response: ${SOURCE}" exit 1 fi - DESTINATION="${SOURCE:1}" + DESTINATION="${INSTALL_ROOT}${SOURCE:1}" SOURCE="$(readlink -f "${SOURCE}")" if [ -e "${DESTINATION}" ]; then if [ -L "${DESTINATION}" ] && [ "$(readlink "${DESTINATION}")" == "${SOURCE}" ]; then