-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotmanager.sh
executable file
·179 lines (157 loc) · 5.39 KB
/
dotmanager.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/zsh
#
# Dotmanager
#
# Author: Goncalo Sousa
# License: MIT
# Version: 0.1
# GitHub: https://github.com/diutsu/dotmanager
# License: This project is licensed under the MIT License - see the LICENSE file for details.
#
# Dotmanager is a simple script for managing your dotfiles, allowing you to install, update, add,
# check status, or view differences with ease.
#
# Usage:
# ./dotmanager.sh [-iua:hsD:] [file]
#
# Options:
# -i: Install mode
# -u: Update mode
# -a <file>: Add mode. Add a new file to the managed files folder.
# -h: Print this help message.
# -s: Status mode. Print the status between the source and destination directories.
# -D <file>: Diff mode. Print the difference with the specified file.
#
src_dir="${DOTFILES:-.}/files"
dest_dir="$HOME"
print_help() {
echo "Usage: $0 [-iua:hsd:] [file]"
echo "Options:"
echo " -i: Install mode"
echo " -u: Update mode"
echo " -a <file>: Add mode. Add a new file to the managed files folder."
echo " -h: Print this help message."
echo " -s: Status mode. Print the status between the source and destination directories."
echo " -D <file>: Diff mode. Print the difference with the specified file."
exit 0
}
print_status() {
echo "Status between '$src_dir' and '$dest_dir':"
# Check status for each managed file
for file in "${managed_files[@]}"; do
rel_path=$(echo "$file" | sed "s|$src_dir/||")
dest_file="$dest_dir/$rel_path"
if [ -e "$dest_file" ]; then
if cmp -s "$file" "$dest_file"; then
echo "Up to date: $rel_path"
else
echo "Needs update: $rel_path"
fi
else
echo "Missing in local system: $rel_path"
fi
done
}
# Parse command-line options
while getopts "iua:hsD:" opt; do
case $opt in
i) mode="install" ;;
u) mode="update" ;;
a) mode="add"; new_file="$OPTARG" ;;
h) print_help ;;
s) mode="status" ;;
D) mode="diff" ; diff_file="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2 ; print_help ;;
esac
done
# Check if no mode is specified
if [[ -z "$mode" ]]; then
echo "Error: No mode specified. Use -i (install), -u (update), -a (add), or -h (help). Exiting."
exit 1
fi
# Check if the destination directory exists
if [[ ! -d "$dest_dir" ]]; then
echo "Destination directory '$dest_dir' does not exist. Exiting."
exit 1
fi
# Create a list of excluded files
excluded_files=("dotmanager.sh" "README.md" "LICENSE" ".git")
# Find managed files excluding excluded files
echo $src_dir
managed_files=($(find "$src_dir" -type f ! -name "${excluded_files[1]}" ! -name "${excluded_files[2]}" ! -name "${excluded_files[3]}" ! -name "${excluded_files[4]}"))
if [[ "$mode" == "add" ]]; then
# Check if a new file is provided
if [ -z "$OPTARG" ]; then
echo "Option -a requires a file path. Exiting."
exit 1
fi
# Get the absolute path of the new file
new_file=$(realpath "$OPTARG")
# Get the relative path to the home directory
rel_path=$(realpath --relative-to="$HOME" "$new_file")
# Check if the new file is under the home directory
if [[ "$rel_path" == ".."* ]]; then
echo "Error: The new file must be under the home directory ('$HOME'). Exiting."
exit 1
fi
# Check if the file already exists in the files folder
if [ -e "$dest_path" ]; then
# Update the existing file
cp "$new_file" "$dest_path"
echo "Updated $dest_path with $new_file"
else
# Copy the new file to the files folder
cp "$new_file" "$dest_path"
echo "Added $new_file to $dest_path"
fi
elif [[ "$mode" == "status" ]]; then
print_status
exit 0
elif [[ "$mode" == "diff" ]]; then
# Check if a diff file is provided
if [ -z "$diff_file" ]; then
echo "Error: Option -D requires a file path. Exiting."
exit 1
fi
# Get the absolute path of the diff file
diff_file=$(realpath "$diff_file")
# Check if the diff file is one of the managed files
rel_path=$(realpath --relative-to="$HOME" "$diff_file")
if [ ! -e "$src_dir/$rel_path" ]; then
echo "Error: The specified file is not managed. Exiting."
exit 1
fi
# Print the difference with the specified file
diff -u "$src_dir/$rel_path" "$diff_file"
exit 0
fi
# Process the list of files
for file in "${managed_files[@]}"; do
# Remove common prefix from file names
file_name=$(echo "$file" | sed "s|$src_dir/||")
dest_file="$dest_dir/$file_name"
if [[ "$mode" == "install" ]]; then
if [ -e "$dest_file" ]; then
if diff -r "$file" "$dest_file" > /dev/null; then
echo "No change between $file and $dest_file"
else
cp -r "$file" "$dest_dir"
echo "Updated $file with $dest_file"
fi
else
cp -r "$file" "$dest_dir"
echo "Created: $dest_file"
fi
elif [[ "$mode" == "update" ]]; then
if [ -e "$dest_file" ]; then
if diff -r "$file" "$dest_file" > /dev/null; then
echo "No change between $file and $dest_file"
else
cp -r "$dest_file" "$file"
echo "Updated: $file with $dest_file"
fi
else
echo "File not found in local filesystem: $dest_file. Skipping..."
fi
fi
done