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

#include <getopt.h>

#include <vdr/plugin.h>

#include "setup.h"
#include "soccerosd.h"
#include "soccertools.h"
#include "i18n.h"


// ------------- cMenuSoccerSetup ------------------------------ //

class cMenuSoccerSetup : public cMenuSetupPage {
private:
 cSoccerSetup data;
protected:
  virtual void Store(void);
public:
  cMenuSoccerSetup(void);
};    

cMenuSoccerSetup::cMenuSoccerSetup(void) {
  data = SoccerSetup;
  Add(new cMenuEditBoolItem(tr("Update data on VDR start"),         &data.s_update));
}


void cMenuSoccerSetup::Store(void) {
  SoccerSetup = data;
  SetupStore("AutoUpdate",                                                 SoccerSetup.s_update);
} 



// -------------- cPluginSoccer ----------------------------------- //

static const char *VERSION        = "0.0.2a";
static const char *DESCRIPTION    = "Shows infos about german soccer";
static const char *MAINMENUENTRY  = "Soccer";

class cPluginSoccer : public cPlugin {
private:
 
public:
  cPluginSoccer(void);
  virtual ~cPluginSoccer();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return tr(DESCRIPTION); }
  virtual const char *CommandLineHelp(void);
  virtual bool ProcessArgs(int argc, char *argv[]);
  virtual bool Initialize(void);
  virtual bool Start(void);
  virtual void Stop(void);
  virtual void Housekeeping(void);
  virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
  virtual bool Service(const char *Id, void *Data = NULL);
  virtual const char **SVDRPHelpPages(void);
  virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
  };

cPluginSoccer::cPluginSoccer(void)
{

}

cPluginSoccer::~cPluginSoccer()
{

}

const char *cPluginSoccer::CommandLineHelp(void) {

  static char *help_str = NULL;
  
  free(help_str);
  asprintf(&help_str,
                     "  -D DIR    --data=DIR    DIR to datafiles\n"
		     "  -S DIR    --script=DIR  DIR to downloadscript\n");

  return help_str;
}

bool cPluginSoccer::ProcessArgs(int argc, char *argv[]) {
  
  static struct option long_options[] = {
       { "data",          required_argument, NULL, 'D' },
       { "script",        required_argument, NULL, 'S' },
       { NULL }
     };  
 
  int c, option_index = 0;
  while ((c = getopt_long(argc, argv, "D:S:", long_options, &option_index)) != -1) {
      switch (c) {
         case 'D': DataDir     = optarg; break;
         case 'S': ScriptDir   = optarg; break;
	 default: return false;
	 }
  }
  
  return true;	 
}

bool cPluginSoccer::Initialize(void) {

  if (DataDir==NULL) {
    DataDir = strdup(ConfigDirectory(PLUGIN_NAME_I18N));
    esyslog("%s: Parameter '-D' not set.\n"
            " Set default value : %s\n", PLUGIN_NAME_I18N, DataDir);
  }	    

  if (ScriptDir==NULL) {
    ScriptDir = strdup(ConfigDirectory(PLUGIN_NAME_I18N));
    esyslog("%s: Parameter '-S' not set.\n"
            " Set default value : %s\n", PLUGIN_NAME_I18N, ScriptDir);
  }	    

  return true;
}

bool cPluginSoccer::Start(void)
{
  if (SoccerSetup.s_update == true) {
    char *buffer;
    asprintf(&buffer, "%s%s \"%s\"", ScriptDir, "/getcontent.sh", DataDir);
    system(buffer);
    dsyslog("%s: Executing as autoupdate: '%s'\n", PLUGIN_NAME_I18N, buffer);
    free(buffer);
  }

  RegisterI18n(Phrases);    	
  return true;
}

void cPluginSoccer::Stop(void)
{

}

void cPluginSoccer::Housekeeping(void)
{

}

cOsdObject *cPluginSoccer::MainMenuAction(void)
{
  return new cSoccer();
}


bool cPluginSoccer::Service(const char *Id, void *Data)
{
   return false;
}

const char **cPluginSoccer::SVDRPHelpPages(void)
{
   return NULL;
}

cString cPluginSoccer::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
  return NULL;
}

cMenuSetupPage *cPluginSoccer::SetupMenu(void)
{
  return new cMenuSoccerSetup;
}

bool cPluginSoccer::SetupParse(const char *Name, const char *Value)
{
  if    (!strcasecmp(Name, "AutoUpdate"))   SoccerSetup.s_update    = atoi(Value);
  else
    return false;
    
  return true;    
}




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