-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·161 lines (135 loc) · 4.45 KB
/
install.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env bash
# If called with two positional arguments, installs a single dotfile:
# ./install.sh TARGET_PATH LINK_PATH
#
# If called without arguments, try to auto install/upgrade all dotfiles:
# ./install.sh
usage() {
echo "usage: $0 [TARGET_PATH] [LINK_PATH]"
echo ""
echo " When called without arguments, install all dotfiles."
exit 0
}
prompt_yes_no() {
while true; do
read -p "$1 [y/n]: "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes)
echo "yes"
return
;;
n|no)
echo "no"
return
;;
*)
;;
esac
done
}
# Install a dotfile with prompt if the file exists already
install_dotfile() {
local target_path=$1
local link_path=$2
if ! [ -a "$target_path" ]; then
echo "Target path ($target_path) does not exist."
return 1
fi
# Check if the link already exists and points to the same file
if [ -L "$link_path" ] && [ "$(realpath -e "$link_path")" == "$(realpath -e "$target_path")" ]; then
echo "Link ($link_path) already points to the correct target, skipping..."
return 0
fi
if [ -a "$link_path" ]; then
local existing_target="$(realpath -e "$link_path")"
echo "The file ($link_path) already exists. Select an action:"
select action in "overwrite" "view diff" "view content" "skip"; do
case $action in
overwrite)
rm "$link_path" && ln -s "$target_path" "$link_path";
return 0
;;
"view diff")
diff "$existing_target" "$target_path"
read -p "Press Enter to continue..."
;;
"view content")
less "$existing_target"
;;
skip)
echo "File skipped"
return 0
;;
esac
done
fi
# link directory does not exist
_parent_dir="$(dirname "$link_path")"
if ! [ -d "$_parent_dir" ]; then
REPLY=$(prompt_yes_no "Directory $_parent_dir does not exist. Do you want to create it?")
if [ "$REPLY" == yes ]; then
mkdir -p "$_parent_dir"
else
echo "Directory not created, aborting."
return 0
fi
fi
# Create the link if it doesn't exist or is broken
rm "$link_path" 2>/dev/null
ln -s "$target_path" "$link_path"
echo "${link_path} -> ${target_path} installed."
}
for opt in "$@"; do
case $opt in
-h|--help|--usage)
usage
;;
*)
;;
esac
done
case "$#" in
0)
INSTALL_ALL=0
;;
2)
INSTALL_ALL=1
;;
*)
usage
;;
esac
if [ $INSTALL_ALL -ne 0 ]; then
install_dotfile "$1" "$2"
exit $?
fi
DIR="$(dirname "$(realpath -e $0)")"
# Shell #######################################################################
# Install manually by sourcing shell/.bashrc in ~/.bashrc
install_dotfile "$DIR"/shell/.inputrc "$HOME"/.inputrc
# cli_util ####################################################################
install_dotfile "$DIR"/cli_util/htoprc "$HOME"/.config/htop/htoprc
# git #########################################################################
install_dotfile "$DIR"/git/gitconfig "$HOME"/.gitconfig
install_dotfile "$DIR"/git/gitignore "$HOME"/.gitignore
install_dotfile "$DIR"/git/gitattributes "$HOME"/.gitattributes
# ssh #########################################################################
# Check if the dotfile is already included
grep -E "Include (\\w|/)+" ~/.ssh/config \
| cut -d ' ' -f2 \
| xargs -I % sh -c 'realpath -e %' \
| grep -q "^$(realpath -e ./ssh_config)$"
if [ "$?" -ne 0 ]; then
REPLY=$(prompt_yes_no "ssh not configured yet. Include this config in global file?")
if [[ "$REPLY" == "yes" ]]; then
echo -e "\nInclude $(realpath -e ./ssh_config)" >> ~/.ssh/config
else
echo "Not including ssh config."
fi
else
echo "ssh config already installed."
fi
# python ######################################################################
install_dotfile "$DIR"/python/uv.toml "$HOME"/.config/uv/uv.toml
# Misc ########################################################################
install_dotfile "$DIR"/.Rprofile "$HOME"/.Rprofile