Commit 3d93be34 authored by Vitaly Lipatov's avatar Vitaly Lipatov

radio.sh: rewrite with socket support

parent 593b1c22
#!/bin/bash
export LC_ALL=ru_RU.UTF-8
# get current port
source ./.port
ROOT_DIRECTORY=./$2
ROOT_DIRECTORY=$(pwd)
fatal()
{
echo "$*" >&2
exit 1
}
function usage() {
echo "Использование : ./radio.sh <параметр> <имя>
......@@ -9,55 +19,65 @@ function usage() {
-m - управление потоком
-d - удаление потока
-l - список запущенных потоков
-p - порт потока"
-r - перезапустить поток
"
}
function get_port() {
local CONFIG_FILE=./$1.mpd.conf
local PORT=`cat $CONFIG_FILE | grep port | awk '{ print $2 ; exit; }' | tr -d \"`
echo $PORT
elem_control()
{
local conf
local bind
conf=$ROOT_DIRECTORY/$1.mpd.conf
[ -s "$conf" ] || fatal "$conf is missed"
# if socket
bind=`cat $conf | grep "^bind_to_address" | awk '{ print $2 ; exit; }' | tr -d \"`
[ -e "$bind" ] && echo "--host $bind" && return
# if port
bind=`cat $conf | grep "^port" | awk '{ print $2 ; exit; }' | tr -d \"`
echo "-p $bind"
}
function create_stream() {
local CONFIG_FILE=./$1.mpd.conf
local ROOT_DIRECTORY=$2
local ABS_PATH_TO_CFG=`pwd`/$1.mpd.conf
local STREAM_DIRECTORY=$ROOT_DIRECTORY/$1
local ABS_PATH_TO_CFG=$STREAM_DIRECTORY/$1.mpd.conf
if [[ ! -f ./$1.mpd.conf ]];then
cp -p ./template.mpd.conf $CONFIG_FILE
mkdir -p $ROOT_DIRECTORY/playlists
mkdir $ROOT_DIRECTORY/Music
touch $ROOT_DIRECTORY/mpd.log
touch $ROOT_DIRECTORY/mpdstate
touch $ROOT_DIRECTORY/mpd.db
touch $ROOT_DIRECTORY/mpd.pid
if [[ ! -f $ABS_PATH_TO_CFG ]];then
cp -p ./template.mpd.conf $ABS_PATH_TO_CFG
mkdir -p $STREAM_DIRECTORY/playlists
#mkdir $STREAM_DIRECTORY/Music
touch $STREAM_DIRECTORY/mpd.log
touch $STREAM_DIRECTORY/mpdstate
touch $STREAM_DIRECTORY/mpd.db
touch $STREAM_DIRECTORY/mpd.pid
sed -i 's?#1?'`pwd`/$1'?' ./$1.mpd.conf
sed -i "s/#2/$PORT/g" ./$1.mpd.conf
sed -i "s/#3/$1/g" ./$1.mpd.conf
chown -R icecastradio $ROOT_DIRECTORY
chgrp -R icecastradio $ROOT_DIRECTORY
chmod -R 777 $ROOT_DIRECTORY
chmod +x $ROOT_DIRECTORY/playlists
chown -R icecastradio $STREAM_DIRECTORY
chgrp -R icecastradio $STREAM_DIRECTORY
chmod -R 777 $STREAM_DIRECTORY
chmod +x $STREAM_DIRECTORY/playlists
# Systemd configuration
cp template.service /etc/systemd/system/$1.service
sed -i 's?#1?'$ABS_PATH_TO_CFG'?' /etc/systemd/system/$1.service
echo PORT=$((PORT+1)) > ./.port
serv daemon-reload
serv start $1
serv enable /etc/systemd/system/$1.service
serv $1 on
# Enable playlist repeat
mpc -q -p `cat $CONFIG_FILE | grep port | awk '{ print $2 ; exit; }' | tr -d \"` repeat on
mpc -q $(elem_control $STREAM_NAME) repeat on
echo "Файлы конфигураций cозданы успешно !"
echo "Запущен mpd-поток под названием : $1 по адресу : radio.azbyka.ru:8000/$1"
echo "Для начала работы требуется обновить базу данных mpd"
echo "Для работа с потоком : ./radio -m $1"
echo "Запущен mpd-поток под названием : $1 по адресу : radio.azbyka.ru:80/$1"
#echo "Для начала работы требуется обновить базу данных mpd"
echo "Для работы с потоком : ./radio -m $1"
else
echo -e "Поток \"$1\" уже создан.\nДля работы с ним : ./radio -m $1"
fi
......@@ -65,48 +85,58 @@ function create_stream() {
}
function manage_stream() {
local STREAM_NAME=$2
ncmpc -p `$1 $STREAM_NAME`
local STREAM_NAME=$1
echo "ncmpc $(elem_control $STREAM_NAME)"
ncmpc $(elem_control $STREAM_NAME)
}
function delete_stream(){
systemctl stop $1
rm -drf ./$1*
rm -f /etc/systemd/system/$1.service
serv daemon-reload
serv stop $1
rm -rvf ./$1*
sudo rm -f /etc/systemd/system/$1.service
sudo systemctl daemon-reload
}
function restart_stream(){
mpc $(elem_control $1) update
serv restart $1
}
function get_streams() {
URL=http://radio.azbyka.ru/json.xsl
URL=http://radio2.azbyka.ru/json.xsl
CONTENT=$(curl $URL 2>&1 /dev/null)
ACTIVE_STREAMS=`grep -oP '(?<=/).*?(?=")' <<< $CONTENT`
echo "Список запущенных потоков:"
for ELEM in $ACTIVE_STREAMS;
do
local ELEM_STATUS=`mpc -p \`./radio.sh -p $ELEM\` | awk '{print $1}' | sed -n 2p`
ELEM_STATUS=$(mpc $(elem_control $ELEM) | awk '{print $1}' | sed -n 2p)
echo -e "$ELEM\t$ELEM_STATUS"
done
echo
}
while getopts "c:m:d:lhp:" opt
while getopts "c:m:d:lhr:" opt
do
case "${opt}"
in
c) create_stream $2 $ROOT_DIRECTORY;;
m) manage_stream get_port $2;;
d) delete_stream $2;;
l) get_streams;;
h) usage $ROOT_DIRECTORY;;
p) get_port $2;;
*) usage $ROOT_DIRECTORY;;
c) create_stream $2 ;;
m) manage_stream $2 ;;
d) delete_stream $2 ;;
l) get_streams ;;
r) restart_stream $2 ;;
h) usage ;;
p) elem_control $2 ;;
*) usage ;;
esac
exit
done
usage
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment