/*
 * telecable.c: A plugin for the Video Disk Recorder
 *
 */

#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <vdr/interface.h>
#include <vdr/plugin.h>
#include <vdr/menuitems.h>
#include <vdr/status.h>

#include "telecable.h"
#include "i18n.h"
#include "opts.h"  // From Sc plugin 
#include "data.h"  // From Image / Mp3 plugin

static cOpts *TelecableOpts;
static int   SiteConfig; 
static int   BRandom; 
static int   FType;
static int   MaxSiteConfig=0;
static cFileSources TelecableSources;
static const char *FILE_TYPE[3]   = { "telecable.rar", "mixed.rar", "xmltv.rar" };



char ** UrlDescriptorList;
cPluginTelecable * thePluginTelecable;


// --- cMenuSetupTelecable ------------------------------------------------------------
class cMenuSetupTelecable : public cMenuSetupPage {
private:
  char *cfgdir;
protected:
  virtual void Store(void);
public:
  cMenuSetupTelecable(const char *CfgDir);
  virtual ~cMenuSetupTelecable();
  virtual eOSState ProcessKey(eKeys Key);
  };

cMenuSetupTelecable::cMenuSetupTelecable(const char *CfgDir)
{
  cfgdir=strdup(CfgDir);
  SetSection(tr(TELECABLE));
  TelecableOpts->Backup();
  TelecableOpts->Create(this);
}

cMenuSetupTelecable::~cMenuSetupTelecable()
{
  free(cfgdir);
}

void cMenuSetupTelecable::Store(void)
{
  TelecableOpts->Store();
}

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

  switch(state) {
    case osUnknown:
      if(Key==kOk) { Store(); state=osBack; }
      break;

    default:
      break;
    }
  return state;
}



// --- cTelecableSetup ---------------------------------------------------------------

cTelecableSetup TelecableSetup;

cTelecableSetup::cTelecableSetup(void)
{
  SiteConfig = 1;
  BRandom = 1; 
  FType = 0;
}


// --- cPluginTelecable ----------------------------------------------------------


cPluginTelecable::cPluginTelecable(void)
{
  // Initialize any member variables here.
  // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
  // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!

  // MENU SETUP
  TelecableOpts=new cOpts(0,3);
  TelecableOpts->Add(new cOptBool("RandomSite"   , tr(RANDOM),&BRandom ));
  TelecableOpts->Add(new cOptSite("SelectedSite" ,tr(URL) ,&SiteConfig ,MAX_URL_ITEMS /* ? MaxSiteConfig : MaxSiteConfig!=0*/,(const char**)UrlDescriptorList));
  TelecableOpts->Add(new cOptSel("FileType"     ,tr(ARCHIVE) ,&FType      , 3             , FILE_TYPE));
  thePluginTelecable=this;
}

cPluginTelecable::~cPluginTelecable()
{
  free(UrlDescriptorList);
}

const char *cPluginTelecable::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return "Sorry. No comand line help.\n";
}

bool cPluginTelecable::ProcessArgs(int argc, char *argv[])
{
  return true;
}

bool cPluginTelecable::Start(void)
{
  // Start any background activities the plugin shall perform.

  RegisterI18n(Phrases);

  TelecableSources.Load(AddDirectory(ConfigDirectory(),  "telecablesources.conf"));
  if(TelecableSources.Count()<1) {
    esyslog("telecableplugin: FATAL! You must have defined at least one source in telecablesources.conf");
    return false;
    }

 MaxSiteConfig = MAX_URL_ITEMS ? TelecableSources.Count()  : TelecableSources.Count()<MAX_URL_ITEMS ;
 if (SiteConfig > MaxSiteConfig)
	SiteConfig = 0;  // Default is 1st Url   

  if((UrlDescriptorList=MALLOC(char *,MAX_URL_ITEMS))) {
    int i =0;
    for( i=0; i<MaxSiteConfig ; i++) {
	TelecableSources.SetSource(TelecableSources.Get( i ));
	UrlDescriptorList[i]=(char *)tr((TelecableSources.GetSource())->Description());
	}
    for( ; i<MAX_URL_ITEMS ; i++) 
      UrlDescriptorList[i]=(char *)tr(NOMORE);
    }

  return true;
}

void cPluginTelecable::Housekeeping(void)
{
  // Perform any cleanup or other regular tasks.
}

cOsdObject *cPluginTelecable::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  char Msg1[400];
  char Msg2[400];
  char Err1[400];
  char sysCall[400];
  char from[400];

  if (BRandom)
	{
	SiteConfig = rand() / (RAND_MAX / MaxSiteConfig + 1);
        }

  TelecableSources.SetSource(TelecableSources.Get( SiteConfig ));
  strcpy(Msg1,tr(DOWNLOADED));
  strcpy(Msg2,tr(UPDATED));
  strcpy(Err1,tr(ERR_DOWNLOADED));
    
  strcpy(sysCall,ConfigDirectory());
  strcat(sysCall,"/telecable/RunTelecable.sh ");
  strcat(sysCall,ConfigDirectory());
  strcat(sysCall," ");
  strcat(sysCall,(TelecableSources.GetSource())->BaseDir());
  strcpy(from,(TelecableSources.GetSource())->BaseDir());
  if ( FType == 1 )
  {
	  strcat(sysCall,"/mixed.rar mixed");
	  strcat(from,"/mixed.rar");
  }  
  else if ( FType == 2 )
  {
	  strcat(sysCall,"/xmltv.rar xmltv");
	  strcat(from,"/xmltv.rar");
  }
  else 
  {
	  strcat(sysCall,"/telecable.rar telecable");
	  strcat(from,"/telecable.rar");
  }
  strcat(sysCall," ");
  strcat(sysCall,Msg1);
  strcat(sysCall," ");
  strcat(sysCall,Msg2);
  strcat(sysCall," ");
  strcat(sysCall,Err1);
  strcat(sysCall,"  &");
  system(sysCall);
  
  Interface->Confirm( tr(UPDATING), 2);
  Interface->Confirm( from, 2);

return NULL;
}


cMenuSetupPage *cPluginTelecable::SetupMenu(void)
{
  return new cMenuSetupTelecable(ConfigDirectory());
}

bool cPluginTelecable::SetupParse(const char *Name, const char *Value)
{
  if(!TelecableOpts->Parse(Name,Value))
    return false;
  return true;
}

VDRPLUGINCREATOR(cPluginTelecable); // Don't touch this!


