#!/bin/bash

################
# Variables & Examples
server_host="localhost"
server_port="8000"
server_mountpoint="test.ogg"
server_user="source"
server_password="hackme"
server_name="Refugees Emancipation Radio"
server_description="Worldwide Radio for Refugees"
server_genre="Voice of Refugees VOR"
server_url="http://www.refugeesemancipation.com"
s_quality="5.0"
stream_artist="Refugees Emancipation Radio"
stream_album="www.refugeesemancipation.com"
stream_genre="Voice of Refugees VOR"
################

BASE_DIR=$(pwd)

function get_data {
echo "Please enter the server-host"
echo "Example: $server_host"
read server_host
echo "Please enter the server-port"
echo "Example: $server_port"
read server_port
echo "Please enter the server-mountpoint"
echo "Example: $server_mountpoint"
read server_mountpoint
echo "Please enter the server-username"
echo "Example: $server_user"
read server_user
echo "Please enter the server-password"
echo "Example: $server_password"
read server_password
echo "Please enter a stream title for the server"
echo "Example: $server_name"
read server_name
echo "Please enter a stream description for the server"
echo "Example: $server_description"
read server_description
echo "Please enter a stream genre for the server"
echo "Example: $server_genre"
read server_genre
echo "Please enter a stream url for the server"
echo "Example: $server_url"
read server_url
echo "Please enter the quality of the stream"
echo "Example: $s_quality"
read s_quality
echo "Please enter an artist for the stream"
echo "Example: $stream_artist"
read stream_artist
echo "Please enter an album for the stream"
echo "Example: $stream_album"
read stream_album
echo "Please enter a genre for the stream"
echo "Example: $stream_genre"
read stream_genre
echo ""
}

function create_script {
if [ ! -d $BASE_DIR/liq ]; then
  mkdir $BASE_DIR/liq || exit 1
fi
if [ ! -e $BASE_DIR/liq/livestream.liq ]; then
get_data
  cat > $BASE_DIR/liq/livestream.liq << -EOF-
#!/usr/bin/liquidsoap

### global settings
set("frame.channels",2)
set("log.file",true)
set("log.file.path","$BASE_DIR/liq/livestream.log")
set("log.stdout",true)

### jack input with the same number of channels than the global setting above
liqstream=mksafe(input.jack(id="liqstream"))

### add metadata to liqstream

liqstream=rewrite_metadata([("title", "tee"), ("album", "$stream_album"), ("artist", "$stream_artist"), ("genre", "$stream_genre"), ("date", "2009")], liqstream)

### output liqstream
output.icecast.vorbis(samplerate=44100, stereo=true, restart=true, restart_delay=1, mount="$server_mountpoint", name="$server_name", host="$server_host", port=$server_port, user="$server_user", password="$server_password", genre="$server_genre", url="$server_url", description="$server_description", quality=$s_quality, liqstream)

-EOF-
fi
chmod 700 $BASE_DIR/liq/livestream.liq
}

function set_title_date {
echo "enter a stream-title and press <enter> to start the livestream (no \" allowed)"
read stream_title
if [ -z "$stream_title" ]; then
  stream_title="$(date +'%F') live"
fi
sed -e "s#\((\"title\", \"\).*\(\"), (\"album\"\)#\1${stream_title}\2#g" -i $BASE_DIR/liq/livestream.liq
sed -e "s#\((\"date\", \"\).*\(\")], liqstream)\)#\1$(date +'%Y')\2#g" -i $BASE_DIR/liq/livestream.liq
}


function start_livestream {
echo "starting livestream using title:"
echo "${stream_title}"
echo "press Ctrl + c to quit livestreaming"
$BASE_DIR/liq/livestream.liq
}

if [ ! -x "$BASE_DIR/liq/livestream.liq" ]; then
  create_script
fi
set_title_date
start_livestream
