-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·124 lines (107 loc) · 3.76 KB
/
setup.sh
File metadata and controls
executable file
·124 lines (107 loc) · 3.76 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
#!/bin/bash
set -o nounset
set -o pipefail
set -o errexit
# Default behavior: do backup and install fonts
DO_BACKUP=1
DO_FONTS=1
DOTFILES="$HOME/dotfiles"
function parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--no-backup)
DO_BACKUP=0
shift
;;
--no-fonts)
DO_FONTS=0
shift
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [--no-backup] [--no-fonts]"
exit 1
;;
esac
done
}
function install_fonts() {
if [ -f "$HOME/.config/fonts_installed" ]; then
echo "Fonts already installed, skipping."
return
fi
git clone --filter=blob:none https://github.com/ryanoasis/nerd-fonts.git fonts
cd fonts
./install.sh
cd ..
rm -rf fonts
touch "$HOME/.config/fonts_installed"
}
function do_clone() {
local OH_MY_ZSH_DIR="$HOME/.oh-my-zsh"
if [ -d "$OH_MY_ZSH_DIR/.git" ]; then
echo "oh-my-zsh already installed at $OH_MY_ZSH_DIR"
else
if [ -d "$OH_MY_ZSH_DIR" ]; then
echo "Warning: $OH_MY_ZSH_DIR exists but is not a valid Git repo. Removing..."
rm -rf "$OH_MY_ZSH_DIR"
fi
echo "Cloning oh-my-zsh to $OH_MY_ZSH_DIR"
git clone https://github.com/robbyrussell/oh-my-zsh.git "$OH_MY_ZSH_DIR"
fi
local CUSTOM_PATH=${ZSH_CUSTOM:-$OH_MY_ZSH_DIR/custom}
if [ ! -d "$CUSTOM_PATH/plugins/zsh-autosuggestions" ]; then
echo "Cloning zsh-autosuggestions to $CUSTOM_PATH/plugins/zsh-autosuggestions"
git clone https://github.com/zsh-users/zsh-autosuggestions "$CUSTOM_PATH/plugins/zsh-autosuggestions"
else
echo "zsh-autosuggestions already installed"
fi
}
function backup_file() {
local src="$1" # Source file in $DOTFILES
local dest="$2" # Destination in $HOME
local ts="$3" # Timestamp for backup directory
local rel_path="$4" # Relative path for subfolder backups
if [ $DO_BACKUP -eq 1 ] && [ -e "$dest" ] && [ ! "$(readlink "$dest")" = "$src" ]; then
echo "Backing up $dest to $HOME/.old_dotfiles.$ts/"
mkdir -p "$HOME/.old_dotfiles.$ts/$(dirname "$rel_path")"
mv "$dest" "$HOME/.old_dotfiles.$ts/$rel_path"
fi
}
function make_links() {
local TS=""
[ $DO_BACKUP -eq 1 ] && TS=$(date +'%Y-%m-%d_%H%M')
# Enable dotglob to include dotfiles in globbing
shopt -s dotglob
# Symlink files in the root of $DOTFILES to $HOME, excluding setup.sh
for file in "$DOTFILES"/*; do
if [ -f "$file" ] && [ "$(basename "$file")" != "setup.sh" ]; then
local dest="$HOME/$(basename "$file")"
backup_file "$file" "$dest" "$TS" "$(basename "$file")"
ln -sf "$file" "$dest"
echo "Symlinked $(basename "$file") to $dest"
fi
done
# Symlink files in subfolders, creating directories as needed, excluding .git
find "$DOTFILES" -mindepth 2 -type f -not -path "$DOTFILES/.git/*" | while read -r src; do
# Get relative path from $DOTFILES (e.g., ".local/bin/kubectx")
local rel_path="${src#$DOTFILES/}"
# Destination path in $HOME (e.g., "$HOME/.local/bin/kubectx")
local dest="$HOME/$rel_path"
# Directory to create (e.g., "$HOME/.local/bin")
local dest_dir="$(dirname "$dest")"
# Backup if file exists and isn’t already the right symlink
backup_file "$src" "$dest" "$TS" "$rel_path"
# Create the directory and symlink
mkdir -p "$dest_dir"
ln -sf "$src" "$dest"
echo "Symlinked $rel_path to $dest"
done
}
function main() {
parse_args "$@"
do_clone
[ $DO_FONTS -eq 1 ] && install_fonts
make_links
}
main "$@"