/*
 * menu_syslog.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include "viewer.h"
#include "menu_syslog.h"
#include "tools.h"
#include <vdr/interface.h>
#include <vdr/menu.h>
#include <vdr/plugin.h>
#include <vdr/menu.h>

// --- cMenuViewerSyslogItem ------------------------------------------------------
cMenuViewerSyslogItem::cMenuViewerSyslogItem(const char *name, const char *text)
{
  szName=strdup(name);
  szFile=strdup(text);
  Set();
}

cMenuViewerSyslogItem::~cMenuViewerSyslogItem()
{
  free(szName);
  free(szFile);
}

void cMenuViewerSyslogItem::Set(void)
{
  char *buffer = NULL;
  char *buf2 = NULL;
  char bytes[256];
  int ret;
  struct stat fileinfo; // Holds file information structure

  if ((ret=stat(szFile, &fileinfo)) != -1) {
     FormatThousands(bytes, fileinfo.st_size);
     asprintf(&buf2, "(%s Bytes)", bytes);
  } else {
     asprintf(&buf2, tr("(no access)"));
  }
  asprintf(&buffer, "%s:\t%s %s", szName, szFile, buf2);
  free(buf2);
  SetText(buffer, false);
}

char *cMenuViewerSyslogItem::GetFile()
{
  return szFile;
}

int cMenuViewerSyslogItem::Compare(const cListObject &ListObject) const
{
  cMenuViewerSyslogItem *p = (cMenuViewerSyslogItem *)&ListObject;
  char* s1 = NULL;
  char* s2 = NULL;
  asprintf(&s1, "%s:\t%s", szName, szFile);
  asprintf(&s2, "%s:\t%s", p->szName, p->szFile);
  int res = strcasecmp(s1, s2);
  free(s1);
  free(s2);
  return res;
}

// --- cMenuViewerSyslog ---------------------------------------------------------
cMenuViewerSyslog::cMenuViewerSyslog(const char *Title, char *filename, char *syslog, bool *bSubMenuOK)
:cOsdMenu(Title)
{
  Filename=filename;
  Syslog=syslog;
  ParseSyslogConfig();
  bSubMenuOk=bSubMenuOK;
}

cMenuViewerSyslog::~cMenuViewerSyslog()
{
}

bool cMenuViewerSyslog::ParseSyslogConfig()
{
  char *cmd = NULL;
  Clear();
  char destination[2048];
  char name[2048];
  char file[2048];
  char rest[2048];
  int ret;
  uint len=10;

// grep '^destination' /etc/syslog-ng/syslog-ng.conf
// destination newscrit { file("/var/log/news/news.crit"); };
// destination messages { file("/var/log/messages"); };

  asprintf(&cmd, "grep '^destination' %s", Syslog);
  FILE *p = popen(cmd, "r");
  if (p) {
     char *s;
     cReadLine ReadLine;
     while ((s = ReadLine.Read(p)) != NULL) {
        ret=sscanf(s, "%s %s { file(\"%s\");%s", destination, name, file, rest);
// ret=3, d=destination, n=mail, f=/var/log/mail");, r=
        if(!strncmp(destination, "destination", strlen("destination"))) {
           char *p=strchr(file, '\"');
           if(p && *p=='\"')
              *p='\0';
           len=max(len, strlen(name)+2);
           Add(new cMenuViewerSyslogItem(name, file));
        }
     }
  }
  SetCols(len);
  pclose(p);
  free(cmd);
  Display();
  return true;
}

eOSState cMenuViewerSyslog::ProcessKey(eKeys Key)
{
  eOSState state = cOsdMenu::ProcessKey(Key);

  if (state == osUnknown) {
     switch (Key) {
        case kGreen:
        case kYellow:
        case kBlue:
                   return osContinue;
        case kOk:  if (Count() > 0) {
                      cMenuViewerSyslogItem* p=(cMenuViewerSyslogItem *)Get(Current());
                      strn0cpy(Filename, p->GetFile(), MaxFileName);
                      *bSubMenuOk=true;
                   }
		   return osBack;
        default:   break;
     }
  }
  return state;
}

