<%pre>

#include <exception.h>
#include <tools.h>
#include <recman.h>

using namespace vdrlive;

</%pre>
<%args>
  int r[];
  time_t vdr_start = 0;
  time_t recordings_tree_creation = 0;
</%args>
<%cpp>
  reply.setContentType("text/javascript");
  bool error = false;
  if (vdr_start != 0 && vdr_start != LiveSetup().GetVdrStart()) {
    esyslog("live ERROR, get_recordings.ecpp, vdr restarted");
    error = true;
  }
  </%cpp>
vdr_restart = <$ error?"true":"false"$>;
  <%cpp>
  if (!error) {
    bool recordings_tree_re_created = false;
    RecordingsTreePtr recordingsTree(RecordingsManager::GetRecordingsTree());
    if (recordings_tree_creation != 0 && recordings_tree_creation != RecordingsManager::GetLastRecordingsUpdate() ) {
      dsyslog2("get_recordings.ecpp, recordings tree re-created");
      recordings_tree_re_created = true;
    }
    </%cpp>
recordings_tree_re_created = <$ recordings_tree_re_created?"true":"false"$>;
    <%cpp>
/*
    esyslog("live, get_recordings.ecpp, qparam as url = %s", qparam.getUrl().c_str() );
    if(qparam.paramcount())
      esyslog("live, get_recordings.ecpp, qparam[0] = %s", qparam[0].c_str() );

    esyslog("live, get_recordings.ecpp, r size = %d", (int)r.size());
    if (r.size() > 0)
      esyslog("live, get_recordings.ecpp, r[0] = %d", r[0]);
*/

    cToSvConcat<0> recording_item;
    recording_item.reserve(5000);
    size_t rec_size = RecordingsManager::all_recordings[(int)(RecordingsManager::eSortOrder::id)].size();
    for (int recording: r) {
      RecordingsItemRec *rPtr = nullptr;
      if ((size_t)recording < rec_size) rPtr = RecordingsManager::all_recordings[(int)(RecordingsManager::eSortOrder::id)][recording];
      if (rPtr) {
//      dsyslog("live, get_recordings.ecpp, write recording %d, rPtr->Id() %d", recording, rPtr->Id() );
        recording_item.clear();
        rPtr->AppendAsJSArray(recording_item);
        </%cpp>
recs[<$$cToSvInt(rPtr->Id())$>]=[<$$ recording_item $>]
        <%cpp>
      } else esyslog("live ERROR, get_recordings.ecpp, recording %i not found", recording);
    }
  }
</%cpp>
