#!/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) =
    filename = meta["filename"]
    track_duration = request.duration(filename)

    # TODO Post playlog to engine-api directly. This requires some additional show state handling.
    json_data = json()
    json_data.add("action", "on_metadata")
    json_data.add("meta", meta)
    json_data.add("track_duration", track_duration)
    playlog = url.encode(json.stringify(json_data))

    log(level=3, "Posting playlog: #{playlog}")
    # headers = [("Content-Type","application/json; charset=utf-8")]
    # result = http.post(http_version="1.1", headers=headers, data="#{playlog}", "#{engine_control}")
    ignore(process.run("curl -X POST -H 'Content-Type: application/json' --data '#{playlog}' #{engine_control}"))

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"