/*
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Peter Hofer <ph@desktopbsd.net>
 *			(C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#include "pkgupgradeengine.h"

#include <sys/utsname.h>

#include <sysctl.h>
#include <unixprocess.h>
#include <packagesettings.h>
#include <pkgconstants.h>

extern "C" int getosreldate(void);


PkgUpgradeEngine::PkgUpgradeEngine() {
	/* answerY = false; */
	keepGoing = false;
	installNewPkgs = false;
	recursion = NoRecursion;
	packageUse = UsePorts;
}


/** Loads the "packages" settings of the DesktopBSD configuration
  * using DBSDSettings.
  */
void  PkgUpgradeEngine::loadSettings() {
	PackageSettings s;

	int  rec = 0;
	if(s.getUpwardRecursive())
		rec |= UpwardRecursive;

	if(s.getRecursive())
		rec |= Recursive;

	if(s.getKDialogOptions())
		addMakeEnv("DIALOG=/usr/local/bin/kdialog-ports-wrapper");

	setRecursion(rec);
	setKeepGoing(s.getKeepGoing());
	setPackageUse((PackageUse)  s.getPackageUse());
	addMakeArgs(s.getMakeArguments());
	addMakeEnv(s.getMakeEnvironment());
}



/** Returns a QProcess that applies the specified
  * actions on the given list of packages or NULL
  * if not packages for upgrading were specified.
  *
  * The returned QProcess must be deleted after use.
  */
UnixProcess * PkgUpgradeEngine::getProcess() {
	/* Stop here when no packages are specified */
	if(packages.isEmpty())
		return  NULL;


	UnixProcess  *process = new UnixProcess(QString(PORTUPGRADE_CMD));
	process->setWorkingDirectory(QDir(PACKAGES_DIRECTORY));

	if(recursion & Recursive)
		process->addArgument("-r");
	if(recursion & UpwardRecursive)
		process->addArgument("-R");

	if(installNewPkgs)
		process->addArgument("-N");

	if(keepGoing)
		process->addArgument("-k");

	switch(packageUse) {
		case UsePackages:
			process->addArgument("-P");
			break;

		case UsePackagesOnly:
			process->addArgument("-PP");
			break;

		default: break;
	}

	if(!makeArgs.isEmpty()) {
		process->addArgument("-m");
		process->addArgument(makeArgs.join(" "));
	}

	if(!makeEnv.isEmpty()) {
		process->addArgument("-M");
		process->addArgument(makeEnv.join(" "));
	}

	Package  *pkg = packages.first();
	while(pkg != NULL) {
		process->addArgument(pkg->getID());

		pkg = packages.next();
	}

	process->setEnvironment(getEnvironment());
	return  process;
}


QString PkgUpgradeEngine::readDefaultRepository() {
	const struct {
		int from;     /* Lowest version number to match */
		int to;      /* Highest version number to match */
		const char *repository;  /* Directory it lives in */
	} releases[] = {
		{ 300000, 399000, "3-stable" },
		{ 400000, 499000, "4-stable" },
		{ 502100, 502128, "5-current" },
		{ 503100, 599000, "5-stable" },
		{ 600100, 699000, "6-stable" },
		{ 700000, 799000, "7-stable" },
		{ 0, 9999999, "current" },
		{ 0, 0, NULL }
	};

	int release = getosreldate();
	for(int i = 0; releases[i].repository != NULL; ++i)
		if(release >= releases[i].from && release <= releases[i].to)
			return releases[i].repository;

	return "";
}


QStringList PkgUpgradeEngine::getEnvironment() {
	/* Find out architecture */
	struct utsname u;
	uname(&u);

	/* Server package directory */
	QString packageDirectory = PackageSettings().getPackageDirectory();
	
	QString site = "PACKAGESITE=";
	if(packageDirectory.startsWith("ftp://") || packageDirectory.startsWith("http://"))
		site += packageDirectory;
	else {
		site += QString("ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/%1/packages-%2/Latest/")
						.arg(u.machine)
						.arg(packageDirectory.isEmpty() ? readDefaultRepository() : packageDirectory);
	}
				  
	return QStringList(site);
}
