-
Notifications
You must be signed in to change notification settings - Fork 10
/
symlinks.sh
executable file
·120 lines (100 loc) · 3.97 KB
/
symlinks.sh
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
117
118
119
120
#!/usr/bin/env bash
debug=${1:-false}
# Load help lib if not already loaded.
if [ -z ${libloaded+x} ]; then
source ./lib.sh
fi;
# Load dirs and files if not already loaded.
if [ -z ${filesloaded+x} ]; then
source ./files.sh
echo -en "\n"
fi;
# Set dotfilesdir var if not declared.
if [ -z ${dotfilesdir+x} ]; then
dotfilesdir="$(dirname "$(pwd)")"
fi;
# Set defaultdotfilesdir var if not declared.
if [ -z ${defaultdotfilesdir+x} ]; then
defaultdotfilesdir="$HOME/dotfiles"
fi;
# Used later if we need to symlink to "$HOME/dotfiles"
linkdotfilesdir=false
linkdotfilesdirerror=false
bot "Create symlinks for directories and files."
action "Creating directory symlinks."
# Symlink directories
for i in "${dotfilesdirarray[@]}"; do
# symlink, targetting from $dotfilesdir to $HOME
process_symlinks "$i" "$dotfilesdir" "$HOME"
done;
action "Creating file symlinks."
# Loop dotfiles directories with files, symlink them.
for i in "${dotfilesfilearray[@]}"; do
declare -a tmparr=()
# Properly store the results of find on these directories in an array
# We want to handle .*, *.cfg, *.conf and NOT .DS_Store, .git, .osx, .macos and no *.sh files
while IFS= read -r -d $'\0'; do
tmparr+=("$REPLY")
done < <(find "$i" -type f -maxdepth 1 \( -name ".*" -o -name "*.cfg" -o -name "*.conf" \) -a -not -name .DS_Store -not -name .git -not -name .osx -not -name .macos -not -name "*.sh" -print0)
for j in "${tmparr[@]}"; do
# Symlink, targetting from $i (parent folder of dotfile group) to $HOME
process_symlinks "$j" "$i" "$HOME"
done;
done;
# Custom symlinks.
action "Creating custom symlinks."
# git-friendly symlinks
process_symlinks "$dotfilesdir/bin/git-friendly/branch" "$dotfilesdir/bin/git-friendly" "$dotfilesdir/bin"
process_symlinks "$dotfilesdir/bin/git-friendly/merge" "$dotfilesdir/bin/git-friendly" "$dotfilesdir/bin"
process_symlinks "$dotfilesdir/bin/git-friendly/pull" "$dotfilesdir/bin/git-friendly" "$dotfilesdir/bin"
process_symlinks "$dotfilesdir/bin/git-friendly/push" "$dotfilesdir/bin/git-friendly" "$dotfilesdir/bin"
# If files are not stored in ~/dotfiles then symlink to that folder
# note: if ~/dotfiles does exist or links elsewhere it only breaks
# the alias `dotfiles` and export $DOTFILES.
if ! [[ "$defaultdotfilesdir" -ef "$pwd" ]]; then
action "Symlinking dotfiles dir to $defaultdotfilesdir"
if [ -e "$defaultdotfilesdir" ]; then
ask_for_confirmation "\n'$defaultdotfilesdir' already exists, do you want to overwrite it?"
if answer_is_yes; then
if [ -L "$defaultdotfilesdir" ]; then
# Target is a link, unlink it.
unlink "$defaultdotfilesdir"
# Mark to symlink to default dotfiles dir location.
linkdotfilesdir=true
else
ask_for_confirmation "\n'$defaultdotfilesdir' is a directory, unlinking will delete all files in this directory -- ARE YOU SURE?"
if answer_is_yes; then
# Target is a dir or file, trash or rm it.
if hash trash 2>/dev/null; then
# Prefer trash if installed, means we can restore.
trash "$defaultdotfilesdir"
else
# rm = bye bye.
rm -rf "$defaultdotfilesdir"
fi;
# Flag to symlink to default dotfiles dir location.
linkdotfilesdir=true
else
# Feedback to user that we couldn't symlink.
linkdotfilesdirerror=true
fi;
fi;
else
# Feedback to user that we couldn't symlink.
linkdotfilesdirerror=true
fi;
else
# Flag to symlink to default dotfiles dir location.
linkdotfilesdir=true
fi;
fi;
# Symlink to "$HOME/dotfiles" if required.
if $linkdotfilesdir; then
ln -s "$dotfilesdir" "$defaultdotfilesdir"
fi;
# Report error symlinking to "$HOME/dotfiles" if required.
if $linkdotfilesdirerror; then
error "Could not symlink to $defaultdotfilesdir -- this means the commands 'df', 'dotfiles' and export '\$DOTFILES_DIR' will not point to $dotfilesdir correctly."
fi;
echo -e "\n"
success "Processed symlinks.\n"