Browse Source

Add symlink support to push/pull

master
Riyyi 5 years ago
parent
commit
8cb0272558
  1. 56
      dotfiles.sh

56
dotfiles.sh

@ -2,9 +2,16 @@
# User-config--------------------------- # User-config---------------------------
# File which holds all installed packages
PACKAGE_FILE="packages" PACKAGE_FILE="packages"
# Files that are stored in the repository but shouldn't get copied (regex)
EXCLUDE_FILES="${0#??}|$PACKAGE_FILE|.*.md$|.*README.org$|.git|screenshot.png" EXCLUDE_FILES="${0#??}|$PACKAGE_FILE|.*.md$|.*README.org$|.git|screenshot.png"
# Directories that are treated like a system directory (/) (regex)
SYSTEM_DIR='boot|etc|usr/share'
# Arch User Repository helper program name (needs pacman flag compatibility!)
AUR_HELPER="trizen" AUR_HELPER="trizen"
# -------------------------------------- # --------------------------------------
@ -59,14 +66,14 @@ EOF
set_files() { set_files() {
FILES="$(find . -type f -o -type l \ FILES="$(find . -type f -o -type l \
| awk -v e="^($EXCLUDE_FILES)" 'substr($0, 3) !~ e { print $0 }')" | awk -v e="^./($EXCLUDE_FILES)" '$0 !~ e { print $0 }')"
} }
list_files() { list_files() {
# If unset # If unset
[ -z "${FILES+x}" ] && set_files [ -z "${FILES+x}" ] && set_files
# Remove ./ from beginning of filepaths # Remove leading ./ from filepaths
echo "$FILES" | sed 's/^\.\///' echo "$FILES" | sed 's/^\.\///'
} }
@ -95,26 +102,31 @@ pull_push() {
# If unset # If unset
[ -z "${FILES+x}" ] && set_files [ -z "${FILES+x}" ] && set_files
for f in $FILES; do MATCH="^./($SYSTEM_DIR)/"
# Remove the first character (.) from the string
f=${f#?} # Filter system directories and remove leading ./ from filepaths
# Resolved symbolic link HOME_FILES="$(echo "$FILES" \
fr=$(readlink -f "$f") | awk -v m="$MATCH" '$0 !~ m { print substr($0, 3) }')"
# The filepath starts with '/boot/', '/etc/', '/usr/share/' for f in $HOME_FILES; do
if [ -n "$(echo "$fr" | sed -nE 's/^(\/(boot|etc|usr\/share)\/).*$/\1/p')" ]; then if [ "$1" = "pull" ]; then
if [ "$1" = "pull" ]; then # cp /home/<user>/<file> /[<some dir>/]dotfiles/<file>
sudo cp "$fr" "$(pwd)/$fr" cp -a "$HOME/$f" "$(pwd)/$f"
elif [ "$1" = "push" ]; then elif [ "$1" = "push" ]; then
sudo cp "$(pwd)/$fr" "$fr" cp -a "$(pwd)/$f" "$HOME/$f"
fi fi
else done
if [ "$1" = "pull" ]; then
# cp /home/<user>/<file> /home/<user>/[<some dir>/]dotfiles/<file> # Filter non-system directories and remove leading ./ from filepaths
cp "$HOME$f" "$(pwd)/$f" SYSTEM_FILES="$(echo "$FILES" \
elif [ "$1" = "push" ]; then | awk -v m="$MATCH" '$0 ~ m { print substr($0, 3) }')"
cp "$(pwd)/$f" "$HOME$f"
fi for f in $SYSTEM_FILES; do
if [ "$1" = "pull" ]; then
# cp /<file> /[<some dir>/]dotfiles/<file>
sudo cp -a "/$f" "$(pwd)/$f"
elif [ "$1" = "push" ]; then
sudo cp -a "$(pwd)/$f" "/$f"
fi fi
done done
} }

Loading…
Cancel
Save