-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.sh
executable file
·160 lines (136 loc) · 4.48 KB
/
link.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
#!/bin/bash
set -euo pipefail
cleanup() {
echo
print_message info "Script interrupted....."
exit 1
}
# Set up trap for cleanup
trap cleanup INT TERM
dotfiles_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
config_dir="$HOME/.config"
backup_dir="$HOME/config_backup"
backup_timestamp=$(date +"%Y%m%d_%H%M%S")
source "$dotfiles_dir/scripts/utils.sh"
ensure_dir() {
local dir="$1"
if [[ ! -d "$dir" ]]; then
print_message warning "Directory $dir does not exist. Creating..."
if mkdir -p "$dir"; then
print_message success "Created $dir directory"
else
print_message error "Failed to create directory: $dir"
exit 1
fi
fi
}
ensure_dir "$config_dir"
ensure_dir "$backup_dir"
# Create symbolic link
create_symlink() {
local source_dir="$1"
local target_dir="$2"
local target_name="$3"
if [[ -L "$target_dir/$target_name" ]]; then
print_message warning "Removing existing symlink: $target_name"
unlink "$target_dir/$target_name" || {
print_message error "Failed to remove existing symlink: $target_name"
return 1
}
fi
if [[ -d "$target_dir/$target_name" ]]; then
local backup_name="${target_name}.bk-${backup_timestamp}"
print_message warning "$target_name directory already exists. Removing"
mv "${target_dir:?}/$target_name" "$backup_dir/$backup_name" || {
print_message error "Failed to move $target_name to backup"
return 1
}
print_message success "Moved $target_name to $backup_dir"
fi
print_message info "Creating symbolic link for $target_name"
ln -s "$source_dir/$target_name" "$target_dir/$target_name" || {
print_message error "Failed to create symbolic link for $target_name"
return 1
}
print_message success "Successfully created symbolic link for $target_name"
}
# Function to link config files
link_config() {
local config="$1"
echo -e "\n"
create_symlink "$dotfiles_dir/config" "$config_dir" "$config" || exit 1
}
# Function to link shell files
link_home_file() {
local file="$1"
local source_file="$dotfiles_dir/home/$file"
local target_file="$HOME/$file"
# Validate source file exists
if [[ ! -f "$source_file" ]]; then
print_message error "Source file not found: $source_file"
return 1
fi
if [[ -L "$target_file" ]]; then
print_message warning "Existing symlink found for $file. Unlinking."
unlink "$target_file"
fi
if [[ -e "$target_file" ]]; then
local backup_name="${file}.bk-${backup_timestamp}"
print_message warning "File $target_file exists. Backing up."
# Move existing file to backup with timestamped name
if mv "$target_file" "$backup_dir/$backup_name"; then
print_message success "Moved $file to $backup_dir/$backup_name"
else
print_message error "Failed to move $file to backup"
return 1
fi
fi
if ln -s "$source_file" "$target_file"; then
print_message success "Created symbolic link for $file"
else
print_message error "Failed to create symbolic link for $file"
return 1
fi
print_message success "Successfully created symbolic link for $file"
}
link_zsh_files() {
local zsh_files=(".zshrc" ".p10k.zsh")
for file in "${zsh_files[@]}"; do
link_home_file "$file"
done
link_config "zsh"
}
link_git_files() {
link_home_file ".gitconfig"
}
# Available config files
configs=("bat" "cava" "git" "hypr" "kitty" "Kvantum" "lazygit" "lazyvim" "fastfetch" "nvim" "rofi" "swaync" "tmux" "waybar" "wlogout" "zsh")
# If no arguments provided, show usage
if [ $# -eq 0 ]; then
print_message error "Usage: $0 [config_name ...] [all]"
print_message info "Available configs: ${configs[*]}"
exit 1
fi
# Process arguments
for arg in "$@"; do
if [ "$arg" = "all" ]; then
for config in "${configs[@]}"; do
case "$config" in
"zsh") link_zsh_files ;;
"git") link_git_files ;;
*) link_config "$config" ;;
esac
done
break
elif [[ " ${configs[*]} " == *" $arg "* ]]; then
case "$arg" in
"zsh") link_zsh_files ;;
"git") link_git_files ;;
*) link_config "$arg" ;;
esac
else
print_message error "Unknown config: $arg"
exit 1
fi
done
print_message success "All requested configurations have been linked."