/*
 * swcon.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: swcon.c 1.11 2002/12/13 15:01:43 kls Exp $
 */

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

static const char *VERSION        = "0.1";
static const char *DESCRIPTION    = "Switching Console";
static const char *DEFMAINMENUENTRY = "SwitchToX";

// Global variables that control the overall behaviour:

int VDRConsole = 8;
int SwConsole = 7;


class cPluginSwCon : public cPlugin {
private:
   // Add any member variables or functions you may need here.
   const char * mainMenuEntry;
   const char * scriptFile;
public:
   cPluginSwCon(void);
   virtual ~cPluginSwCon();
   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 Start(void);
   virtual void Housekeeping(void);
   virtual const char *MainMenuEntry(void) { return mainMenuEntry;}
   virtual cOsdObject *MainMenuAction(void);
};


// --- cPluginSwCon ----------------------------------------------------------

cPluginSwCon::cPluginSwCon(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!
   mainMenuEntry = DEFMAINMENUENTRY;
   scriptFile = NULL;
}

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

const char *cPluginSwCon::CommandLineHelp(void)
{
   // Return a string that describes all known command line options.
   return "  -t <title>, --title=<title>       Title shown in VDR-Menu\n"
          "  -c <console>, --console=<console> Number of the other console\n"
          "  -s <script>, --script=<script>    Name of script to run\n";
}

bool cPluginSwCon::ProcessArgs(int argc, char *argv[])
{
   // Implement command line argument processing here if applicable.
   static struct option long_options[] = {
      { "title",    required_argument, NULL, 't'},
      { "console",  required_argument, NULL, 'c'},
      { "script",   required_argument, NULL, 's'},
      { NULL}
   };

   int c;
   while((c = getopt_long(argc, argv, "t:c:s:", long_options, NULL)) != -1) {
      switch(c) {
         case 't': 
            mainMenuEntry = optarg;
            break;
         case 'c': 
            SwConsole = atoi(optarg);
            break;
         case 's': 
            scriptFile = optarg;
            break;
         default:  
            return false;
      }
   }
   printf( "ScrFile: %s\n",scriptFile );
   printf( "MME: %s\n",mainMenuEntry );
   return true;
}

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

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

cOsdObject *cPluginSwCon::MainMenuAction(void)
{
   // Perform the action when selected from the main VDR menu.
   if(scriptFile != NULL) {
      isyslog( "Executing <%s>", scriptFile );
      system( scriptFile );
   }
   return NULL;
}

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