-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.sh
executable file
·111 lines (96 loc) · 3.64 KB
/
flags.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
#!/bin/bash
# Define a variable for verbose mode
VERBOSE=false
# Process -v/--verbose and -h/--help options
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-v | --verbose )
VERBOSE=true
;;
-h | --help )
echo "📋 Usage: $0 [OPTIONS]"
echo
echo " This script processes flag files in a directory, extracts information,"
echo " executes the appropriate commands based on the extracted data, and stores"
echo " the results in a log file. It ensures that only recent files are retained"
echo " and older files are deleted."
echo
echo "Available options:"
echo " -v, --verbose Enable verbose mode to display detailed steps."
echo " -h, --help Display this help message."
echo
echo "Recognized Flag Types:"
echo " - presences-mac: Processes presence data based on MAC addresses."
echo " - presences-daterange: Processes presence data for a specified date range."
echo " - presences-day: Processes presence data the current day."
echo
echo "Examples:"
echo " $0 Run the script in silent mode."
echo " $0 --verbose Run the script with detailed messages."
exit 0
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
BASE_DIR=$(realpath "$(dirname "$0")")
source "${BASE_DIR}/.env"
source "${BASE_DIR}/lib/functions.sh"
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "❌ Error: jq is not installed. Please install jq to run this script."
exit 1
fi
# Retain only the 50 most recent files by date
# $VERBOSE && echo "🗑️ Deleting old flags"
find "$FLAGS_DIR" -type f | sort -r | tail -n +51 | while read OLD_FILE; do
rm -f "$OLD_FILE"
done
# Iterate over all files in the directory
for FILE in "$FLAGS_DIR"/*; do
# Check if it's a file
if [ -f "$FILE" ]; then
if [[ "$FILE" == *.* ]]; then
continue
fi
STREAM_FILE="${FILE}.stream"
RESPONSE_FILE="${FILE}.response"
if [ -f "$STREAM_FILE" ]; then
$VERBOSE && echo "⚡ Flag $FILE is already being processed."
continue
fi
if [ -f "$RESPONSE_FILE" ]; then
$VERBOSE && echo "💤 Flag $FILE has already been processed."
continue
fi
# Remove null bytes and process the file content
JSON_CONTENT=$(cat "$FILE")
# Check if the JSON file is empty
if [ -z "$JSON_CONTENT" ]; then
$VERBOSE && echo "❌ File $FILE is empty or invalid."
continue
fi
# Extract the value of content.slug
SLUG=$(echo "$JSON_CONTENT" | jq -r '.slug' 2>/dev/null)
if [ -z "$SLUG" ] || [ "$SLUG" == "null" ]; then
$VERBOSE && echo "❌ The slug for file $FILE is empty or invalid."
continue
fi
# Create a file named [filename].stream
touch "$STREAM_FILE"
if [ "$SLUG" == "presences-mac" ]; then
CMD=$(process_presences_mac "$JSON_CONTENT")
elif [ "$SLUG" == "presences-daterange" ]; then
CMD=$(process_presences_daterange "$JSON_CONTENT")
elif [ "$SLUG" == "presences-day" ]; then
CMD=$(process_presences_day)
else
$VERBOSE && echo "❌ Unknown command: $SLUG."
fi
if [ -z "$CMD" ]; then
$VERBOSE && echo "❌ Unknown command: $SLUG."
else
echo "⏱️ Processing $SLUG"
$CMD >> "$STREAM_FILE"
mv "$STREAM_FILE" "$RESPONSE_FILE"
echo "✅ Processing complete - Log stored in $RESPONSE_FILE"
fi
fi
done