
/*
 * burn.c: A plugin for the Video Disk Recorder
 *
 * See the files COPYING and README for copyright information and how to reach 
 * the author.
 *
 * $Id: burn.c,v 1.8 2004/12/05 19:05:31 lordjaxom Exp $
 */

#include "i18n.h"
#include "menuburn.h"
#include "setup.h"
#include <vdr/plugin.h>
#include <vdr/videodir.h>
#include <getopt.h>

static const char *VERSION = "0.0.009";
static const char *DESCRIPTION = "Versatile convert-and-burn plugin";
static const char *MAINMENUENTRY = "Write DVDs";

class cPluginBurn:
    public cPlugin
{
  public:
    cPluginBurn(void);
    virtual ~cPluginBurn();

    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 cString Active(void);
    virtual const char *MainMenuEntry(void) { return tr(MAINMENUENTRY); }
    virtual cOsdObject *MainMenuAction(void);
    virtual cMenuSetupPage *SetupMenu(void);
    virtual bool SetupParse(const char *Name, const char *Value);
};

cPluginBurn::cPluginBurn(void)
{
}

cPluginBurn::~cPluginBurn()
{
}

const char *cPluginBurn::CommandLineHelp(void)
{
    static char *help_str = NULL;

    free(help_str);
    asprintf(&help_str,
            "  -s CMD,   --script=CMD    use CMD as vdrburn script\n"
            "                            (default: %s)\n"
            "  -d DIR,   --datadir=DIR   use DIR for temporary files\n"
            "                            (default: %s)\n"
            "  -D DEV    --dvd=DEV       use DEV as DVD Writer\n"
            "                            (default: %s)\n"
            "  -i DIR    --iso=DIR       use DIR to store ISO images\n"
            "                            (default: %s)\n",
            VDRBURNCMD,
            VideoDirectory,
            DVDDEV,
            ISODIR ? ISODIR : "none");
    return help_str;
}

bool cPluginBurn::ProcessArgs(int argc, char *argv[])
{
    static struct option long_options[] = {
        { "script",  required_argument, NULL, 's' },
        { "datadir", required_argument, NULL, 'd' },
        { "dvd", required_argument, NULL, 'D' },
        { "iso", required_argument, NULL, 'i' },
        { NULL }
    };

    Datadir = VideoDirectory;
    Script = VDRBURNCMD;

    int c, optind = 0;
    while ((c = getopt_long(argc, argv, "s:d:D:i:", long_options, &optind)) != -1) {
        switch (c) {
            case 's':
                Script  = optarg;
                break;
            case 'd':
                Datadir = optarg;
                break;
            case 'D':
                DVDdev = optarg;
                break;
            case 'i':
                ISOdir = optarg;
                break;
            default:
                return false;
        }
    }

    // determine type of DVDdev
    if (DVDdev == NULL) {
        DVDdev = DVDDEV;
        if (DVDdev == NULL) {
            if (DVDdev != NULL) {
                struct stat sbuf;
                if (stat(DVDdev, &sbuf) != 0) {
                    DVDdev = NULL;
                } else if (!S_ISBLK(sbuf.st_mode)) {
                    DVDdev = NULL;
                }
            }
        }
    } else {
        struct stat sbuf;
        if (stat(DVDdev, &sbuf) != 0) {
            esyslog("ERROR: Couldn't open %s: %m", DVDdev);
            fprintf(stderr, "ERROR: Couldn't open %s: %m\n", DVDdev);
            return false;
        }

        if (!S_ISBLK(sbuf.st_mode)) {
            esyslog("ERROR: %s is not a block-device, aborting", DVDdev);
            fprintf(stderr, "ERROR: %s is not a block-device, aborting\n",
                    DVDdev);
            return false;
        }
    }

    if (ISOdir == NULL) {
        ISOdir = ISODIR;
        if (ISOdir != NULL) {
            struct stat sbuf;
            if (stat(ISOdir, &sbuf) != 0) {
                ISOdir = NULL;
            } else if (!S_ISDIR(sbuf.st_mode)) {
                ISOdir = NULL;
            }
        }
    } else {
        struct stat sbuf;
        if (stat(ISOdir, &sbuf) != 0) {
            dsyslog("creating directory %s", ISOdir);
            if (mkdir(ISOdir, ACCESSPERMS)) {
                esyslog("ERROR: Failed to create directory %s.", ISOdir);
            }
        } else {
            if (!S_ISDIR(sbuf.st_mode)) {
                esyslog("ERROR: Failed to create directory %s: file exists but is not a directory.", ISOdir);
                return false;
            }
        }

        if (stat(ISOdir, &sbuf) != 0) {
            esyslog("ERROR: Couldn't open %s: %m", ISOdir);
            fprintf(stderr, "ERROR: Couldn't open %s: %m\n", ISOdir);
            return false;
        }

        if (!S_ISDIR(sbuf.st_mode)) {
            esyslog("ERROR: %s is not a directory, aborting", ISOdir);
            fprintf(stderr, "ERROR: %s is not a directory, aborting\n",
                    ISOdir);
            return false;
        }
    }
    return true;
}

bool cPluginBurn::Initialize(void)
{
    return true;
}

bool cPluginBurn::Start(void)
{
    RegisterI18n(Phrases);
    return true;
}

void cPluginBurn::Stop(void)
{
    cBurnMenuBurn::Cleanup();
}

void cPluginBurn::Housekeeping(void)
{
    cBurnMenuBurn::Housekeeping();
}

cString cPluginBurn::Active(void)
{
    if (cBurnMenuBurn::JobActive())
       return tr("Burn Plugin is busy");
    else
       return NULL;
}

cOsdObject *cPluginBurn::MainMenuAction(void)
{
    return new cBurnMenuBurn;
}

cMenuSetupPage *cPluginBurn::SetupMenu(void)
{
    return new cBurnMenuSetupPage;
}

bool cPluginBurn::SetupParse(const char *Name, const char *Value)
{
    return BurnSetup.SetupParse(Name, Value);
}

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