#
# 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/>.

#####################################
#         DYNAMIC SOURCES           #
#####################################

def create_dynamic_source(~skip=true, name) =
  log(
    "Creating dynamic source '#{name}' (skip:#{skip})"
  )
  track =
    process.read.lines(
      "cat " ^
        string.quote("next-track.txt")
    )
  track = list.hd(default="", track)
  dyn_source = request.dynamic.list({[request.create(track)]})
  dyn_source
end

#####################
# stream to icecast #
#####################

# FIXME: this function is deprecated and could be removed?
def stream_to_icecast(
  id,
  encoding,
  bitrate,
  host,
  port,
  pass,
  mount_point,
  url,
  description,
  genre,
  user,
  stream,
  streamnumber,
  connected,
  name,
  channels
) =
  source = ref(stream)
  def on_error(msg) =
    connected := "false"
    log(msg)
    5.
  end
  def on_connect() =
    connected := "true"
    log(
      "Successfully connected to stream_#{streamnumber}"
    )
  end

  user_ref = ref(user)
  if user == "" then user_ref := "source" end

  snd_icy_metadata = ref(false)

  enc = ref(%vorbis(stereo = true))

  # TODO: move this into out_stream.liq?
  # if encoding == "ogg" then%include "outgoing_streams/ogg.liq" end
  # https://github.com/savonet/liquidsoap/pull/1858
  if
    encoding == "mp3"
  then
    enc := %mp3(stereo = true)
    snd_icy_metadata := true
  end
  # if encoding == "ogg" then%include "outgoing_streams/ogg.liq" end

  log(
    "Icecast output format: #{encoding} #{bitrate} - #{enc()}"
  )

  output_icecast_stereo =
    output.icecast(
      id=id,
      host=host,
      port=port,
      password=pass,
      mount=mount_point,
      fallible=true,
      url=url,
      description=description,
      name=name,
      genre=genre,
      user=user_ref(),
      on_error=on_error,
      on_connect=on_connect,
      send_icy_metadata=snd_icy_metadata(),
      enc(),
      source()
    )

  # ignore(output_icecast_mono)
  ignore(output_icecast_stereo)
end

############
# line out #
############

def get_output(source, device, name) =
  if
    device != ""
  then
    if
      use_alsa == true
    then
      log(
        "--- Set ALSA Output ---"
      )
      output.alsa(
        id=name, device=device, bufferize=alsa_buffered_output(), source
      )
    elsif
      use_jack == true
    then
      log(
        "--- Set JACK AUDIO Output ---"
      )
      output.jack(id=name, source)
    else
      log(
        "--- Set PULSE AUDIO Output ---"
      )
      output.pulseaudio(
        id=name,
        client=
          "AuraEngine Line OUT",
        source
      )
    end
  else
    output_name = "#{name}_DUMMY"
    log(
      "Using dummy output: #{output_name}"
    )
    output.dummy(id=output_name, blank())
  end
end