/*
 * netconfig.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: netconfig.c,v 1.1.1.1 2005/06/27 13:37:56 cm Exp $
 */

#include <vdr/plugin.h>
#include <getopt.h>
#include "netconfig_conf.h"
#include "netconfig_menu.h"
#include "netconfig_i18n.h"

bool menuEntry;
cNetconfigConf oConf;

static const char *VERSION        = "0.0.2";
static const char *DESCRIPTION    = "Network configuration";
static const char *MAINMENUENTRY  = "Network";

class cPluginNetconfig: public cPlugin {
private:
  // Add any member variables or functions you may need here.
public:
  cPluginNetconfig(void);
  virtual ~cPluginNetconfig();
  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 menuEntry ? tr(MAINMENUENTRY) : NULL; }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
};

cPluginNetconfig::cPluginNetconfig(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!

  menuEntry = true;
}

cPluginNetconfig::~cPluginNetconfig() {
  // Clean up after yourself!
}

const char *cPluginNetconfig::CommandLineHelp(void) {
  static char *p_help = NULL;

  if (NULL == p_help)
    asprintf(&p_help, "  -d CMD,  --dhcp-script=CMD     use CMD to start/stop the dhcp client\n");

  return p_help;
}

bool cPluginNetconfig::ProcessArgs(int argc, char *argv[]) {
  int c = 0;

  static struct option long_options[] = { {"dhcp-script", required_argument, NULL, 'd'},
                                          {        0,                 0,    0,   0} };

  while (-1 != (c = getopt_long(argc, argv, "d:", long_options, NULL))) {
    switch (c) {
      case 'd':
        oConf.SetDhcpScript(optarg);
        break;
     default:
        return false;
        break;
    }
  }

  return true;
}

bool cPluginNetconfig::Initialize(void) {
  // Initialize any background activities the plugin shall perform.

  RegisterI18n(tlPhrases);
  return true;
}

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

  oConf.SetConfig(AddDirectory(ConfigDirectory(), NC_CONFIG_FILE));

  return true;
}

void cPluginNetconfig::Stop(void) {
  // Stop any background activities the plugin shall perform.

  if (oConf.isOk())
    oConf.Save();
}

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

cOsdObject *cPluginNetconfig::MainMenuAction(void) {
  // Perform the action when selected from the main VDR menu.

  return new cNetconfigMenu();
}

cMenuSetupPage *cPluginNetconfig::SetupMenu(void) {
  // Return a setup menu in case the plugin supports one.

  return new cNetconfigSetupMenu();
}

bool cPluginNetconfig::SetupParse(const char *Name, const char *Value) {
  if (!strcasecmp(Name, "menuEntry"))
    menuEntry = atoi(Value);
  else
    return false;

  return true;
}

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