-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-config.sh
executable file
·104 lines (86 loc) · 2.92 KB
/
update-config.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
#!/bin/bash
function file_update() {
local fileSource="$1"
# Remove the sync folder from the destination file path
local fileDest="/${fileSource#sync/}"
# Substitute the string "my_user" with the actual user
fileDest="${fileDest//my_user/$USERNAME}"
# Create the destination file and the path if they do not not exist
if [ ! -f "$fileDest" ]; then
print_color red "The file $fileDest does not exist"
filePath="$(dirname "$fileDest")"
# Get the owner of the closest existing folder to the file
existingPath="$filePath"
while [ ! -d "$existingPath" ] && [ "$existingPath" != "/" ]; do
existingPath="$(dirname "$existingPath")"
if [ -d "$existingPath" ]; then
break
fi
done
local folderOwnerAndGroup=$(stat -c "%U:%G" "$existingPath")
local folderOwner=$(stat -c "%U" "$existingPath")
if [ "$folderOwner" = "root" ]; then
sudo mkdir -p "$filePath"
else
mkdir -p "$filePath"
fi
sudo touch "$fileDest"
sudo chown $folderOwnerAndGroup "$fileDest"
# Setting restricting permissions on new files
sudo chmod 644 "$fileDest"
print_color green "Created new file $fileDest with owner $folderOwner"
fi
git diff $fileDest $fileSource >/dev/null 2>&1
diff_found=$?
if [ $diff_found -ne 0 ]; then
echo "Showing diff between local and remote $(basename "$fileDest") file:"
# Even if code is installed, we don't want to use it to show the diff if running in tty mode
if command -v code >/dev/null 2>&1 && [ -n "$XDG_SESSION_TYPE" ] && [ "$XDG_SESSION_TYPE" != "tty" ]; then
code --diff $fileDest $fileSource
else
git --no-pager diff --no-index --color=always $fileDest $fileSource
fi
print_color yellow "Do you want to update $fileDest (y/n)?"
read -r answer
if [ "$answer" == "${answer#[Yy]}" ]; then
print_color red "Not updating $(basename "$fileDest")"
else
# Save the original owner of the file
local owner=$(stat -c "%U:%G" $fileDest)
local ownerPermissions=$(stat -c "%a" $fileDest)
sudo cp -i -p $fileSource $fileDest
# Restore the original owner of the file.
# Git does not maintain the original owner when you commit a file
sudo chown $owner $fileDest
# Restore also original permission
sudo chmod $ownerPermissions "$fileDest"
fi
fi
}
function checkDeps() {
$SCRIPT_PATH/install-deps.sh update
}
USERNAME=$(whoami)
SCRIPT_PATH=$(dirname "${BASH_SOURCE[0]}")
if [[ "${SCRIPT_PATH:0:1}" != "/" ]]; then
echo "This script needs to be called with the absolute path"
exit 1
fi
cd $SCRIPT_PATH
source print-color.sh
# Get all files in the sync folder
mapfile -t files < <(find sync -type f)
# Update all the files
for file in "${files[@]}"; do
file_update "$file"
done
print_color white "Config was updated!"
# Optional check for deps if the deps argument is present
case "$1" in
deps)
checkDeps ;;
init)
;;
*)
;;
esac