#!/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 library functions
%include "library.liq"

# Include dependency-free functions
%include "functions.liq"

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

# Called when some new metadata info is available
def on_metadata_notification(meta) =
    show_name = ref(list.assoc(default="", "show_name", meta))
    show_id = ref(list.assoc(default="-1", "show_id", meta))
    timeslot_id = list.assoc(default="-1", "timeslot_id", meta)
    playlist_id = list.assoc(default="-1", "playlist_id", meta)
    playlist_track_num = list.assoc(default="", "playlist_item", meta)
    track_duration = int_of_float(request.duration(meta["filename"]))
    track_type = (eval_track_type(meta["track_type"], meta["source"]))
    track_start = meta["on_air"]
    track_artist = meta["artist"]
    track_album = meta["album"]
    track_title = meta["title"]

    source_type = eval_source_type(meta["source"])
    if source_type == "fallback" then
        log("Detected FALLBACK playing (Show ID: #{!engine_fallback_show_id})")
        show_name := !engine_fallback_show_name
        show_id := !engine_fallback_show_id
    elsif source_type == "queue" then
        log("ERROR in metadata handling: Invalid source type #{source_type}")
    end

    playlog = [
        ("log_source", !engine_id),
        ("show_name", !show_name),
        ("show_id", !show_id),
        ("timeslot_id", timeslot_id),
        ("playlist_id", playlist_id),
        ("track_type", track_type),
        ("track_start", track_start),
        ("track_duration", "#{track_duration}"),
        ("track_title", track_title),
        ("track_album", track_album),
        ("track_artist", track_artist),
        ("track_num", playlist_track_num)
    ]
    post_playlog(!engine_api_playlog, playlog)
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"