Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·142 lines (123 loc) · 4.03 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·142 lines (123 loc) · 4.03 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
set -u
function make_link() {
local source_file="$1"
local target_file="$2"
local valid_choice="N"
# Check if the target already exists
if [ -e "$target_file" ]; then
until [ "$valid_choice" = "Y" ]; do
# Prompt the user to replace
read -rp "$target_file already exists. Replace with symlink to $source_file? (y)es, (n)o or (a)bort [enter = no]: " replace_target </dev/tty
[ -z "$replace_target" ] && replace_target="N"
case "$replace_target" in
[yY])
# User opted to replace, so remove the existing target
echo "Removing existing $target_file"
rm -rf "$target_file"
valid_choice="Y"
;;
[nN])
# User declined to replace, so take no action and return
echo "Leaving $target_file untouched."
return
;;
[aA])
# User aborted
echo "Leaving $target_file untouched. Aborting."
exit 1
;;
*)
# Invalid choice, prompt again
echo "Invalid choice. Please enter 'y', 'n', or 'a'."
;;
esac
done
fi
# Create the symlink
echo "Linking $source_file -> $target_file"
ln -sfn "$source_file" "$target_file" || {
echo "Failed to create symlink from $source_file to $target_file" >&2
exit 1
}
}
# Default mode is to install (i), unless -u option passed
MODE="i"
while getopts 'u' OPTION; do
case $OPTION in
u) MODE="u"
;;
?) echo "Usage: $0 [-u]" 1>&2
exit 1
;;
esac
done
# Get the name of this script
THIS_SCRIPT="$(basename "$0")"
# Change to the script directory
cd "$(dirname "$0")" || exit 1
# Iterate down a (possible) chain of symlinks
while [ -L "$THIS_SCRIPT" ]
do
THIS_SCRIPT="$(readlink "$THIS_SCRIPT")"
cd "$(dirname "$THIS_SCRIPT")" || exit 1
THIS_SCRIPT="$(basename "$THIS_SCRIPT")"
done
# Canonicalize the physical path
SCRIPT_DIR="$(pwd -P)"
if [ "$MODE" = "i" ]; then
# Make the topics available at ~/.dotfiles
DOTFILES_DIR="$HOME/.dotfiles"
# If we're not already in ~/.dotfiles
[ "$SCRIPT_DIR" != "$DOTFILES_DIR" ] && make_link "$SCRIPT_DIR" "$DOTFILES_DIR"
# Symlink all {name}.symlink files in topics to ~/.{name}
while IFS= read -r symlink_file; do
symlink_basename="$(basename "$symlink_file")"
symlink_basename="$HOME/.${symlink_basename%.symlink}"
make_link "$symlink_file" "$symlink_basename"
done < <(find "$SCRIPT_DIR" -name "*.symlink" -type f)
# Special case symlink for claude files
CLAUDE_DIR="$HOME/.claude"
if [ -d "$CLAUDE_DIR" ]; then
make_link "$SCRIPT_DIR/claude/CLAUDE.md" "$CLAUDE_DIR/CLAUDE.md"
make_link "$SCRIPT_DIR/claude/settings.json" "$CLAUDE_DIR/settings.json"
fi
# Special case symlink for vscode files
VSCODE_DIR="$HOME/Library/Application Support/Code/User"
if [ -d "$VSCODE_DIR" ]; then
make_link "$SCRIPT_DIR/vscode/projects.json" "$VSCODE_DIR/projects.json"
make_link "$SCRIPT_DIR/vscode/settings.json" "$VSCODE_DIR/settings.json"
make_link "$SCRIPT_DIR/vscode/snippets" "$VSCODE_DIR/snippets"
fi
# Special case symlink for Maven settings
M2_DIR="$HOME/.m2"
[ -d "$M2_DIR" ] && make_link "$SCRIPT_DIR/maven/settings.xml" "$M2_DIR/settings.xml"
# Copy all {name}.launchagent files in topics to ~/Library/LaunchAgents/{name}.plist
if [ "$(uname)" = "Darwin" ]; then
mkdir -p "$HOME/Library/LaunchAgents"
while IFS= read -r launchagent_file; do
launchagent_basename="$(basename "$launchagent_file")"
launchagent_basename="$HOME/Library/LaunchAgents/${launchagent_basename%.launchagent}.plist"
echo "Copying $launchagent_file -> $launchagent_basename"
cp "$launchagent_file" "$launchagent_basename"
done < <(find "$SCRIPT_DIR" -name "*.launchagent" -type f)
fi
else
# Remove any symlinks to the script directory
for symlink_file in "$HOME"/.[!.]* "$HOME"/*; do
# Skip if file does not exist
[ -e "$symlink_file" ] || [ -L "$symlink_file" ] || continue
# Check if it's a symlink pointing to the script directory
if [ -L "$symlink_file" ]; then
target="$(readlink "$symlink_file")"
case "$target" in
"$SCRIPT_DIR"/*)
echo "Unlinking $(basename "$symlink_file")"
rm "$symlink_file"
;;
esac
fi
done
fi
echo "Done"
exit 0