-
Notifications
You must be signed in to change notification settings - Fork 1
/
symlinkctl
executable file
·116 lines (107 loc) · 4.29 KB
/
symlinkctl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
# Creates symbolic links for all dotfiles and directories in this directory to your home directory. Modifying
# $FILES_NOT_TO_SYMLINK_REGEX to exclude items from being symlinked. If a file with the same name already exists in your
# home directory, it will be backed-up with .backup extension.
# ## configuration variables ############################################################################################
FILES_NOT_TO_SYMLINK_REGEX="git|gitignore|gitmodules|idea|README.md|symlinkctl"
TOP_LEVEL_DIRECTORIES_TO_SYMLINK=("./.liquidprompt" "./.tmux")
TOP_LEVEL_DIRECTORIES_TO_TRAVERSE=("./" "./.ssh/" "./.vim/")
########################################################################################################################
# ## macOS support #####################################################################################################
if test "$(uname)" == "Darwin"; then
if command -v ggrep greadlink > /dev/null; then
unalias grep readlink
shopt -s expand_aliases
alias grep=ggrep readlink=greadlink
else
printf "\e[0;31m\n Please install coreutils (https://formulae.brew.sh/formula/coreutils) before running this script.\n\e[0m"
exit 1
fi
fi
########################################################################################################################
# $1 - pwd
# $2 - absolute home path
# $3 - dotfile path relative to pwd
enable() {
local DEST DOTFILE FILES_TO_SYMLINK GREP_STRING I SOURCE TYPE_STRING
mkdir -p "$2"/"$3"
GREP_STRING="\w\b(?<!$FILES_NOT_TO_SYMLINK_REGEX)$"
if [[ $3 == "./" ]]; then TYPE_STRING="-type f"; fi
# shellcheck disable=SC2207 disable=SC2086
FILES_TO_SYMLINK=($(find "$3" -maxdepth 1 $TYPE_STRING | grep -P "$GREP_STRING" | tr '\n' ' '))
if [[ $3 == "./" ]]; then
FILES_TO_SYMLINK+=("${TOP_LEVEL_DIRECTORIES_TO_SYMLINK[@]}")
fi
for DOTFILE in "${FILES_TO_SYMLINK[@]}"; do
DOTFILE="${DOTFILE#"$3"}"
SOURCE=$(readlink -f "$1/$3$DOTFILE")
DEST=$(readlink -f "$2/$3$DOTFILE")
# if the file exists in the home directory
if [[ -e "$DEST" ]]; then
# and the file that exists isn't already a symlink to the source file
if [[ "$DEST" != "$SOURCE" ]]; then
I=0
while true; do
I=$((I + 1))
# find a unique name for the backup file
if [[ -e "$DEST.$I.backup" ]]; then
continue
else
# backup the file
if mv "$DEST" "$DEST.$I.backup"; then
printf "\e[0;33m %s → %s\n" "$DEST" "$DEST.$I.backup"
# and create the symlink
if ln -fs "$SOURCE" "$DEST"; then
printf "\e[0;33m [✔] %s → %s\e[0m\n" "$SOURCE" "$DEST"
else
printf "\e[0;31m [✖] %s → %s\e[0m\n" "$SOURCE" "$DEST"
fi
break
else
printf "\e[0;31m could not copy %s → %s, exiting out of paranoia\n" "$DEST" "$DEST.$I.backup"
exit 1
fi
fi
done
else
printf "\e[0;32m [✔] %s is already linked \e[0m\n" "$DEST"
fi
else
# otherwise, create the symlink
if ln -fs "$SOURCE" "$DEST"; then
printf "\e[0;33m [✔] %s → %s\e[0m\n" "$SOURCE" "$DEST"
else
printf "\e[0;31m [✖] %s → %s\e[0m\n" "$SOURCE" "$DEST"
fi
fi
done
}
disable() {
local FILES_TO_UNLINK LINK_TARGET
# shellcheck disable=SC2207
FILES_TO_UNLINK=($(find ~ -maxdepth 2 -type l | tr '\n' ' '))
for LINKED_FILE in "${FILES_TO_UNLINK[@]}"; do
LINK_TARGET=$(readlink -f "$LINKED_FILE")
if [[ $LINK_TARGET == $(pwd)* ]]; then
printf "\e[0;33m [✔] removing linked file %s\e[0m\n" "$LINKED_FILE"
rm "$LINKED_FILE"
fi
done
}
if [[ "$1" == "enable" ]]; then
mkdir -p ~/.ssh/control
chmod 0600 .ssh/config
chmod 0600 .ssh/environment
for SYMLINK_PATH in "${TOP_LEVEL_DIRECTORIES_TO_TRAVERSE[@]}"; do
enable "$(pwd)" "$HOME" "$SYMLINK_PATH"
done
vim +PluginInstall +qall
~/.tmux/plugins/tpm/scripts/install_plugins.sh
exit 0
fi
if [[ "$1" == "disable" ]]; then
disable
exit 0
fi
printf "\e[0;31m\n Please enter an argument:\n\n \tenable:\t enable dotfiles by adding symlinks in your home directory\n\n \tdisable: disable dotfiles by removing the symlinks from your home directory \e[0m \n\n"
exit 0