#!/usr/bin/env bash set -euo pipefail echo "--- AutoQueue started ---" while true; do inotifywait -e close_write -m "${WATCH_DIR}" | while read -r path action file; do TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') echo "[$TIMESTAMP] Detected new file: $file" | tee -a "${LOG_FILE}" ATTEMPTS=0 MEDIA_ID="" while [ -z "$MEDIA_ID" ] && [ $ATTEMPTS -lt 6 ]; do ATTEMPTS=$((ATTEMPTS + 1)) sleep 5 MEDIA_ID=$(curl -s -H "Authorization: Bearer ${AZURACAST_API_KEY}" "${AZURACAST_API_URL}/files" | jq -r --arg NAME "${SUBDIR}/$file" '.[] | select(.path | endswith($NAME)) | .id') if [ -z "$MEDIA_ID" ]; then echo "[$TIMESTAMP] Attempt $ATTEMPTS: File $file not yet indexed" | tee -a "${LOG_FILE}" fi done if [ -n "$MEDIA_ID" ]; then echo "[$TIMESTAMP] Queuing $file (ID $MEDIA_ID) as next..." | tee -a "${LOG_FILE}" if curl -s -X POST -H "Content-Type: application/json" \ -H "Authorization: Bearer ${AZURACAST_API_KEY}" \ -d "{\"track_id\": \"$MEDIA_ID\", \"play_next\": true}" \ "${AZURACAST_API_URL}/requests"; then echo "[$TIMESTAMP] Successfully queued $file" | tee -a "${LOG_FILE}" else echo "[$TIMESTAMP] Failed to queue $file" | tee -a "${LOG_FILE}" fi else echo "[$TIMESTAMP] Gave up after $ATTEMPTS attempts: $file not indexed" | tee -a "${LOG_FILE}" fi done done