#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <getopt.h>
#include <stdlib.h>
#include <vdr/interface.h>
#include <vdr/plugin.h>
#include <unistd.h>
#include "menu.h"
#include "config.h"
#include "menuitem.h"

#define ISOEXT       "*.ISO"
//#define FINDCMD      "cd '%s' && find '%s' -follow -type f -iname '%s' 2> /dev/null"
#define TSDIR        "video_ts"
#define FINDCMD      "cd '%s' && find '%s' -follow -iname '%s' -printf '%%p\n' -o -iname '%s' -printf '%%h\n' 2> /dev/null"

#define SCRIPT_DVDWRITE_CMD      "%s '%s'"
#define SCRIPT_DVDREAD_CMD      "%s '%s' '%s' '%s'"
#define RMCMD      "rm '%s' 2> /dev/null"
#define MOUNTCMD      "ln -nfs '%s' '%s' 2> /dev/null"
#define RESETCMD      "ln -nfs '%s' '%s' 2> /dev/null"


// --- cDVDList -------------------------------------------------------------
cDVDList::cDVDList(void) : cOsdMenu(tr("DVDSelect"), 12, 6)
{
  int i;
  char *cmd = NULL;
  cReadLine reader;
  
  SetHelp();
  
  //asprintf(&cmd, FINDCMD, DVDSelectSetup.ImageDir, DVDSelectSetup.ImageDir, ISOEXT,TSDIR);
  asprintf(&cmd, FINDCMD, DVDSelectSetup.ImageDir, DVDSelectSetup.ImageDir, DVDSelectSetup.IsoExt,TSDIR);
  FILE *p = popen(cmd, "r");
  if (p) {
    char *s;
    while ((s = reader.Read(p)) != NULL) {
      Add(new cMenuDVDItem(s,DVDSelectSetup.ImageDir), &i);
    }//while
    pclose(p);
    Sort();
  }//if;
  else
#if VDRVERSNUM >= 10307
    Skins.Message(mtError, "Error while opening pipe!");
#else
    Interface->Error("Error while opening pipe!");
#endif
  free(cmd);

}


cDVDList::~cDVDList()
{
}

eOSState cDVDList::ProcessKey(eKeys Key)
{
  eOSState state = cOsdMenu::ProcessKey(Key);
  
  if (state == osUnknown) {
    
    switch (Key) {
    case kGreen:
      //      return Mount();
      return DVDWriteScript();
      
    case k0:
      AddSubMenu(new cMenuReadDVD());
      break;

    case kOk:
    case kRed:
      return Start();
      break;
      
    case kYellow:
      return Delete();
      break;
      
    case kBlue:
      return Reset();
      break;
      
    default:
      break;
    }//switch
  }//if
  return state;
}

eOSState cDVDList::Delete() {
  int rc=0;
  char *cmd = NULL;
  
  cMenuDVDItem *item = (cMenuDVDItem*)Get(Current());
  if (item) {
    if (Interface->Confirm(tr("Delete DVD?"))) {
      esyslog(cmd);
      asprintf(&cmd, RMCMD, item->dateiname);
      rc=system(cmd);
    }
  }
  
  //Nach Löschen Menü ausblenden,
  //da es neu aufgebaut werden muß.
  return osEnd;
}

eOSState cDVDList::ReadDVDMenue() {
  int rc=5;
  Clear();
  Add(new cMenuEditIntItem( tr("name"),&rc,1,10));
  Display();

  return osContinue;
}

eOSState cDVDList::Mount() {
  int rc=0;
  cMenuDVDItem *item = (cMenuDVDItem*)Get(Current());
  if (item) {
    //      SetItemsDirty();
#if VDRVERSNUM >= 10307
    Skins.Message(mtStatus, "Mounte");
#else
    Interface->Status("Mounte");
#endif
    char *cmd = NULL;
    char *device = NULL;
    //	bool result = false;
    
    asprintf(&device, "%s",DVDSelectSetup.NameDevice);
    
    //asprintf(&cmd, RMCMD, device);
    //rc=system(cmd);
    //esyslog(cmd);
    /*
    //Debugausgabe:
    if (rc==0){
    Interface->Status("erfolgreich 1");
    }else{
    Interface->Error("Fehler 1!");
    }
    */
    
    asprintf(&cmd, MOUNTCMD, item->dateiname, device);
    esyslog(cmd);
    rc=system(cmd);
    //Debugausgabe:
    //	Interface->Status(cmd);

    /*
    //Debugausgabe:
    if (rc==0){
    Interface->Status("erfolgreich");
    }else{
    Interface->Error("Fehler!");
    }
    */
#if VDRVERSNUM >= 10307
    Skins.Flush();
#else
    Interface->Flush();
#endif
  }
  
  return osContinue;
}

