
// Project: webCDwriter - Network CD Writing
// File: Cdrecord.cpp
// Author: Jörg Haeger, 13.03.2000

#include "Config.h"
#include "Support.h"
#include "Cdrecord.h"

Cdrecord::Cdrecord(struct CDstatus *CDstatus):
	CDstatus(CDstatus),
	Process(config.getCdrecordPath())
{
	for (int i = 0; i < 256; i++)
	{
		results[i].key = -1;
		results[i].code = 0;
		results[i].qual = 0;
		results[i].str = NULL;
	}
	firstError = lastCommand = lastError = -1;

	cannotReadTOC = cannotReadSessionOffset = 0;
	fixating = 0;
	mayNotFit = 0;
	mediumNotPresent = 0;
	fifoPercent = MBwritten = MBofTrackWritten = MBtotal = 0;
	time(&t0);
	t1 = t2 = t0;

	// add dev-argument of cdrecord
	char str[4*1024];
	sprintf(str, "dev=%s", config.getCdrecordDevArg());
	addArg(str);
}

Cdrecord::~Cdrecord()
{
	for (int i = 0; i < 256; i++)
		if (results[i].str != NULL)
			delete results[i].str;
}

const char *Cdrecord::getFirstError()
{
	if (firstError >= 0)
	{
		static char str[45 + 1];
		strncpy(str, results[firstError].str, sizeof str - 1);
		str[sizeof str - 1] = 0;
		return str;
	}
	else
	if (mayNotFit)
		return "may not fit";
	else
		return NULL;
}

const char *Cdrecord::getLastError()
{
	if (lastError < 0)
		return NULL;
	else
	{
		static char str[45 + 1];
		strncpy(str, results[lastError].str, sizeof str - 1);
		str[sizeof str - 1] = 0;
		return str;
	}
}

void Cdrecord::getProgress(int &current, int &total)
{
/*	time(&t1);
	current = t1 - t0;
	total = t2 - t0;
*/
	current = MBwritten + MBofTrackWritten;
	total = MBtotal;
}

int Cdrecord::isBlankDisk()
{
	// a blank disk is detected by read toc
	return results[0x43].code == 0x8F && results[0x43].qual == 0x00;
}

int Cdrecord::isDriveReady()
{
	return
		results[0x00].key == 0x00
		&& results[0x00].code == 0x00
		&& results[0x00].qual == 0x00;
}

int Cdrecord::isInProcessOfBecomingReady()
{
	// logical unit is in process of becoming ready
	return results[0x00].code == 0x04 && results[0x00].qual == 0x01;
}

int Cdrecord::isMediumNotPresent()
{
//	return mediumNotPresent;
	return results[0x00].code == 0x3A;
}

int Cdrecord::isTrayClosed()
{
	return (results[0x00].code == 0x3A && results[0x00].qual == 0x01)
		|| isDriveReady();
}

