/*
 * release.c
 *
 * $Author: dm3e $
 * $Date: 1993/01/08 19:23:06 $
 * $Revision: 1.2 $
 * $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 "emtd.h"

/***************************************************************************
 * [exported] emtd_ReleaseCommand
 *
 * Description:
 *      This is the top level procedure that performs the release operation
 *      on the server side of things.
 *
 * Arguments:
 *      request - The request structure associated with caller of this proc.
 *      errstr - Where an error string could be returned.
 *
 * Environment:
 *
 * History:
 *      Mon Oct 19 15:13:23 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_ReleaseCommand(request, errstr)
comreq_t            request;
char **errstr;
{
  register long       code;
  char                buffer[MAXBUFSIZE];
  struct timeval  tp;
  llist_t c_system;

/*
 * Check the data that's been supplied.
 */
  if (NULL == request->env) {
      emtd_Broadcast(request, EMTD_LOGERR, "Unknown environment: %s.\n",
		     REQ_ENVIRONMENT);
      return EMTD_BAD_ENVIRONMENT;
  }

  IF_RTN(emtd_LockForRelease(request, errstr)); 
  IF_RTN(emtd_RunDepotAndRelease(request, errstr));
  emtd_Broadcast(request, EMTD_LOGBASE, "Release complete.\n");
  return OK;
}

/***************************************************************************
 * [exported] emtd_TitleRelease
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Fri Nov 13 16:37:00 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_TitleRelease(request, errstr)
comreq_t            request;
char **errstr;
{
    char                buffer[MAXBUFSIZE];
    llist_t c_system;
    
    sprintf(buffer, "Releasing %s %s to %s %s for ", REQ_COLLECTION,
	    REQ_VERSION, REQ_ENVIRONMENT, REQ_TREE);
    for (c_system = REQ_SYSTEMS; c_system; c_system = c_system->next) {
	strcat(buffer, c_system->buffA);
	if (NULL == c_system->next) {
	    strcat(buffer, ".");
	} else if (NULL == c_system->next->next) {
	    if (REQ_SYSTEMS == c_system) {
		strcat(buffer, " and ");
	    } else {
		strcat(buffer, ", and ");
	    }
	} else {
	    strcat(buffer, ", ");
	}
    }
    strcat(buffer, "\n");
    emtd_Broadcast(request, EMTD_LOGBASE,  buffer);
    return OK;
}

/***************************************************************************
 * [exported] emtd_LockForRelease
 *
 * Description:
 *      Locks the source and dest areas, if requested, in preparation for
 *      the release to some environment.
 *
 * Arguments:
 *      request - The request structure associated with caller of this proc.
 *      errstr - Where an error string could be returned.
 *
 * Environment:
 *
 * History:
 *      Mon Oct 19 15:40:33 1992 - created by David Allen Markley
 *
 ***************************************************************************/
static long emtd_LockForRelease(request, errstr)
comreq_t            request;
char **errstr;
{
    register long       code;
    llist_t c_system;
    char *vol,
    *sys;
    edb_sys_t c_sys;
    long common_done = 0;

    if (REQ_FLAGS & LOCK_SOURCE) {
	vol = make_volume_name (NULL, REQ_COLLECTION, REQ_VERSION, SRC_VOL);
	IF_RTN(emtd_VolumeSetACL(request, request->tree->src_path,
				 SRC_VOL | LOCK_VOLUME, errstr));
	free(vol);
    }
    if (! (REQ_FLAGS & LOCK_DESTS)) return OK;
    for (c_system = REQ_SYSTEMS; c_system; c_system = c_system->next) {
	sys = c_system->buffA;
	if (!strcasecmp (sys, "common")) continue;
	if (NULL == (c_sys = get_system (request->tree->dest, sys))) {
	    emtd_Broadcast(request, EMTD_LOGERR, "Unknown system: %s\n", sys);
	    continue;
	}
	vol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION, DEST_VOL);
	IF_RTN(emtd_VolumeSetACL(request, c_sys->path, DEST_VOL | LOCK_VOLUME,
				 errstr));
	free(vol);
	if (! common_done) {
	    common_done++;
	    vol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION,
				   COMMON_VOL);
	    code = emtd_VolumeSetACL(request, c_sys->path, COMMON_VOL |
				     DEST_VOL | LOCK_VOLUME, errstr);
	    switch (code) {
	    case VL_NOENT:
	    case ENOENT:
		break;
	    default:
		emtd_Broadcast(request, EMTD_LOGERR,
			       "Trouble setting ACL on the common volume.\n");
		return code;
	    }
	}
    }
    return OK;
}

/***************************************************************************
 * [exported] emtd_RunDepotAndRelease
 *
 * Description:
 *      Does everything necessarry finish the release requested. This runs
 *      depot on the appropriate environments, and replicates or releases the
 *      correct volumes.
 *
 * Arguments:
 *      request - The request structure associated with caller of this proc.
 *      errstr - Where an error string could be returned.
 *
 * Environment:
 *
 * History:
 *      Mon Oct 19 16:11:57 1992 - created by David Allen Markley
 *
 ***************************************************************************/
static long emtd_RunDepotAndRelease(request, errstr)
comreq_t            request;
char **errstr;
{
    register long       code;
    llist_t c_system;
    char *vol,
    *cvol,
    *sys;
    edb_sys_t c_sys;
    edb_rep_t c_rep;
    long depot_code = 0;
    long common_done = 0;

    for (c_system = REQ_SYSTEMS; c_system; c_system = c_system->next) {
	sys = c_system->buffA;
	if (!strcasecmp (sys, "common")) continue;
	if (NULL == (c_sys = get_system (request->env, sys))) {
	    emtd_Broadcast(request, EMTD_LOGERR, "Unknown system: %s\n", sys);
	    continue;
	}
	vol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION, DEST_VOL);

#ifndef USE_TIMESTAMPS
	IF_RTN(emtd_VolumeUnmount(request, vol, sys, c_sys->path, 0 ,errstr));
#endif	/* USE_TIMESTAMPS */
	IF_RTN(emtd_VolumeMount(request, vol, sys, c_sys->path, 0, errstr));

	if (REQ_FLAGS & DO_DEPOT) {
	    depot_code = emtd_Depot(request, sys, DEPOT_QUICK | DEPOT_VERBOSE,
				    errstr);
	    switch (depot_code) {
	    case 0:
		break;
	    default:
		return code;
	    }
	}
	if (depot_code) {
	    emtd_Broadcast(request, EMTD_LOGERR, "%s not released.\n", vol);
	    free(vol);
	    continue;
	}
	if (c_sys->replicate && (REQ_FLAGS & RELEASE_LOWER)) {
	    IF_RTN(emtd_VolumeReplicate(request, vol, sys, errstr));
	    if (! common_done) {
		common_done++;
		cvol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION,
					COMMON_VOL);
		code = emtd_VolumeReplicate(request, cvol, "common", errstr);
		free(cvol);
		if (code && VL_NOENT != code) return code;
	    }
	} else if (REQ_FLAGS & RELEASE_LOWER) {
	    IF_RTN(emtd_VolumeRelease(request, vol, errstr));
	    if (! common_done) {
		common_done++;
		cvol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION,
					COMMON_VOL);
		free(cvol);
		code = emtd_VolumeRelease(request, cvol, errstr);
		if (code && VL_NOENT != code) return code;
	    }
	}
	free(vol);   
	if (REQ_FLAGS & RELEASE_UPPER) {
	    for (c_rep = c_sys->rep_vols; c_rep; c_rep = c_rep->next) {
		IF_RTN(emtd_VolumeRelease(request, c_rep->name, errstr));
	    }
	}
    }
    return OK;
}
