/*
 * btrcu plugin for VDR
 *
 * plugin.c - main plugin class
 *
 * Copyright (C) 2004 Roland Praml <pram @ gmx . de>
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 */

#include <getopt.h>

#include "btrcu.h"

#include <vdr/plugin.h>

static const char *VERSION        = "0.0.1";
static const char *DESCRIPTION    = "Controls VDR through a Bluetooth mobile phone";
static const char *MAINMENUENTRY  = NULL;

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

cPluginBtRCU::cPluginBtRCU(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!
}

cPluginBtRCU::~cPluginBtRCU()
{
	// Clean up after yourself!
	delete BtRCU;
}

const char *cPluginBtRCU::CommandLineHelp(void)
{
	// Return a string that describes all known command line options.
	return "  -d DEV,   --device=DEV   use DEV as the BtRCU device (default is /dev/rfcomm0)\n";
}

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

	strcpy(device, "/dev/rfcomm0");
	while ((c = getopt_long(argc, argv, "d:", long_options, NULL)) != -1)
	{
		switch (c)
		{
			case 'd':
				strcpy(device, optarg);
				break;
			default:
				return false;
		}
	}
	return true;
}

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

bool cPluginBtRCU::Start(void)
{
	// Start any background activities the plugin shall perform.
	BtRCU = new cBtRCU(device, AddDirectory(ConfigDirectory(), "btrcu.sh"));
	return true;
}

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

cOsdObject *cPluginBtRCU::MainMenuAction(void)
{
	// Perform the action when selected from the main VDR menu.
	return NULL;
}

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