const char *Cdrecord::readLine()
{
	const char *lineStr = Process::readLine();
	if (lineStr == NULL)
		return NULL;

	lineType = none;
	int a, b, c, d = 100;
	if (sscanf(lineStr, "Blocks total: %d "
		"Blocks current: %d "
		"Blocks remaining: %d", &a, &b, &c) == 3)
	{
		if (CDstatus != NULL)
		{
			CDstatus->blocks0 = a;
			CDstatus->blocks1 = b;
			CDstatus->blocks2 = c;
		}
		lineType = capacity;
	}
	else
	if (strstr(lineStr, "Cannot read first writable address") != NULL)
		throw "can't read first writable address";
	else
	if (strstr(lineStr, "Cannot read session offset") != NULL)
		cannotReadSessionOffset = 1;
	else
	if (strstr(lineStr, "Cannot read TOC header") != NULL
		|| strstr(lineStr, "Cannot read TOC/PMA") != NULL)
		cannotReadTOC = 1;
	else
	if (sscanf(lineStr, "CDB: %x", &lastCommand) == 1)
	{
		// see cdrecord-1.8/libscg/scsierrs.c
		if (0 <= lastCommand && lastCommand < 256)
		{
			results[lastCommand].key = 0;
			results[lastCommand].code = 0;
			results[lastCommand].qual = 0;
			results[lastCommand].str = NULL;
		}
	}
	else
	if (sscanf(lineStr, "Current Secsize: %d", &a) == 1)
	{
		if (CDstatus != NULL)
			CDstatus->blockSize = a;
	}
	else
/*	if (strstr(lineStr, "Drive needs to reload") != NULL)
	{
		// the media to return to proper status.
		terminate();
		throw "reload media";
	}
	else
*/	if (strstr(lineStr, "Fixating...") == lineStr)
		fixating = 1;
	else
	if (strstr(lineStr, "Is erasable") != NULL)
	{
		if (CDstatus != NULL)
			CDstatus->isRW = 1;
	}
	else
	if (strstr(lineStr, "Is not erasable") != NULL)
	{
		if (CDstatus != NULL)
			CDstatus->isRW = 0;
	}
	else
	if (strstr(lineStr, "Last chance to quit") != NULL)
	{
		if (mayNotFit)
		{
			terminate();
			throw "may not fit";
		}
		else
		if (CDstatus != NULL && !CDstatus->isClosed
			&& CDstatus->blocks2 < 4500 + 6750)
		{
			terminate();
			throw "restartWithoutMulti";
		}
	}
	else
	if (sscanf(lineStr, "Manuf. index: %d", &a) == 1)
	{
		if (CDstatus != NULL)
			CDstatus->manufacturerIndex = a;
	}
	else
	if (strstr(lineStr, "may not fit") != NULL)
		mayNotFit = 1;
	else
	if (sscanf(lineStr, "Sense Code: %x Qual %x", &code, &qual) == 2)
	{
		if (0 <= lastCommand && lastCommand < 256)
		{
//			printf("cmd = %02x, key = %02x, "
//				"code = %02x, qual = %02x\n",
//				lastCommand, key, code, qual);
			if (firstError == -1)
				firstError = lastCommand;
			lastError = lastCommand;
			results[lastCommand].key = key;
			results[lastCommand].code = code;
			results[lastCommand].qual = qual;

			char *a = strchr(lineStr, '(');
			char *b = strchr(lineStr, ')');
			if (a != NULL && b != NULL && a < b)
			{
				char str[45 + 1];
				int n = min(b - a - 1, sizeof str - 1);
				strncpy(str, a + 1, n);
				str[n] = 0;
				results[lastCommand].str = strDup(str);
			}
		}
		if (strstr(lineStr, "medium not present") != NULL)
			mediumNotPresent = 1;
	}
	else
	if (sscanf(lineStr, "Sense Key: %x", &key) == 1)
		;
	else
	if (sscanf(lineStr, "Starting to write CD/DVD at speed %d", &a) == 1)
	{
		if (CDstatus != NULL)
		{
			CDstatus->speed = a;
			if (CDstatus->speed == 0)
				CDstatus->speed = 1;
			if (strstr(lineStr, "multi session") != NULL)
				CDstatus->isClosed = 0;
			else
			if (strstr(lineStr, "single session") != NULL)
				CDstatus->isClosed = 1;

			time(&t0);
			t1 = t0;
			int numOfSectors = 4500
				+ (CDstatus->blocks1 - CDstatus->blocks2)
				+ 2250;
			if (CDstatus->blocks1 == CDstatus->blocks0)
				numOfSectors += 4500;
			t2 = t0 + 9 + numOfSectors / 75 / CDstatus->speed;
		}
	}
	else
	if (sscanf(lineStr, "Track %d: audio %d",
		&a, &b) == 2)
//		numOfSectors += (1024 * 1024 * b) / 2352;
		MBtotal += b;
	else
	if (sscanf(lineStr, "Track %d: data %d",
		&a, &b) == 2)
//		numOfSectors += (1024 * 1024 * b) / 2048;
		MBtotal += b;
	else
	if (sscanf(lineStr, "Track %d: %d of %d MB written (fifo %d",
		&a, &b, &c, &d) >= 3)
	{
		lineType = progress;
		if (b == c)
		{
			MBwritten += c;
			MBofTrackWritten = 0;
		}
		else
			MBofTrackWritten = b;
		fifoPercent = d;
	}

	return lineStr;
}
