-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·175 lines (146 loc) · 5.99 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·175 lines (146 loc) · 5.99 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env bash
###########################################################
# ____ ___ ____ __ __ _ ____ _ _ _ ____ #
# / ___|_ _/ ___| \/ | / \ / ___| | | | / \ | _ \ #
# \___ \| | | _| |\/| | / _ \| | | |_| | / _ \ | | | | #
# ___) | | |_| | | | |/ ___ \ |___| _ |/ ___ \| |_| | #
# |____/___\____|_| |_/_/ \_\____|_| |_/_/ \_\____/ #
# #
# ██████╗ █████╗ ███████╗██╗ ██╗██████╗ ██████╗ #
# ██╔══██╗██╔══██╗██╔════╝██║ ██║██╔══██╗██╔════╝ #
# ██████╔╝███████║███████╗███████║██████╔╝██║ #
# ██╔══██╗██╔══██║╚════██║██╔══██║██╔══██╗██║ #
# ██████╔╝██║ ██║███████║██║ ██║██║ ██║╚██████╗ #
# ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ #
# http://github.com/adstanley #
# http://sigmachad.io #
# Chet Manley #
###########################################################
#
# This script is designed to manage dotfiles in a user's home directory.
#
# TODO: Change entire script to use gnu stow
set -eou pipefail # Prevents errors in a pipeline from being masked
# trap "printf"
# Configuration
TEMP="$HOME/tmp"
# Github Directory
GITHUB_DIR="$HOME/.github"
# Dotfiles Directory
DOTFILES_DIR="$GITHUB_DIR/dotfiles"
# Dotfiles backup directory
BACKUP_DIR="$HOME/.backup/dotfiles_backup-$(date +%Y%m%d_%H%M%S)"
# Dotfiles repo url
REPO_URL="https://github.com/adstanley/dotfiles.git"
# Check if repo exists locally. If the repo does not exist locally clone it.
# If the repo exists locally, pull the latest changes.
# Check if the .git directory exists inside the target folder
if [ ! -d "$DOTFILES_DIR/.git" ]; then
printf "Cloning dotfiles repository...\n"
# If the directory exists but isn't a git repo, git clone might complain,
# so we handle the clone cleanly.
git clone "$REPO_URL" "$DOTFILES_DIR" || {
echo "Error: Failed to clone repository."
exit 1
}
else
printf "Valid Git repository detected. Pulling latest changes...\n"
# git -C executes the command inside that directory without permanently 'cd'ing there
git -C "$DOTFILES_DIR" pull || {
echo "Error: Failed to update repository."
exit 1
}
fi
# Make backup directory if it doesn't exist
if [ ! -d "$BACKUP_DIR" ]; then
if ! mkdir -p "$BACKUP_DIR"; then
printf "Error: Failed to create backup directory: '%s'\n" "$BACKUP_DIR"
exit 1
fi
fi
# Define dotfiles array
declare -A DOTFILES_ARRAY
DOTFILES_ARRAY=(
[".bashrc"]="bash"
[".gitconfig"]="git"
[".nanorc"]="nano"
[".tmux.conf"]="tmux"
["updatedb.conf"]="updatedb"
[".vimrc"]="vim"
[".zshrc"]="zsh"
)
# Reversion Function
function restore_backup() {
local target_file="$1"
local target="$2"
local backup_name="$(hostname)_$target_file"
if [ -f "$BACKUP_DIR/$backup_name" ]; then
printf "Restoring backup for %s.\n" "$target_file"
mv "$BACKUP_DIR/$backup_name" "$target" || {
printf "Error: Failed to restore backup for %s.\n" "$target_file"
exit 1
}
else
printf "No valid backup found for %s, skipping restoration.\n" "$target_file"
fi
}
# Process each dotfile in the array
for target_file in "${!DOTFILES_ARRAY[@]}"; do
# Target file is Key
target="$HOME/$target_file"
# Source subdir is value
source_subdir="${DOTFILES_ARRAY[$target_file]}"
# Construct source path
source="$DOTFILES_DIR/$source_subdir/$target_file"
# If file exists and it is not a symlink, back it up
if [ -f "$target" ] && [ ! -L "$target" ]; then
printf "Backing up existing %s to %s/\n" "$target_file" "$BACKUP_DIR"
mv -vn "$target" "$BACKUP_DIR/" || {
printf "Error: Failed to backup %s.\n" "$target_file"
exit 1
}
# Change backup file name to include hostname
mv "$BACKUP_DIR/$target_file" "$BACKUP_DIR/$(hostname)_$target_file"
fi
# Remove existing symlink if it exists
if [ -L "$target" ]; then
rm -v "$target" || {
printf "Error: Failed to remove existing symlink for %s.\n" "$target_file"
exit 1
}
fi
# Create symlink
if [ -f "$source" ]; then
printf "Creating symlink for %s\n" "$target_file"
ln -s "$source" "$target" || {
printf "Error: Failed to create symlink for %s.\n" "$target_file"
restore_backup "$target_file" "$target"
exit 1
}
else
printf "Warning: %s not found in %s directory of dotfiles repository.\n" "$target_file" "$source_subdir"
restore_backup "$target_file" "$target"
fi
done
printf "Dotfiles installation complete. Backups stored in %s\n" "$BACKUP_DIR"
# Define folder array
declare -A FOLDERS
FOLDERS=(
# ["bash_completion"]="$HOME/.bash_completion"
["nvim"]="$HOME/.config/nvim"
)
# Process each folder in the array
for folder_name in "${!FOLDERS[@]}"; do
target_folder="${FOLDERS[$folder_name]}"
source_folder="$DOTFILES_DIR/$folder_name"
# Create symlink for the folder
if [ -d "$source_folder" ]; then
printf "Creating symlink for %s\n" "$target_folder"
ln -s "$source_folder" "$target_folder" || {
printf "Error: Failed to create symlink for %s.\n" "$target_folder"
exit 1
}
else
printf "Warning: %s not found in dotfiles repository.\n" "$source_folder"
fi
done