#!/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)
    json_data = json_of(meta)
    json_data = '{ "action": "on_metadata", "data": #{json_data}, "track_duration": "#{track_duration}" }'
    # There's currently an issue with Liquidsoap http.post requests:
    # headers = [("Content-Type","application/json; charset=utf-8")]
    # ignore(http.post(headers=headers, data="#{json_data}", "http://#{engine_control}"))
    ignore(system("curl -X POST -H 'Content-Type: application/json' --data '#{json_data}' #{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               #
#####################################


# When some regular playout is happening and it is returned to the fallback,
# we don't want to resume the previous fallback track:
def on_track_change(s) =
    source.skip(station_playlist)
    source.skip(station_folder)
end


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

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

stripped_stream = on_track(on_track_change, stripped_stream)

# Scheduled fallback is not used, since we replaced it with "default playlists":

# fallback_one = fallback(
#     id="fallback-scheduled",
#     track_sensitive=false,
#     replay_metadata=true,
#     [ stripped_stream, stripped_fallback_mixer])

# fallback_one = on_track(on_track_change, fallback_one)

fallback_station_playlist = fallback(
    id="fallback-station-playlist",
    track_sensitive=false,
    replay_metadata=true,
    [ stripped_stream, station_playlist])

station_folder = mksafe(
    playlist(id="station_folder",
    fallback_station_dir,
    mode="randomize",
    reload=fallback_station_dir_reload,
    reload_mode="seconds"))

fallback_station_folder = fallback(
    id="fallback-station-folder",
    track_sensitive=false,
    replay_metadata=true,
    [ fallback_station_playlist, station_folder])

output_source = fallback_station_folder

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

# create soundcard output
%include "out_soundcard.liq"

# stream output
%include "out_stream.liq"

# enable socket functions
%include "serverfunctions.liq"