/*
 * Create.c
 *
$Author: dm3e $
$Date: 1992/12/01 23:44:01 $
$Revision: 1.12 $
$State: Exp $
$Locker:  $

/***********************************************************
        Copyright 1991 by Carnegie Mellon University

                      All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/

#include <stdio.h>
#include <strings.h>
#include <sys/time.h>
#include <afs/cmd.h>
#include "rxemt.h"
#include "globals.h"
#include "alloc.h"
#include "tools.h"
#include "hash.h"
#include "util.h"
#include "shell.h"
#include "emt_err.h"

#define INITIAL_VERSION 11

#define PARM_TREE (as->parms[EMT_STD_PARM].items->data)
#define PARM_COLLECTION (as->parms[EMT_STD_PARM+1].items->data)
#define PARM_VERSION (as->parms[EMT_STD_PARM+2].items->data)
#define PARM_SYSTEMS (as->parms[EMT_STD_PARM+3].items)
#define PARM_SITES (as->parms[EMT_STD_PARM+4].items)
#define PARM_SOURCE (as->parms[EMT_STD_PARM+5].items)
#define PARM_OBJECT (as->parms[EMT_STD_PARM+6].items)
#define PARM_LAST_VERSION (as->parms[EMT_STD_PARM+7].items)
#define PARM_USERS (as->parms[EMT_STD_PARM+8].items)
#define PARM_VISITORS (as->parms[EMT_STD_PARM+9].items)
#define PARM_SOURCEQ (as->parms[EMT_STD_PARM+10].items)
#define PARM_DESTQ (as->parms[EMT_STD_PARM+11].items)
#define PARM_OBJECTQ (as->parms[EMT_STD_PARM+12].items)
#define PARM_COMMONQ (as->parms[EMT_STD_PARM+13].items)
#define PARM_VERBOSE (as->parms[EMT_STD_PARM+14].items)
#define PARM_FORCE (as->parms[EMT_STD_PARM+15].items)

extern struct rx_connection *conn;

/***************************************************************************
 * [exported] emt_CreateCommand
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Wed Oct  7 15:53:18 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long emt_CreateCommand(as)
struct cmd_syndesc *as;
{
    register long code;
    register struct cmd_item *ti;
    char *errstr;
    struct rx_call *call;
    req_in_t request = (req_in_t)new_req_in();
    llist_t c_list;

    request->type = EMTD_CREATE;
    request->tree = copy_string(PARM_TREE);
    request->collection = copy_string(PARM_COLLECTION);
    request->environment = copy_string("");
    request->current_version = copy_string(PARM_VERSION);
    request->old_version = (PARM_LAST_VERSION) ?
      copy_string(PARM_LAST_VERSION->data) : copy_string("");
    request->path = copy_string("");
    c_list = NULL; 
    for(ti=PARM_USERS; ti; ti=ti->next) {
	new_llist(&c_list, ti->data, NULL, NULL);
	if (NULL == request->users) request->users = c_list;
    }
    c_list = NULL;
    for(ti=PARM_VISITORS; ti; ti=ti->next) {
	new_llist(&c_list, ti->data, NULL, NULL);
	if (NULL == request->visitors) request->visitors = c_list;
    }
    c_list = NULL;
    for(ti=PARM_SYSTEMS; ti; ti=ti->next) {
	new_llist(&c_list, ti->data, NULL, NULL);
	if (NULL == request->systems) request->systems = c_list;
    }
    c_list = NULL;
    for(ti=PARM_SITES; ti; ti=ti->next) {
	new_llist(&c_list, ti->data, NULL, NULL);
	if (NULL == request->sites) request->sites = c_list;
    }
    request->srcq = (long)((PARM_SOURCEQ) ? atoi(PARM_SOURCEQ->data) :
			   DEFAULT_SOURCEQ);
    request->destq = (long)((PARM_DESTQ) ? atoi(PARM_DESTQ->data) : 
			    DEFAULT_DESTQ);
    request->objq = (long)((PARM_OBJECTQ) ? atoi(PARM_OBJECTQ->data) :
			   DEFAULT_OBJECTQ);
    request->commq = (long)((PARM_COMMONQ) ? atoi(PARM_COMMONQ->data) :
			    DEFAULT_COMMONQ);
    
    request->flags |= CREATE_SOURCE_NEW;
    if (PARM_SOURCE) {
	if (!strcasecmp(PARM_SOURCE->data, "copy") && PARM_LAST_VERSION) {
	    request->flags |= CREATE_SOURCE_COPY;
	} else if (!strcasecmp(PARM_SOURCE->data, "none")) {
	    request->flags &= ~(CREATE_SOURCE_COPY | CREATE_SOURCE_NEW);
	}
    }
    if (PARM_OBJECT) {
	if (!strcasecmp(PARM_OBJECT->data, "afs")) {
	    request->flags |= CREATE_OBJECT_AFS;
	}
    }
    if (PARM_FORCE) request->flags |= FORCE_REQUEST;

    errstr = NULL;
    call = rx_NewCall(conn);
    code = StartEMT_QueueCommand(call, request);
    ReadWaiting(call, PARM_VERBOSE);
    code = EndEMT_QueueCommand(call, &errstr);
    code = rx_EndCall(call, code);
    if (code) return code;
    return 0;
}

#undef PARM_TREE
#undef PARM_COLLECTION
#undef PARM_VERSION
#undef PARM_SYSTEMS
#undef PARM_SOURCE
#undef PARM_OBJECT
#undef PARM_LAST_VERSION
#undef PARM_USERS
#undef PARM_SOURCEQ
#undef PARM_DESTQ
#undef PARM_OBJECTQ
#undef PARM_COMMONQ
#undef PARM_VERBOSE
