Added mapping functionality for configuration files/folders in /home/hannes-subfolders.

This commit is contained in:
2013-09-16 03:00:36 +02:00
parent 8168bbe253
commit 6b3fbbc46e
2 changed files with 41 additions and 22 deletions

1
MAPPING Normal file
View File

@@ -0,0 +1 @@
terminator::.config/

View File

@@ -3,39 +3,57 @@
# config directory # config directory
config_dir="$HOME/dotfiles/" config_dir="$HOME/dotfiles/"
mapping_file="$config_dir/MAPPING"
# backup directory, files that would otherwise be overwritten go there # backup directory, files that would otherwise be overwritten go there
backup_dir="$HOME/dotfiles.bak/" backup_dir="$HOME/dotfiles.bak/"
# the following folders inside $config_dir will be inspected and symlinked: # the following folders inside $config_dir will be inspected and the
symlink_folders="bash git i3 vim zsh conky" # contents symlinked:
symlink_folders="bash git i3 vim zsh conky terminator"
path_combine() path_combine()
{ {
echo "$(dirname "$1")/$(basename "$1")/$(basename "$2")" echo "$(dirname "$1")/$(basename "$1")/$(basename "$2")"
} }
# backup the old config files DEFAULT_ROOT="$HOME"
backup_dir="$backup_dir/$(date +%Y-%m-%dT%H:%M:%S)" # returns "" if no mapping necessary
echo "Backing up old configuration files into \"$backup_dir\"." # format of the MAPPING file:
mkdir -p "$backup_dir" # folder::root
for folder in $symlink_folders ; do get_mapping()
for file in "$(path_combine "$config_dir" "$folder")"/* ; do {
oldfile="$(path_combine "$HOME" ".$(basename "$file")")" entry="$(grep -E "^$1::.+$" "$mapping_file" | head -n1 | grep -Eo "::.+$" | cut -c 3-)"
if [[ -e "$oldfile" ]] ; then echo "$entry"
destination="$(path_combine "$backup_dir" "$(basename "$oldfile")")" }
echo "mv: \"$oldfile\" -> \"$destination\""
mv "$oldfile" "$destination"
else
echo "\"$oldfile\" not found, skipped."
fi
done
done
# now symlink the files in $config_dir into the home directory
echo "Creating symlinks for configuration files in \"$config_dir\"." backup_dir="$backup_dir/$(date +%Y-%m-%dT%H:%M:%S)"
# backup the old config files, symlinks will be skipped
# then symlink the files in $config_dir into the home directory
for folder in $symlink_folders ; do for folder in $symlink_folders ; do
for file in "$(path_combine "$config_dir" "$folder")"/* ; do mapping="$(get_mapping "$folder")"
destination="$(path_combine "$HOME" ".$(basename "$file")")"
source_folder="$(path_combine "$config_dir" "$folder")"
[[ "$(ls "$source_folder")" ]] || continue
for file in "$source_folder"/* ; do
if [[ -z "$mapping" ]]; then
destination="$(path_combine "$DEFAULT_ROOT" ".$(basename "$file")")"
else
destination="$(path_combine "$(path_combine "$HOME" "$mapping")" "$(basename "$file")")"
fi
if [[ -L "$destination" ]]; then
continue
elif [[ -e "$destination" ]]; then
backup_destination="$(path_combine "$backup_dir" "$(basename "$destination")")"
[[ -d "$backup_dir" ]] || mkdir -p "$backup_dir"
echo "mv \"$destination\" -> \"$backup_destination\""
mv "$destination" "$backup_destination"
fi
[[ -e "$destination" ]] && [[ "$(readlink "$destination")" == "$file" ]] && continue
echo "ln -sf \"$file\" -> \"$destination\"" echo "ln -sf \"$file\" -> \"$destination\""
ln -sf "$file" "$destination" ln -sf "$file" "$destination"
done done