/*****************************************************************************
 *
 *  core.cpp -- Core stuff.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the JobManager module of the DVDx 4 application.
 *
 *  GNU Lesser General Public License Usage
 *  This file may be used under the terms of the GNU Lesser General
 *  Public License version 2.1 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.LGPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU Lesser General Public License version 2.1 requirements
 *  will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 *  GNU General Public License Usage
 *  Alternatively, this file may be used under the terms of the GNU
 *  General Public License version 3.0 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.GPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU General Public License version 3.0 requirements will be
 *  met: http://www.gnu.org/copyleft/gpl.html.
 *
 *****************************************************************************/

#include "../encoder/ffmpeg.h"
#include "../encoder/ffprobe.h"
#include "../encoder/mencoder.h"
#include <QCoreApplication>
#include <QDir>
#include <QSettings>
#include <QString>

extern Console *console;
extern FFmpeg *ffmpeg;
extern FFprobe *ffprobe;
extern MEncoder *mencoder;

#ifdef WIN32
#define DEFAULTFFMPEGPATH		"ffmpeg/ffmpeg.exe" 
#define DEFAULTFFPROBEPATH		"ffmpeg/ffprobe.exe" 
#define DEFAULTMENCODERPATH		"mplayer/mencoder.exe" 
#define DEFAULTMPLAYERPATH		"mplayer/mplayer.exe" 
#define DEFAULTMEDIAINFOPATH	"mediainfo/mediainfo.exe" 
#elif defined(Q_OS_MAC)
#define DEFAULTFFMPEGPATH		"/opt/dvdx4/bin/ffmpeg"
#define DEFAULTFFPROBEPATH		"/opt/dvdx4/bin/ffprobe"
#define DEFAULTMENCODERPATH		"/opt/local/bin/mencoder"
#define DEFAULTMPLAYERPATH		"/opt/local/bin/mplayer"
#define DEFAULTMEDIAINFOPATH	"/opt/dvdx4/bin/mediainfo"
#else
#define DEFAULTFFMPEGPATH		"/opt/dvdx4-open-edition/bin/ffmpeg"
#define DEFAULTFFPROBEPATH		"/opt/dvdx4-open-edition/bin/ffprobe"
#define DEFAULTMENCODERPATH		"mencoder"
#define DEFAULTMPLAYERPATH		"mplayer"
#define DEFAULTMEDIAINFOPATH	"/opt/dvdx4-open-edition/bin/mediainfo"
#endif

namespace Core 
{

void start()
{
	QSettings settings;

	// Create interfaces and main console
	console = new Console();
	ffmpeg = new FFmpeg();
	ffprobe = new FFprobe();
	mencoder = new MEncoder();

	// Read core settings and start interfaces
	settings.beginGroup("Core");
	settings.beginGroup("Interfaces");

	QDir dir;
	QString libraryPath, path;

	// FFmpeg
#ifdef WIN32
	path = settings.value("ffmpeg", QCoreApplication::applicationDirPath() + "/" + DEFAULTFFMPEGPATH).toString();
#else
	path = settings.value("ffmpeg", DEFAULTFFMPEGPATH).toString();
#endif

	libraryPath.clear(); 
#ifdef Q_OS_LINUX
	if (settings.contains("libffmpeg"))
	{
		libraryPath = settings.value("libffmpeg").toString();
	}
	else
	{
		// Linux version got its own FFmpeg build with its own library in binary path/../lib
		dir.setPath(path);
		dir.cdUp();
		dir.cdUp();
		dir.cd("lib");
		libraryPath = dir.absolutePath();
	}
#endif

	ffmpeg->interfaceStart(path, libraryPath);

	// FFprobe
#ifdef WIN32
	path = settings.value("ffprobe", QCoreApplication::applicationDirPath() + "/" + DEFAULTFFPROBEPATH).toString();
#else
	path = settings.value("ffprobe", DEFAULTFFPROBEPATH).toString();
#endif

	libraryPath.clear(); 
#ifdef Q_OS_LINUX
	if (settings.contains("libffprobe"))
	{
		libraryPath = settings.value("libffprobe").toString();
	}
	else
	{
		// Linux version got its own FFmpeg build with its own library in binary path/../lib
		dir.setPath(path);
		dir.cdUp();
		dir.cdUp();
		dir.cd("lib");
		libraryPath = dir.absolutePath();
	}
#endif

	ffprobe->interfaceStart(path, libraryPath);

	// Mencoder
#ifdef WIN32
	path = settings.value("mencoder", QCoreApplication::applicationDirPath() + "/" + DEFAULTMENCODERPATH).toString();
#else
	path = settings.value("mencoder", DEFAULTMENCODERPATH).toString();
#endif

	mencoder->interfaceStart(path);

	settings.endGroup();  // Interfaces
	settings.endGroup();  // Core
}

void stop()
{
	QSettings settings;

	// Save core settings
	settings.beginGroup("Core");
	settings.beginGroup("Interfaces");
	settings.setValue("ffmpeg", ffmpeg->interfacePath());
	settings.setValue("ffprobe", ffprobe->interfacePath());
	//settings.setValue("mediainfo", mediainfo->interfacePath());
	settings.setValue("mencoder", mencoder->interfacePath());
	//settings.setValue("mplayer", mplayer->interfacePath());
	settings.endGroup();
	settings.endGroup();
}

} // namespace Core 
