
/*
 * See the files COPYING and README for copyright information and how to reach 
 * the author.
 *
 *  $Id: process-vdr.c,v 1.2 2004/12/05 18:50:34 lordjaxom Exp $
 */

#include "process-vdr.h"
#include "jobs.h"
#include "menuburn.h"
#include "i18n.h"
#include "setup.h"
#include <vdr/tools.h>
#include <vdr/interface.h>
#include <vdr/videodir.h>
#include <stdlib.h>

cBurnProcessVDR::cBurnProcessVDR(cBurnJob * Job):
    cBurnProcess(__VDR_COUNT__)
{
    mJob = Job;

    mJob->SetTempDir(TempDir());
    SetLogfile("%s/vdr.log", TempDir());
    Start();
}

cBurnProcessVDR::~cBurnProcessVDR()
{
}

void cBurnProcessVDR::InitRepeat()
{
    cBurnProcess::InitRepeat();
    SetStep((int)svGrowisofs);
}

void cBurnProcessVDR::Process(int Step, int SubStep, const char *Line)
{
    float fdone;

    switch ((eStepVDR) Step) {
        case svGrowisofs:
            char *asp;
            if (sscanf(Line, "%f%% done, estimate finish %as",
                    &fdone, &asp) == 2)
            {
                free(asp);
                SetBurning(true, (int)fdone);
                SetProgress(1, (int)fdone);
            }
            break;

        default:
            break;
    }
}

bool cBurnProcessVDR::Continue(int Step, int SubStep)
{
    cBurnRecording *rec;
    switch ((eStepVDR) Step) {
        case svGrowisofs:
            if (SubStep == 0) {
                char *cmd, *tmp, *path;
                char *dvdTitle;
                if (BurnSetup.CreateArchiveMarks) {
                    asprintf(&dvdTitle, "Archive: %04d - %s",
                            mJob->GetArchiveMark(), mJob->Title());
                } else {
                    dvdTitle = strdup(mJob->Title());
                }
                if (strlen(dvdTitle) > 32) {
                    dvdTitle[32] = 0;
                }

                if (!mJob->CreateArchiveMarks()) {
                    cBurnMenuBurn::Confirm(tr("Failed to set archive marks"));
                    return false;
                }

                switch (mJob->StoreMode()) {
                    case smCreateISO:
                        asprintf(&cmd, "mkisofs "
                                "-V '%s' -r -J -l -f -allow-lowercase "
                                "-allow-multidot -graft-points", dvdTitle);
                        for (rec = mJob->First(); rec; rec = mJob->Next(rec)) {
                            tmp = cmd;
                            asprintf(&cmd, "%s '%s'='%s'", tmp,
                                    (rec->FileName()) +
                                    strlen(VideoDirectory), rec->FileName());
                            free(tmp);
                        }
                        tmp = cmd;
                        asprintf(&cmd, "%s > '%s/%s.iso'", tmp, ISOdir,
                                mJob->Title());
                        free(tmp);
                        break;

                    case smBurnISO:
                        asprintf(&cmd,
                                "growisofs -use-the-force-luke=tty -Z %s "
                                 "-V '%s' -r -J -l -f -allow-lowercase "
                                 "-allow-multidot " "-graft-points", DVDdev,
                                 dvdTitle);
                        for (rec = mJob->First(); rec; rec = mJob->Next(rec)) {
                            char *tmp = cmd;
                            asprintf(&cmd, "%s '%s'='%s'", tmp,
                                    (rec->FileName()) +
                                    strlen(VideoDirectory), rec->FileName());
                            free(tmp);
                        }
                        break;

                    case smCreateBurn:
                        asprintf(&path, "%s/fifo", TempDir());
                        mkfifo(path, 0644);
                        asprintf(&cmd,
                                "growisofs -use-the-force-luke=tty -Z %s=%s "
                                "| mkisofs -V '%s' -r -J -l -f "
                                "-allow-lowercase -allow-multidot "
                                "-graft-points", DVDdev, path, dvdTitle);
                        for (rec = mJob->First(); rec; rec = mJob->Next(rec)) {
                            tmp = cmd;
                            asprintf(&cmd, "%s '%s'='%s'", tmp,
                                    (rec->FileName()) +
                                    strlen(VideoDirectory), rec->FileName());
                            free(tmp);
                        }
                        tmp = cmd;
                        asprintf(&cmd, "%s | tee '%s/%s.iso' > %s", tmp,
                                ISOdir, mJob->Title(), path);
                        free(tmp);
                        free(path);
                        break;

                    case smAuthorOnly:
                        break;

                    default:
                        SetStatus(rsError);
                        return false;
                }

                free(dvdTitle);
                SetBurning(true);
                SetCmdline("%s", cmd);
                free(cmd);
            } else if (Status() == rsError) {
                mJob->ClearArchiveMarks();

                Execute("eject %s", DVDdev);
                cBurnMenuBurn::Confirm(tr("Please insert an empty disk and press Ok"));
                SetNextStep(nsRetry);
            } else {
                if (!mJob->CreateNextArchiveMark()) {
                    cBurnMenuBurn::Confirm(tr("Failed to set archive marks"));
                    return false;
                }
                SetBurning(false);
                SetNextStep();
            }
            break;

        default:
            break;
    }

    return true;
}