//Undo mounting
eOSState cDVDList::Reset() {
  int rc=0;
    //      SetItemsDirty();
#if VDRVERSNUM >= 10307
  Skins.Message(mtStatus, "Resette");
#else
    Interface->Status("Resette");
#endif
    char *cmd = NULL;
    char *device = NULL;
    char *orgDevice = NULL;
    //	bool result = false;
    
    asprintf(&device, "%s",DVDSelectSetup.NameDevice);
    asprintf(&orgDevice, "%s",DVDSelectSetup.NameOrgDevice);
    
    //asprintf(&cmd, RMCMD, device);
    //rc=system(cmd);
    //esyslog(cmd);
    /*
    //Debugausgabe:
    if (rc==0){
    Interface->Status("erfolgreich 1");
    }else{
    Interface->Error("Fehler 1!");
    }
    */
    
    asprintf(&cmd, RESETCMD,orgDevice, device);
    esyslog(cmd);
    rc=system(cmd);
    //Debugausgabe:
    //	Interface->Status(cmd);

    /*
    //Debugausgabe:
    if (rc==0){
    Interface->Status("erfolgreich");
    }else{
    Interface->Error("Fehler!");
    }
    */
  
    //  return osContinue;
  return osEnd;
}

eOSState cDVDList::Start() {
  Mount();
  char dvdpluginname[100];
  strcpy(dvdpluginname,"dvd");

  for (int i = 0; ; i++) {
      cPlugin *p = cPluginManager::GetPlugin(i);
      if (p) {
	if (strcasecmp(dvdpluginname,p->Name())==0){
	  p->MainMenuAction();
	  break;
	} else{
	}
      }
      else{
#if VDRVERSNUM >= 10307
	Skins.Message(mtError, "DVD-plugin not found!");
#else
	Interface->Error("DVD-plugin not found!");
#endif
	break;
      }
  }
  return osEnd;
}

eOSState cDVDList::DVDWriteScript()
{
  int rc=0;
  char *cmd = NULL;

  cMenuDVDItem *item = (cMenuDVDItem*)Get(Current());
  if (item) {
    asprintf(&cmd, SCRIPT_DVDWRITE_CMD,DVDSelectSetup.DVDWriteScript,item->dateiname);
    esyslog(cmd);
    rc=system(cmd);
  }

  //Nach Scriptausführung Menü ausblenden,
  //da es neu aufgebaut werden muß.
  return osEnd;
}//DVDWriteScript()


void cDVDList::SetHelp() {
  cOsdMenu::SetHelp(tr("start"),tr("dvdwritescript"),tr("delete"),tr("reset"));
}



// --- cMenuReadDVD --------------------------------------------------------

cMenuReadDVD::cMenuReadDVD()
:cOsdMenu(tr("read dvd"), 12)
{
  strcpy(name,tr("new_dvd"));

  SetHelp();

  Add(new cMenuEditStrItem( tr("dvd-name"), name, MaxFileName, tr(FileNameChars)));
}

cMenuReadDVD::~cMenuReadDVD()
{
}

eOSState cMenuReadDVD::ProcessKey(eKeys Key)
{
  int rc=0;
  char *cmd = NULL;

  eOSState state = cOsdMenu::ProcessKey(Key);

  //  asprintf(&cmd, "%d-%d", Key,state);
  //  esyslog(cmd);

  //  if (state == osUnknown) {
    switch (Key) {
    case kYellow:
    case kGreen:
    case kBlue:
    case kOk:
    case k0:
      return osContinue;
      break;
      
    case kRed:
      asprintf(&cmd, SCRIPT_DVDREAD_CMD,DVDSelectSetup.DVDReadScript,DVDSelectSetup.DVDOutputDir,name,DVDSelectSetup.NameOrgDevice);
      esyslog(cmd);
      rc=system(cmd);
      return osEnd;
    default:
      break;
    }//switch;
    //  }//if
  return state;
}

void cMenuReadDVD::SetHelp() {
  cOsdMenu::SetHelp(tr("start"),NULL,NULL,NULL);
}
