/*
 * See the files COPYING and README for copyright information and how to reach
 * the author.
 *
 *  $Id: chain-archive.c,v 1.4 2006/05/01 19:10:22 lordjaxom Exp $
 */

#include "chain-archive.h"
#include "boost/bind.hpp"
#include "proctools/extract.h"
#include "proctools/shellprocess.h"
#include "proctools/functions.h"
#include "proctools/logger.h"
#include <string>
#include <sstream>

namespace vdr_burn
{

	using namespace std;
	using boost::bind;
	using proctools::process;
	using proctools::shellprocess;
	using proctools::format;
	using proctools::logger;

	chain_archive::chain_archive(job& job):
			chain_vdr("Archive", job),
			m_step(build)
	{
	}

	void chain_archive::process_line(const string& line)
	{
		static const string burn_done("% done, estimate finish");

		istringstream parser(line);
		double percentDone;
		parser >> percentDone;
		char rest[burn_done.length() + 1];
		parser.get(rest, burn_done.length() + 1, '\n');
		if (rest == burn_done)
			set_progress(static_cast<int>(percentDone));
	}

	bool chain_archive::initialize(void)
	{
		string graftPoints =
				accumulate(get_job().get_recordings().begin(),
						   get_job().get_recordings().end(),
						   string(""),
						   bind( plus<string>(), _1,
								 bind( plus<string>(),
									   bind( &recording::get_graft_point, _2 ),
									   " "
						  )));

		switch (get_job().get_store_mode())
		{
		case smCreateISO:
			{
				shellprocess* burn = new shellprocess("burn",
												  "vdrburn-archive.sh mkiso");
				burn->put_environment("ISO_FILE",     get_job().get_iso_path());
				burn->put_environment("GRAFT_POINTS", graftPoints);
				burn->put_environment("DISC_ID", get_job().get_volume_id());
				add_process(burn);
			}
			break;

		case smBurnISO:
			{
				shellprocess *burn = new shellprocess("burn",
												  "vdrburn-archive.sh burndir");
				burn->put_environment("DVD_DEVICE",   BurnParameters.DvdDevice);
				burn->put_environment("GRAFT_POINTS", graftPoints);
				burn->put_environment("BURN_SPEED",   BurnSetup.BurnSpeed);
				burn->put_environment("DISC_ID", get_job().get_volume_id());
				add_process(burn);
			}
			break;

		case smCreateBurn:
			{
				string fifo = format("{0}/burnfifo") % get_paths().temp;
				make_fifo(fifo);

				shellprocess* pipe = new shellprocess("pipe",
												  "vdrburn-archive.sh pipeiso");
				pipe->put_environment("GRAFT_POINTS", graftPoints);
				pipe->put_environment("ISO_FILE",     get_job().get_iso_path());
				pipe->put_environment("ISO_PIPE",     fifo);
				pipe->put_environment("DISC_ID", get_job().get_volume_id());
				add_process(pipe);

				shellprocess* burn = new shellprocess("burn",
												  "vdrburn-archive.sh burniso");
				burn->put_environment("DVD_DEVICE",   BurnParameters.DvdDevice);
				burn->put_environment("ISO_PIPE",     fifo);
				burn->put_environment("BURN_SPEED",   BurnSetup.BurnSpeed);
				add_process(burn);
			}
			break;
		}
		return true;
	}

	bool chain_archive::finished(const process* proc)
	{
		logger::debug("process ended: " + proc->name());
		if (proc->return_code() == 0)
			return true;

		// process failed
		if (proc->name() == "burn") {
		}

		return false;
	}

}
