forked from DovieW/obsidian-android-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-vaults.sh
90 lines (71 loc) · 2.07 KB
/
sync-vaults.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
#!/data/data/com.termux/files/usr/bin/bash
source /data/data/com.termux/files/usr/etc/bash.bashrc
LOCK_FILE="$HOME/sync-vaults.lock"
# Function to remove lock file
cleanup() {
rm -f "$LOCK_FILE"
exit 1
}
# Trap to catch interruptions
trap cleanup INT TERM
# Function to wait for lock release
wait_for_lock_release() {
while [ -e "$LOCK_FILE" ]; do
sleep 1
done
}
if [ -e "$LOCK_FILE" ]; then # check if the lock file exists
if [ -z "$(ps -p $(cat "$LOCK_FILE") -o pid=)" ]; then # Check if the process that created the lockfile is still running
echo "Removing stale lock file."
rm -f "$LOCK_FILE"
else
wait_for_lock_release
fi
fi
# Store the PID in the lock file
echo $$ > "$LOCK_FILE"
skip_pause_val="--skip-pause"
source "$HOME/log_helper.sh"
log_file="$HOME/sync.log"
setup_logging $log_file
cmd () {
printf "\n\033[0;34m%s\033[0m\n" "$(basename "$PWD")"
$HOME/git-sync -ns 2>&1 | tee $LAST_SYNC_PATH
if [ $? -ne 0 ]; then
cat $LAST_SYNC_PATH >> $NOTIFICATION_PATH # Send notifcation
fi
}
git_repos=()
# Populate the array with Git repos from the Obsidian folder
for dir in "$OBSIDIAN_DIR_PATH"/*; do
if [ -d "$dir" ]; then
cd "$dir"
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
git_repos+=("$dir")
fi
fi
done
msg="You can try running 'setup' to see if it helps".
# Exit if no Git repos are found
if [ ${#git_repos[@]} -eq 0 ]; then
echo -e "${YELLOW}No Git repositories found in the Obsidian folder.\n${msg}${RESET}"
exit 1
fi
if [[ -n "$1" && "$1" != "$skip_pause_val" ]]; then # Sync a single repo
if [[ " ${git_repos[@]} " =~ " $OBSIDIAN_DIR_PATH/$1 " ]]; then
(cd "$OBSIDIAN_DIR_PATH/$1" && cmd)
else
echo -e "${RED}Specified directory doesn't exist or is not a Git repository.\n${msg}${RESET}"
exit 1
fi
else # Sync all Git repos
for repo in "${git_repos[@]}"; do
(cd "$repo" && cmd)
done
fi
log_cleanup $log_file
if [[ -z "$1" ]]; then
bypass_log "echo -e '\n\033[44;97mPress enter to finish...\033[0m' && read none"
fi
rm -f "$LOCK_FILE"
exit 0