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

#include <qstring.h>

#include <executor.h>

QString  RCConf::getVar(const QString &  varName) {
	static const QString cmd = "/usr/bin/sed -n 's/^[ \t]*%1=\"\\(.*\\)\"[ \t]*$/\\1/p' /etc/rc.conf"
										"| /usr/bin/tail -n1";

	return  Executor::strFromCmd(cmd.arg(varName));
}


bool  RCConf::unsetVar(const QString &  varName) {
	static const QString cmd = "/usr/bin/sed -i '' '/^[ \t]*\\(%1=\".*\"\\)*[ \t]*$/d' /etc/rc.conf";

	return  Executor::exec(cmd.arg(varName)) == Executor::NoError;
}


/** This sets an /etc/rc.conf variable to a specified value. */
bool RCConf::setVar(const QString& name, const QString& value) {
	static const QString cmd = "/usr/bin/sed -i '' '$a\\\n%1=\"%2\"\n"
										"/^[ \t]*\\(%3=\".*\"\\)*[ \t]*$/d' /etc/rc.conf";

	return  Executor::exec(cmd.arg(name)
									  .arg(value)
									  .arg(name)) == Executor::NoError;
}
