#!/bin/sh
# test-source.sh — source de test pour la webradio « skylac ».
# Pousse un MP3 en boucle vers un mountpoint Icecast, au format cible
# (MP3 mono 48 kbps). Mêmes conditions que RTLSDR-Airband.
#
# Usage :
#   test-source.sh <mount> <fichier.mp3>
#   ex. test-source.sh /lflp-tour.mp3 "/stock/.../Thunderstruck.mp3"
#       test-source.sh /lflp-sol.mp3  "/stock/.../Number Of The Beast.mp3"
#
# Lançable depuis against / un PC / le Pi (ICECAST_HOST = domaine de donald),
# ou depuis donald (ICECAST_HOST=127.0.0.1).
# Le mot de passe source est lu depuis un fichier (jamais en dur ni en argv).

ICECAST_HOST="skylac.unix-scripts.org"
ICECAST_PORT="8000"
PASS_FILE="${HOME}/skylac-source-pass"
BITRATE="48k"

MOUNT="$1"
AUDIO_FILE="$2"

if [ -z "$MOUNT" ] || [ -z "$AUDIO_FILE" ]; then
    echo "Usage : $0 <mount> <fichier.mp3>" >&2
    echo "  ex. $0 /lflp-tour.mp3 \"/stock/.../Thunderstruck.mp3\"" >&2
    exit 1
fi
if [ ! -r "$PASS_FILE" ]; then
    echo "Erreur : mot de passe source introuvable : $PASS_FILE (chmod 600)" >&2
    exit 1
fi
if [ ! -r "$AUDIO_FILE" ]; then
    echo "Erreur : fichier audio introuvable : $AUDIO_FILE" >&2
    exit 1
fi
SOURCE_PASS=$(head -n1 "$PASS_FILE")

echo "Push '$AUDIO_FILE' -> icecast://source@${ICECAST_HOST}:${ICECAST_PORT}${MOUNT} (MP3 mono ${BITRATE})"
echo "Ctrl-C pour arrêter."

# -nostdin : indispensable en arrière-plan (nohup &), sinon SIGTTIN → « Stopped ».
exec ffmpeg -hide_banner -loglevel warning -nostdin \
    -re -stream_loop -1 -i "$AUDIO_FILE" \
    -vn -c:a libmp3lame -b:a "$BITRATE" -ac 1 -ar 44100 \
    -f mp3 -content_type audio/mpeg \
    "icecast://source:${SOURCE_PASS}@${ICECAST_HOST}:${ICECAST_PORT}${MOUNT}"
