/*
 * 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));
  Add(new cMenuEditIntItem(tr("Width for position (def. 6)"),         &data.tab1,4,7));
  Add(new cMenuEditIntItem(tr("Width for club (def. 12)"),            &data.tab2,12,20));
  Add(new cMenuEditIntItem(tr("Width for games (def. 4)"),            &data.tab3,2,5));
  Add(new cMenuEditIntItem(tr("Width for goals (def. 8)"),            &data.tab4,4,10));
  Add(new cMenuEditIntItem(tr("Width for points (def. 8)"),           &data.tab5,4,10));

}


void cMenuSoccerSetup::Store(void) {
  SoccerSetup = data;
  SetupStore("AutoUpdate",                                   SoccerSetup.s_update);
  SetupStore("Tab1",                                         SoccerSetup.tab1);
  SetupStore("Tab2",                                         SoccerSetup.tab2);
  SetupStore("Tab3",                                         SoccerSetup.tab3);
  SetupStore("Tab4",                                         SoccerSetup.tab4);
  SetupStore("Tab5",                                         SoccerSetup.tab5);

} 



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

static const char *VERSION        = "0.0.2b";
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));
    dsyslog("%s: Parameter '-D' not set.\n"
            " Set default value : %s\n", PLUGIN_NAME_I18N, DataDir);
  }	    

  if (ScriptDir==NULL) {
    ScriptDir = strdup(ConfigDirectory(PLUGIN_NAME_I18N));
    dsyslog("%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);
    dsyslog("%s: Executing as autoupdate: '%s'\n", PLUGIN_NAME_I18N, buffer);
    system(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 if  (!strcasecmp(Name, "Tab1"))    SoccerSetup.tab1        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab2"))    SoccerSetup.tab2        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab3"))    SoccerSetup.tab3        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab4"))    SoccerSetup.tab4        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab5"))    SoccerSetup.tab5        = atoi(Value);
    else
      return false;
    
  return true;    
}




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