2013-09-14 14:35:23 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
# config directory
|
2013-09-14 16:00:46 +02:00
|
|
|
config_dir="$HOME/dotfiles/"
|
2013-09-14 14:35:23 +02:00
|
|
|
|
2013-09-16 03:00:36 +02:00
|
|
|
mapping_file="$config_dir/MAPPING"
|
|
|
|
|
|
2013-09-14 14:35:23 +02:00
|
|
|
# backup directory, files that would otherwise be overwritten go there
|
2013-09-16 21:36:23 +02:00
|
|
|
backup_dir="$HOME/.dotfiles.bak/"
|
2013-09-14 14:35:23 +02:00
|
|
|
|
2013-09-16 03:00:36 +02:00
|
|
|
# the following folders inside $config_dir will be inspected and the
|
|
|
|
|
# contents symlinked:
|
2013-10-01 02:49:55 +02:00
|
|
|
symlink_folders='bash git i3 vim zsh conky terminator x mpd ncmpcpp'
|
2013-09-16 03:00:36 +02:00
|
|
|
|
2013-09-16 21:36:23 +02:00
|
|
|
MAPPING_SEPARATOR='::'
|
2013-09-14 14:35:23 +02:00
|
|
|
|
2013-09-14 16:00:46 +02:00
|
|
|
path_combine()
|
|
|
|
|
{
|
|
|
|
|
echo "$(dirname "$1")/$(basename "$1")/$(basename "$2")"
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-16 03:00:36 +02:00
|
|
|
DEFAULT_ROOT="$HOME"
|
|
|
|
|
# returns "" if no mapping necessary
|
|
|
|
|
# format of the MAPPING file:
|
|
|
|
|
# folder::root
|
|
|
|
|
get_mapping()
|
|
|
|
|
{
|
2013-09-16 21:36:23 +02:00
|
|
|
entry="$(grep -E "^$1$MAPPING_SEPARATOR.+$" "$mapping_file" | head -n1 | grep -Eo "$MAPPING_SEPARATOR.+$" | cut -c 3-)"
|
2013-09-16 03:00:36 +02:00
|
|
|
echo "$entry"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-14 14:35:23 +02:00
|
|
|
backup_dir="$backup_dir/$(date +%Y-%m-%dT%H:%M:%S)"
|
2013-09-16 03:00:36 +02:00
|
|
|
|
|
|
|
|
# backup the old config files, symlinks will be skipped
|
|
|
|
|
# then symlink the files in $config_dir into the home directory
|
2013-09-14 14:35:23 +02:00
|
|
|
for folder in $symlink_folders ; do
|
2013-09-16 03:00:36 +02:00
|
|
|
mapping="$(get_mapping "$folder")"
|
|
|
|
|
|
|
|
|
|
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")")"
|
2013-09-14 14:35:23 +02:00
|
|
|
else
|
2013-09-16 03:00:36 +02:00
|
|
|
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"
|
2013-09-14 14:35:23 +02:00
|
|
|
fi
|
|
|
|
|
|
2013-09-16 03:00:36 +02:00
|
|
|
[[ -e "$destination" ]] && [[ "$(readlink "$destination")" == "$file" ]] && continue
|
2013-09-14 16:00:46 +02:00
|
|
|
echo "ln -sf \"$file\" -> \"$destination\""
|
|
|
|
|
ln -sf "$file" "$destination"
|
2013-09-14 14:35:23 +02:00
|
|
|
done
|
|
|
|
|
done
|
|
|
|
|
|