-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
·64 lines (54 loc) · 1.79 KB
/
sync.sh
File metadata and controls
executable file
·64 lines (54 loc) · 1.79 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
#!/bin/bash
DIRECTION="${1:-forward}"
if [[ -e .config.env ]]; then
source .config.env
fi
# BLOG_SOURCE is the upstream (obsidian)
# BLOG_DEST is the folder in this repository where the blogs should be moved/copied from
if [[ -z "$BLOG_SOURCE" || -z "$BLOG_DEST" ]]; then
echo "BLOG_SOURCE or BLOG_DEST not set"
exit 1
fi
if [[ "$DIRECTION" == "forward" ]]; then
SOURCE_FOLDER="$BLOG_SOURCE"
DEST_FOLDER="$BLOG_DEST"
else
SOURCE_FOLDER="$BLOG_DEST"
DEST_FOLDER="$BLOG_SOURCE"
fi
rsync -av "$SOURCE_FOLDER" "$DEST_FOLDER"
if [[ -z "$ATTACHMENT_SOURCE" || -z "$ATTACHMENT_DEST" ]]; then
echo "ATTACHMENT_SOURCE or ATTACHMENT_DEST not set, skipping copying"
exit 0
fi
if [[ "$DIRECTION" == "forward" ]]; then
SOURCE_ATT="$ATTACHMENT_SOURCE"
DEST_ATT="$ATTACHMENT_DEST"
else
SOURCE_ATT="$ATTACHMENT_DEST"
DEST_ATT="$ATTACHMENT_SOURCE"
fi
# Iterate through each Markdown file in the source folder
for markdown_file in "$SOURCE_FOLDER"/*.md; do
# Skip if no markdown files are found // does this work????
if [[ ! -e "$markdown_file" ]]; then
echo "No Markdown files found in $SOURCE_FOLDER."
exit 0
fi
# Extract filenames from image links 
grep -o '!\[[^]]*\](\([^)]*\))' "$markdown_file" | sed 's/.*(\(.*\))/\1/' | while read -r match; do
# Remove ![]() to get the filename
extracted_file=$(echo "$match" | xargs basename)
# Full path of the file to copy
file_to_copy="$SOURCE_ATT/$extracted_file"
file_dest="$DEST_ATT/$extracted_file"
# Check if the file exists
if [[ -f "$file_to_copy" ]]; then
# Copy the file to the destination folder
cp -p "$file_to_copy" "$DEST_ATT"
echo "Copied $file_to_copy to $DEST_ATT"
else
echo "Warning: File $file_to_copy not found."
fi
done
done