key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
/*
**               j###t  ########## ####   ####
**              j###t   ########## ####   ####
**             j###T               "###L J###"
**          ######P'    ##########  #########
**          ######k,    ##########   T######T
**          ####~###L   ####
**          #### q###L  ##########   .#####
**          ####  \###L ##########   #####"
**
**  $Id$
**
**  Class History
**
**  Date        Name         Description
**  ---------|------------|-----------------------------------------------
**  24Aug98     subtle       start of recorded history
**
*/

package key.commands;

import key.*;
import key.util.Trie;
import java.io.IOException;

import java.util.Enumeration;
import java.util.StringTokenizer;

public class Link extends Command
{
	public Link()
	{
		setKey( "ln" );
		usage = "<object> <destination container>";
	}
	
	public void run( Player p, StringTokenizer args, String fullLine, CategoryCommand caller, InteractiveConnection ic, Flags flags ) throws IOException
	{
		String type;

		if( !args.hasMoreTokens() )
			usage( ic );
		else
		{
			String id=args.nextToken();
			if( !args.hasMoreTokens() )
				usage( ic );
			else
			{
				String to=args.nextToken();

				Object o = new Search( id, p.getContext() ).result;
				if( o != null )
				{
					if( o instanceof Atom )
					{
						Object r = new Search( to, p.getContext() ).result;
						if( r == null )
							ic.sendError( "Could not find destination '" + to + "'" );
						else if( r instanceof Container )
						{
							Container t = (Container)r;
							
							try
							{
								t.add( (Atom)o );
								
								if( t.isReference() )
									ic.sendFeedback( "A reference to " + ((Atom)o).getId() + " has been placed in " + t.getId() );
								else
									ic.sendFeedback( ((Atom)o).getName() + " has been moved to " + t.getId() + " (THIS MESSAGE OBSOLETE/INCORRECT - this code is now 'link', not move)" );
								//Log.log( "level", p.getName() + ", from context '" + p.getContext().getId() + "': moved " + id + " " + to );
							}
							catch( BadKeyException e )
							{
								throw new UnexpectedResult( e.toString() + " on removing a matched atom" );
							}
							catch( NonUniqueKeyException e )
							{
								ic.sendError( "There already exists an atom with this name in the destination container" );
								return;
							}
						}
						else
							ic.sendError( "'" + to + "' is not a Container, it is a " + Type.typeOf( r ).getName() );
					}
					else if( o instanceof Trie )
						ic.sendError( "Multiple matches: " + ((Trie)o).contents() );
					else
						ic.sendError( "'" + id + "' is not an atom, it is " + Type.typeOf( o ).getName() );
				}
				else
					ic.sendError( "Could not find '" + id + "'" );
			}
		}
	}
}