#!/usr/bin/bash
  
/usr/bin/ffmpeg -stream_loop -1 -re -i /var/www/kroobot.net/liquidsoap/willyOwind.mp3 -c:a libmp3lame -b:a 128k  -content_type audio/mpeg -f mp3  icecast://source:hackme@localhost:8000/offair > ffmpeg.log 2>&1 &

while true; do

php /var/www/kroobot.net/liquidsoap/03mp3.php

echo "" > /var/www/kroobot.net/liquidsoap/03play.txt
    
TARGET_DIR="/var/www/kroobot.net/station03"

files=("$TARGET_DIR"/*.mp3)
#sorted_files=$'\n' files=( $(printf '%s\n' "${files[@]}" | sort -r) )
for file in "${files[@]}"; do

echo $file > /var/www/kroobot.net/liquidsoap/03play.txt

echo "$(date +'%Y-%m-%d %H:%M:%S')"  > /var/www/kroobot.net/liquidsoap/03time.txt

# Get the current minute 
current_minute=$(date +%-M)

# Determine the remaining minutes until the next top or half hour
if [ "$current_minute" -lt 15 ]; then
    minutes_to_wait=$((15 - current_minute))
elif [ "$current_minute" -lt 30 ]; then
    minutes_to_wait=$((30 - current_minute))
 elif [ "$current_minute" -lt 45 ]; then
    minutes_to_wait=$((45 - current_minute))
else
    minutes_to_wait=$((60 - current_minute))
fi

# Convert to seconds and wait (subtracting current seconds for exactness)
seconds_to_wait=$((minutes_to_wait * 60 - $(date +%-S)))  
 
# Sleep until the calculated time
#echo "Sleeping for $seconds_to_wait seconds..."
sleep "$seconds_to_wait"

/usr/bin/ffmpeg -re -i $file -c:a libmp3lame -b:a 128k  -content_type audio/mpeg -f mp3  'icecast://source:hackme@localhost:8000/station03' 

echo "" > /var/www/kroobot.net/liquidsoap/03time.txt

done
  
done