#!/usr/bin/liquidsoap

#
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2017-2020 - The Aura Engine Team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



icecast_vorbis_metadata = false
inputs = ref ([])


# Load settings from ini file
%include "settings.liq"

# Include some functions
%include "library.liq"


#####################################
#              EVENTS               #
#####################################

# Called when some new metadata info is available
def on_metadata_notification(meta) =
    show_name = list.assoc(default=!engine_current_show_name, "current_show_name", meta)
    show_id = list.assoc(default=!engine_current_show_id, "current_show_id", meta)
    timeslot_id = list.assoc(default=!engine_current_timeslot_id, "current_timeslot_id", meta)
    playlist_id = list.assoc(default=!engine_current_playlist_id, "current_playlist_id", meta)
    playlist_track_num = list.assoc(default="", "current_playlist_item", meta)
    track_type = (eval_track_type(meta))
    track_duration = int_of_float(request.duration(meta["filename"]))

    json_data = json()
    json_data.add("track_start", meta["on_air"])
    json_data.add("track_duration", track_duration)
    json_data.add("track_title", meta["title"])
    json_data.add("track_album", meta["album"])
    json_data.add("track_artist", meta["artist"])
    json_data.add("track_num", int_of_string(playlist_track_num))
    json_data.add("track_type", int_of_string(track_type))
    json_data.add("show_name", show_name)
    json_data.add("show_id", int_of_string(show_id))
    json_data.add("timeslot_id", int_of_string(timeslot_id))
    json_data.add("playlist_id", int_of_string(playlist_id))
    json_data.add("log_source", int_of_string(!engine_id))
    playlog = json.stringify(json_data)
    log("Posting playlog to '#{!engine_api_playlog}': #{playlog}")

    headers = [("Content-Type","application/json")]
    result = http.post(headers=headers, data="#{playlog}", "#{!engine_api_playlog}")

    if result.status_code < 400 then
        log("Successfully posted playlog to Engine API.")
    else
        log("ERROR during playlog POST: #{result.status_code} | #{result.status_message}")
    end
end


#####################################
#              INPUTS               #
#####################################

# Enable queue sources
%include "in_queue.liq"

# Enable fallback sources
%include "in_fallback.liq"

# Enable stream overtakes
%include "in_stream.liq"

# Enabled line in from soundcard
%include "in_soundcard.liq"


#####################################
#             ROUTING               #
#####################################

mixer = mix(id="mixer",
        list.append(
            [
                input_filesystem_0,
                input_filesystem_1,
                input_http_0,
                input_http_1
            ],
            !inputs
        )
    )

stripped_stream = blank.strip(
        id="stripped_stream",
        track_sensitive=false,
        max_blank=fallback_max_blank,
        min_noise=fallback_min_noise,
        threshold=fallback_threshold,
        mixer
    )

output_source = attach_fallback_source(stripped_stream)


#####################################
#             OUTPUTS               #
#####################################

# create soundcard output
%include "out_soundcard.liq"

# stream output
%include "out_stream.liq"

# enable socket functions
%include "serverfunctions.liq"