/*
 * See the files COPYING and README for copyright information and how to reach 
 * the author.
 *
 *  $Id: process-vdr.c,v 1.3 2006/01/20 21:51:29 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(sv_Count),
		mJob(Job)
{
	mJob->SetDirectories(TempDir(), DataDir());
	SetLogFile(mJob->GetLogFile());
	Start();
}

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

	switch ((eStepVDR)Step) {
		case svGrowisofs:
			if (sscanf(Line, "%f%% done, estimate finish", &fdone) == 1) {
				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;

				switch (mJob->StoreMode()) {
					case smCreateISO:
						asprintf(&cmd, "mkisofs "
								"-V VDR_BURN -r -J -l -allow-lowercase -allow-multidot "
								"-graft-points");
						for (rec = mJob->First(); rec; rec = mJob->Next(rec)) {
							tmp = cmd;
							asprintf(&cmd,"%s '%s'='%s'", tmp, 
									*rec->FileName() + strlen(BurnParameters.DataPath), 
									*rec->FileName());
							free(tmp);
						}
						tmp = cmd;
						asprintf(&cmd, "%s > '%s'", tmp, *mJob->GetISOFile());
						free(tmp);
						break;

					case smBurnISO:
						asprintf(&cmd, "growisofs -use-the-force-luke=tty -Z %s "
								"-V VDR_BURN -r -J -l -allow-lowercase -allow-multidot "
								"-graft-points", BurnParameters.DvdDevice);
						for (rec = mJob->First(); rec; rec = mJob->Next(rec)) {
							char *tmp = cmd;
							asprintf(&cmd,"%s '%s'='%s'", tmp, 
									*rec->FileName() + strlen(BurnParameters.DataPath),
									*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 VDR_BURN -r -J -l -allow-lowercase -allow-multidot "
								"-graft-points", BurnParameters.DvdDevice, path);
						for (rec = mJob->First(); rec; rec = mJob->Next(rec)) {
							tmp = cmd;
							asprintf(&cmd,"%s '%s'='%s'", tmp, 
									*rec->FileName() + strlen(BurnParameters.DataPath),
									*rec->FileName());
							free(tmp);
						}
						tmp = cmd;
						asprintf(&cmd, "%s | tee '%s' > %s", tmp, *mJob->GetISOFile(), 
								 path);
						free(tmp);
						free(path);
						break;
				}

				SetBurning(true);
				AddCmdline(cmd);
				free(cmd);
			} else if (Status() == rsError) {
				Execute("eject %s", BurnParameters.DvdDevice);
				cBurnMenuBurn::Confirm(tr("Please insert an empty disk and press Ok"));
				SetNextStep(nsRetry);
			} else {
				SetBurning(false);
				SetNextStep();
			}
			break;

		default:
			break;
	}

	return true;
}

