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

#include <vdr/plugin.h>
#include "config.h"
#include "setup.h"
#include <unistd.h>
#include <string>
#if VDRVERSNUM >= 10332
#include <vdr/device.h>
#endif

static const char *VERSION        = "0.0.2";
static const char *DESCRIPTION    = "Zaps to last channel after ads";
static const char *MAINMENUENTRY  = "Adzap";

class cPluginAdzap : public cPlugin {
private:
  // Add any member variables or functions you may need here.
  std::string jobPath;
  std::string checkPath;
public:
  cPluginAdzap(void);
  virtual ~cPluginAdzap();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return 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);
  };

cPluginAdzap::cPluginAdzap(void) : jobPath("/etc/vdr"), checkPath("/etc/vdr")
{
  // 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!
  config.inetAvail=0;
  config.timeout=3;
}

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

const char *cPluginAdzap::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return "  -c PATH, path to -> adzapcheck.sh\n"
         "  -j PATH, path to -> adzap.job\n";

  return NULL;
}

bool cPluginAdzap::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  int c;

  while( true ) {
    c = getopt( argc, argv, "c:j:" ); // if option is given, it requires parameter
    if( c == -1 ) {
      break;
    }
    switch( c ) {
      case 0:
        break;
      case 'c':
        checkPath = optarg;
        break;
      case 'j':
        jobPath = optarg;
        break;
      default:
        printf( "?? getopt returned character code 0%o ??\n", c );
    }
  }
  checkPath += "/adzapcheck.sh";
  jobPath += "/adzap.job";
  return true;
}

bool cPluginAdzap::Initialize(void)
{
  // Initialize any background activities the plugin shall perform.
  return true;
}

bool cPluginAdzap::Start(void)
{
  // Start any background activities the plugin shall perform.
  return true;
}

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

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

cOsdObject *cPluginAdzap::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  cChannel *Channel=Channels.GetByNumber(cDevice::PrimaryDevice()->CurrentChannel());
  char cmd[500];
  if (config.inetAvail==0)
  {
  		FILE *f=fopen(jobPath.c_str(),"w");
  		fprintf(f,"svdrpsend.pl CHAN \"%s\"",Channel->Name());
  		fclose(f);
  		chmod(jobPath.c_str(),755);
  		sprintf(cmd,"at -f %s now + %u minutes", jobPath.c_str(), config.timeout);
		system(cmd);
  		Skins.Message(mtStatus,tr("Werbezapper wurde aktiviert"));
  		sleep(3);
  		Skins.Flush();
  }
  else
  {
  		sprintf( cmd, "%s \"%s\" \"%s\"", checkPath.c_str(), Channel->Name(),
               jobPath.c_str() );
  		system(cmd);
  		if (access("/tmp/adzap.run",R_OK)==0)
			Skins.Message(mtStatus,tr("Werbezapper wurde aktiviert"));
  		else
			Skins.Message(mtStatus,tr("KEINE WERBUNG ERKANNT"));
  		sleep(3);
  		Skins.Flush();
  }
  return NULL;
}

cMenuSetupPage *cPluginAdzap::SetupMenu(void)
{
  // Return a setup menu in case the plugin supports one.
  return new cAdZapSetup;
}

bool cPluginAdzap::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  if      (!strcasecmp(Name, "InetAvail"))                 config.inetAvail = atoi(Value);
  else if (!strcasecmp(Name, "Timeout"))                   config.timeout = atoi(Value);
  else return false;
  return true;
}

sAdZapConfig config;

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