-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen-lab.sh
79 lines (67 loc) · 2.6 KB
/
gen-lab.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
#!/bin/bash
# Check if the current working directory ends with "sem"
if [[ "$(pwd)" != *"sem" ]]; then
echo "Run from Semester folder"
exit 1
fi
# Define the template file path
TEMPLATE_FILE="../../../../helper-scripts/templates/temp-assignment.md"
TMP_FILE=$(mktemp)
UNIQUE_FILE=$(mktemp)
FINAL_FILE="lab.md"
# Cleanup function to remove temporary files
cleanup() {
rm -f "$TMP_FILE" "$UNIQUE_FILE"
}
trap cleanup EXIT
# Check if FINAL_FILE exists
if [[ -f "$FINAL_FILE" ]]; then
echo "Final file exists. Checking for existing entries..."
else
echo "Final file does not exist. It will be created."
fi
# Loop through all directories in the current working directory
for folder in */; do
# Check for index.md in the subject folder
if [[ -f "${folder}lab/index.md" ]]; then
# Read the content of index.md
content=$(cat "${folder}lab/index.md")
# Check if the content starts with frontmatter
if [[ "$(head -n 1 <<< "$content")" == "---" ]]; then
# Find the second occurrence of "---"
second_occurrence=$(awk '/---/{c++} c==2{print NR; exit}' <<< "$content")
# If a second occurrence is found, skip the frontmatter
if [[ -n "$second_occurrence" ]]; then
start_line=$((second_occurrence + 1))
# Extract content after frontmatter
content=$(tail -n "+$start_line" <<< "$content")
fi
fi
# Extract the subject name from the first line of index.md
subject_name=$(sed -n 's/^# //p' "${folder}lab/index.md" | head -n 1 | sed 's/[[:space:]]*$//') # Remove leading # and trailing whitespace
echo "Processing ${subject_name}"
# Generate the new entry
new_entry="- [${subject_name}](${folder}lab/index.md)"
# Check if the entry already exists in FINAL_FILE
if ! grep -Fxq -- "$new_entry" "$FINAL_FILE" 2>/dev/null; then
echo "$new_entry" >> "$TMP_FILE"
else
echo "$new_entry already exists in $FINAL_FILE. Skipping..."
fi
fi
done
# If there are new entries, process them for uniqueness
if [[ -s "$TMP_FILE" ]]; then
# Check for unique entries and write them to UNIQUE_FILE
while IFS= read -r line; do
echo "$line" >> "$UNIQUE_FILE"
done < "$TMP_FILE"
# If UNIQUE_FILE has entries, append them to FINAL_FILE
if [[ -s "$UNIQUE_FILE" ]]; then
# If FINAL_FILE doesn't exist, copy TEMPLATE_FILE to FINAL_FILE
if [[ ! -f "$FINAL_FILE" ]]; then
cp "$TEMPLATE_FILE" "$FINAL_FILE"
fi
cat "$UNIQUE_FILE" >> "$FINAL_FILE"
fi
fi