/**
 * Part of the CCNx command line utilities
 *
 * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc.
 *
 * This work is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation. 
 * This work is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details. You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

package org.ccnx.ccn.utils;

import java.io.File;

import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.config.UserConfiguration;
import org.ccnx.ccn.profiles.namespace.NamespaceManager;
import org.ccnx.ccn.profiles.security.access.group.GroupAccessControlManager;
import org.ccnx.ccn.protocol.ContentName;

public class CommonSecurity {
	
	public static void setUser(String pathToKeystore) {
		File userDirectory = new File(pathToKeystore);
		String userConfigDir = userDirectory.getAbsolutePath();
		System.out.println("Loading keystore from: " + userConfigDir);
		UserConfiguration.setUserConfigurationDirectory(userConfigDir);
		// Assume here that the name of the file is the userName
		String userName = userDirectory.getName();
		if (userName != null) {
			System.out.println("User: " + userName);
			UserConfiguration.setUserName(userName);
		}
	}
	
	public static void setAccessControl() {
		// register a group access control manager with the namespace manager
		try {
			GroupAccessControlManager gacm = new GroupAccessControlManager(ContentName.fromNative("/"), 
					CommonParameters.groupStorage, CommonParameters.userStorage, CCNHandle.open());
			NamespaceManager.registerACM(gacm);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}

@@ -884,65 +884,8 @@
 on;%0A
-import org.ccnx.ccn.profiles.namespace.NamespaceManager;%0A
 impo
@@ -1672,16 +1672,149 @@
 %09%09try %7B%0A
+%09%09%09// TODO -- broken  -- turns on access control probably before we've done -as for user.%0A%09%09%09CCNHandle ourHandle = CCNHandle.open();%0A
 %09%09%09Group
@@ -1901,16 +1901,16 @@
 (%22/%22), %0A
-
 %09%09%09%09%09Com
@@ -1993,36 +1993,59 @@
 %0A%09%09%09
-Namespace
+ourHandle.key
 Manager
+()
 .re
-gisterACM
+memberAccessControlManager
 (gac

move to new API, but this has state dependence that is ugly, client programs using it are likely to have real problems.