#!/usr/bin/bash

#read -p "Enter the starting number (n): " n

n=0

while true; do

echo "" > /var/www/kroobot.net/liquidsoap/play03.txt
    
filesx=(/var/www/kroobot.net/station02/ *.mp3 ) 

files=( $(printf '%s\n' "${filesx[@]}" | sort -r) )


for f in "${files[@]:n}"; do 

echo $f > /var/www/kroobot.net/liquidsoap/play03.txt

echo "$(date +'%Y-%m-%d %H:%M:%S')"  > /var/www/kroobot.net/liquidsoap/time03.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 $f -c:a libmp3lame -b:a 128k -ar 44100 -ac 2 -f mp3 -content_type 'audio/mpeg'  'icecast://source:hackme@localhost:8080/station03_auto'

echo "" > /var/www/kroobot.net/liquidsoap/time03.txt

#sleep 60

done

n=0
  
done