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

#include <string>
#include <iostream>
#include <getopt.h>

static const char *VERSION        = "0.0.3";
const char *DESCRIPTION    = "Client for Asterisk voicebox";
const char *MAINMENUENTRY  = "Asterisk";

#include "asterisk.h"

#include "ast_menu.h"
#include "i18n.h"
#include "ast_setup.h"

using namespace std;

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

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

const char *cPluginAsterisk::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return 
    " -t DIR      directory specifying path to voiceboxes"
    " -m FILE     script to mount/unmount voicebox directory (optional)";
}

bool cPluginAsterisk::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  static struct option
    long_options[] =
    {
      { "directory", required_argument, NULL, 'd' },
      { "mount",     required_argument, NULL, 'm' },
      { NULL }
    };
  
  int c, option_index = 0;
  while ((c = getopt_long (argc, argv, "d:m:", long_options, &option_index)) != -1)
    {
      switch( c )
	{
	case 'd':
	  {
	    the_setup.Directory = std::string( optarg );
	  } break;
	case 'm':
	  {
	    the_setup.MountScript = std::string( optarg );
	  } break;
	}
    }

  // check that directory exists and was specified?
  
  return true;
}

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

bool cPluginAsterisk::Start(void)
{
  // Start any background activities the plugin shall perform.
  RegisterI18n (Phrases);

  return true;
}

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

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

  astVoiceboxList vbl( the_setup.Directory );
  return new astMainMenu( vbl );
}

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

bool cPluginAsterisk::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  return false;
}

bool cPluginAsterisk::Mount()
{
  bool result = false;
  if( !the_setup.MountScript.empty() )
    {
      string cmd = the_setup.MountScript + " start " + the_setup.Directory;
      cout << cmd << endl;
      result = (bool) system( cmd.c_str() );
    }
  return result;
}

bool cPluginAsterisk::Unmount()
{
  bool result = false;
  if( !the_setup.MountScript.empty() )
    {
      string cmd = the_setup.MountScript + " stop " + the_setup.Directory;
      cout << cmd << endl;
      result = (bool) system( cmd.c_str() );
    }
  return result;
}

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