
// Project: rcdrecord - Network CD Writing
// Author: Jörg Haeger, 06.11.2000
//	Joerg.Haeger@Uni-Bielefeld.de

#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
#include "CommandArgs.h"
#include "Client2.h"

char userID[1024], userPassword[1024];

void readLoginFile()
{
	// set defaults
	sprintf(userID, "%d", getuid());
	struct timeval timeVal;
	gettimeofday(&timeVal, NULL);
	srandom(timeVal.tv_usec);
	sprintf(userPassword, "%d", random());

	const char *HOME = getenv("HOME");
	char loginFile[1024];
	if (HOME == NULL)
		return;

	sprintf(loginFile, "%s/.webCDwriter", HOME);
	FILE *inStream = fopen(loginFile, "r");
	if (inStream != NULL)
	{
		char lineStr[1024];
		while (fgets(lineStr,
			sizeof lineStr, inStream) != NULL)
		{
			char str1[1024], str2[1024];
			if (sscanf(lineStr, "%[^=] = %s",
				str1, str2) < 2)
				continue;
			else
			if (strcasecmp(str1, "userID") == 0)
				strcpy(userID, str2);
			else
			if (strcasecmp(str1, "userPassword") == 0)
				strcpy(userPassword, str2);
		}
		fclose(inStream);
	}
	else
	{
		umask(S_IRWXG | S_IRWXO);
		FILE *outStream = fopen(loginFile, "w");
		if (outStream != NULL)
		{
			fprintf(outStream, "userID=%s\n", userID);
			fprintf(outStream, "userPassword=%s\n",
				userPassword);
			fclose(outStream);
		}
	}
}

int main(int argc, char *argv[])
{
	readLoginFile();
	try {
		CommandArgs args(argc, argv);

		// make sure files and tracks are not mixed
		int files = 0, tracks = 0;
		for (int i = 0; i < args.numOfFiles; i++)
			if (strcmp(args.getType(i), "file") == 0)
				files++;
			else
				tracks++;
		if (files > 0 && tracks > 0)
			throw "Cannot mix files and tracks!";

		printf("Trying to connect to CDWserver...\n");
		Client2 client;
		client.connect(args.host, args.port);
		client.client(product, version);

		struct utsname unameBuf;
		uname(&unameBuf);
		client.system(unameBuf.sysname, unameBuf.release);

		struct passwd *pw = getpwuid(getuid());
		const char *userName;
		if (pw == NULL)
			userName = "";
		else
			userName = pw->pw_name;
		client.user(getuid(), userName);

		client.setProtocol(2);
		client.login(userID, userPassword);
		client.setPrefix(args.prefix);
		if (args.verbose)
			client.setVerbose();

		// run cdrecord blank if blank != fast
		int blankTOC = strcmp(args.blank, "fast") == 0;
		if (args.blank[0] != 0 && !blankTOC)
		{
			printf("\nRunning cdrecord blank=%s...\n", args.blank);
			client.blank(args.blank);
			printf("%c", 7);
		}

		printf("\nTrying to open a session...\n");
		if (args.repeat)
			client.openSession("");
		else
			client.newSession("");

		if (args.numOfFiles > 0)
		{
			if (args.dir[0] != 0)
				if (chdir(args.dir) != 0)
					throw new Error(
						"Cannot change to %s", args.dir);

			if (args.force)
				client.setForce();

			printf("Transmitting the files...\n");
			for (int i = 0; i < args.numOfFiles; i++)
				if (strcmp(args.getType(i), "file") == 0)
					client.putFile(args.getTrack(i));
				else
					client.putTrack(
						args.getType(i),
						args.getTrack(i));
			printf("%d files (%.3f MB) were transmitted.\n",
				client.numOfFiles,
				client.numOfBytes / 1024.0 / 1024);
			if (client.numOfCannotRead > 0)
				printf("%d unreadable files were ignored.\n",
					client.numOfCannotRead);
			if (client.numOfDoesNotExist > 0)
				printf("%d nonexistent files were ignored.\n",
					client.numOfDoesNotExist);
			printf("\n");
		}

		if (args.numOfFiles > 0 || args.repeat)
		{
			client.setFilesystem(
				args.joliet, args.rockRidge, args.hfs);
			client.burnSession(
				blankTOC,
				!args.multi,
				args.dummy,
				args.speed);
		}
	}
	catch (const char *msg)
	{
		printf("error: %s\n", msg);
	}
	catch (Error *error)
	{
		printf("error: %s\n", error->toString());
	}
	printf("%c", 7);

	return 0;
}
