diaspora/script/install/path_sanitizer
Florian Staudacher 4005cd05ea refactor install script
the script is now split up in parts that will be fetched from github
on demand.
fixes #4047
2013-05-01 14:01:11 +02:00

40 lines
1.1 KiB
Bash

#!/usr/bin/env bash
# to be included by install.sh
# Deconstruct a given path string, applies bash expansion and remove all
# remaining relative fragments (e.g. "." or "..").
# Writes the result in the two given variable names, first is the portion
# with the existing path and the second contains the structure relative to the
# existing path, that'd have to be created.
# usage:
# sanitize_path "~/some/path/string" "EXISTING_VAR_NAME" "REL_NEW_PATH_NAME"
sanitize_path() {
# apply bash expansion
eval _path=$1
_existing_path_var=$2
_rel_new_segment_var=$3
_new_segment=""
_chk=1
_test_cmd='test -d "$_path" -a -n "$_path"'
$(eval $_test_cmd) && _chk=0
while [ $_chk -ne 0 ] ; do
# path doesn't exist, split it up
_segment="$(basename $_path)/$_segment"
_path="$(dirname $_path)"
$(eval $_test_cmd) && _chk=0
done
# remove relative fragments
_path="$(realpath $_path)/"
log_dbg "pt1 - existing path: $_path"
log_dbg "pt2 - new path: $_segment"
eval "$_existing_path_var=\"$_path\""
eval "$_rel_new_segment_var=\"$_segment\""
}