/*
 * 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 "portscleanengine.h"

#include <qstring.h>

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

/** Returns a QProcess that performs the specified
  * clean actions.
  *
  * Returns NULL when no flags are specified.
  *
  * The returned QProcess must be deleted after use.
  */
UnixProcess * PortsCleanEngine::getProcess(int f) {
	/* Stop here when no flags are specified */
	if(f == 0)
		return NULL;

	UnixProcess  *process = new UnixProcess(QString(PORTSCLEAN_CMD));

	if(f & WorkClean)
		process->addArgument("-C");

	if(f & LibClean)
		process->addArgument("-L");

	if(f & DistCleanNotReferenced  &&  !(f & DistCleanNotInstalled))
		process->addArgument("-D");
	else if(f & DistCleanNotInstalled)
		process->addArgument("-DD");

	if(f & PkgCleanOutdated && !(f & PkgCleanAll))
		process->addArgument("-P");
	else if(f & PkgCleanAll)
		process->addArgument("-PP");

	return  process;
}

/** Returns a Process that deletes all distfiles in /usr/ports/distfiles. */
UnixProcess * PortsCleanEngine::getRemoveDirProcess(const QString &dir) {
	QDir d(dir);

	QStringList args;
	args.append("/bin/rm"); args.append("-vrf");
	args += d.entryList("[^.]*"); /* Filter ".", ".." and hidden files */

	UnixProcess *p = new UnixProcess(args);
	p->setWorkingDirectory(d);
	return p;
}

/** Returns a Process that deletes all distfiles in /usr/ports/distfiles. */
UnixProcess * PortsCleanEngine::getRemoveAllDistfilesProcess() {
	return getRemoveDirProcess(DISTFILES_DIRECTORY);
}

/** Returns a process that deletes all packages in /usr/ports/packages */
UnixProcess * PortsCleanEngine::getRemoveAllPkgsProcess() {
	return getRemoveDirProcess(PACKAGES_DIRECTORY);
}
