/*
 * delete.c
 *
 * $Author: dm3e $
 * $Date: 1993/01/08 19:21:43 $
 * $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_DeleteCommand
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Thu Oct 15 09:33:07 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_DeleteCommand(request, errstr)
comreq_t            request;
char **errstr;
{
  register long       code;

  IF_RTN(emtd_DeleteDestArea(request, errstr));
  IF_RTN(emtd_DeleteObjectArea(request, errstr));
  IF_RTN(emtd_DeleteSourceAndCommonAreas(request, errstr));
  emtd_Broadcast(request, EMTD_LOGBASE, "Deletion complete\n");
  return OK;
}

/***************************************************************************
 * [exported] emtd_TitleDelete
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Fri Nov 13 16:34:35 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_TitleDelete(request, errstr)
comreq_t            request;
char **errstr;
{
    IF_RTN(emtd_Broadcast(request, EMTD_LOGBASE, "Deleting %s %s.\n",
			  REQ_COLLECTION, REQ_VERSION));
    return OK;
}


/***************************************************************************
 * [private] emtd_DeleteSourceAndCommonAreas
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Thu Oct 15 09:41:11 1992 - created by David Allen Markley
 *
 ***************************************************************************/
static long emtd_DeleteSourceAndCommonAreas(request, errstr)
comreq_t            request;
char **errstr;
{
    register long       code;
    char               *srcvol,
                       *rcsvol,
                       *comvol,
                       *vol,
                       *srvr,
                       *part,
                       *sys;
    llist_t c_system;
    long do_all = 0;
    long no_more_dests = 0;
    long no_more_objs = 0;
    long volid, sites;
    edb_sys_t c_sys;

    srcvol = make_volume_name (NULL, REQ_COLLECTION, REQ_VERSION, SRC_VOL);
    rcsvol = make_volume_name (NULL, REQ_COLLECTION, REQ_VERSION, SRC_VOL);
    comvol = make_volume_name("common", REQ_COLLECTION, REQ_VERSION,
			      COMMON_VOL);

    for (c_system = REQ_SYSTEMS; c_system; c_system = c_system->next) {
	if (!strcasecmp (c_system->buffA, "all")) do_all++;
    }
    if (do_all && (REQ_FLAGS & DELETE_DESTS)) {
	no_more_dests++;
    } else {
	no_more_dests++;
	for (c_sys = request->tree->dest->systems; c_sys; c_sys = c_sys->next){
	    sys = c_sys->name;
	    vol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION, DEST_VOL);
	    code = emt_GetVolumeInfo (vol, &srvr, &part, &volid, &sites);
	    if (VL_NOENT != code) {
		no_more_dests = 0;
		emtd_Broadcast(request, EMTD_LOGERR,
			       "Destination volume %s still exists.\n", vol);
	    }
	    free(vol);
	}
	if (! no_more_dests) {
	    emtd_Broadcast(request, EMTD_LOGERR,
			   "Source and Common volumes not deleted.\n");
	    return OK;
	}
    }
    if (no_more_dests) {
	IF_RTN(emtd_VolumePurge(request, comvol, "common", COMMON_VOL,errstr));
    }
    if (do_all && (REQ_FLAGS & DELETE_OBJECTS)) {
	no_more_objs++;
    } else {
	no_more_objs++;
	for (c_sys = request->tree->obj->systems; c_sys; c_sys = c_sys->next) {
	    sys = c_sys->name;
	    vol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION, OBJ_VOL);
	    code = emt_GetVolumeInfo (vol, &srvr, &part, &volid, &sites);
	    if (VL_NOENT != code) {
		no_more_objs = 0;
		emtd_Broadcast(request, EMTD_LOGERR,
			       "Object volume %s still exists.\n", vol);
	    }
	    free(vol);
	}
	if (! no_more_objs) {
	    emtd_Broadcast(request, EMTD_LOGERR,
			   "Source volume not deleted.\n");
	}
    }
    if (no_more_dests && no_more_objs && (REQ_FLAGS & DELETE_SOURCE)) {
	emtd_Broadcast(request, EMTD_LOGBASE, "Deleting the source area.\n");
	IF_RTN(emtd_VolumePurge(request, rcsvol, "src", SRC_VOL, errstr));
	IF_RTN(emtd_VolumePurge(request, srcvol, "src", SRC_VOL, errstr));
	IF_RTN(emtd_VolumeUnmount(request, srcvol, "src",
				  request->tree->src_path, SRC_VOL, errstr));
    }
    return OK;
}

/***************************************************************************
 * [private] emtd_DeleteDestArea
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Thu Oct 15 09:54:36 1992 - created by David Allen Markley
 *
 ***************************************************************************/
static long emtd_DeleteDestArea(request, errstr)
comreq_t            request;
char **errstr;
{
    register long       code;
    char               *vol,
                       *sys;
    long do_all = 0;
    edb_sys_t c_sys;
    llist_t c_system;

    if (! (REQ_FLAGS & DELETE_DESTS)) return OK;
    emtd_Broadcast(request, EMTD_LOGBASE, "Deleting the destination areas.\n");
    for (c_system = REQ_SYSTEMS; c_system; c_system = c_system->next) {
	if (!strcasecmp (c_system->buffA, "all")) do_all++;
    }

    if (do_all) {
	for (c_sys = request->tree->dest->systems; c_sys; c_sys = c_sys->next){
	    sys = c_sys->name;
	    vol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION, DEST_VOL);
	    IF_RTN(emtd_VolumePurge(request, vol, sys, DEST_VOL, errstr));
	    IF_RTN(emtd_VolumeUnmount(request, vol, sys, c_sys->path,
				      DEST_VOL, errstr));
	    free (vol);
	}
    } else {
	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_VolumePurge(request, vol, sys, DEST_VOL, errstr));
	    IF_RTN(emtd_VolumeUnmount(request, vol, sys, c_sys->path,
				      DEST_VOL, errstr));
	    free (vol);
	}
    }
    return OK;
}

/***************************************************************************
 * [private] emtd_DeleteObjectArea
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Thu Oct 15 10:19:04 1992 - created by David Allen Markley
 *
 ***************************************************************************/
static long emtd_DeleteObjectArea(request, errstr)
comreq_t            request;
char **errstr;
{
    register long       code;
    char               *vol,
                       *sys;
    long do_all = 0;
    edb_sys_t c_sys;
    llist_t c_system;

    if (! (REQ_FLAGS & DELETE_OBJECTS)) return OK;
    emtd_Broadcast(request, EMTD_LOGBASE, "Deleting the object areas.\n");
    for (c_system = REQ_SYSTEMS; c_system; c_system = c_system->next) {
	if (!strcasecmp (c_system->buffA, "all")) do_all++;
    }

    if (do_all) {
	for (c_sys = request->tree->obj->systems; c_sys; c_sys = c_sys->next) {
	    sys = c_sys->name;
	    vol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION, OBJ_VOL);
	    IF_RTN(emtd_VolumePurge(request, vol, sys, OBJ_VOL, errstr));
	    IF_RTN(emtd_VolumeUnmount(request, vol, sys, c_sys->path,
				      OBJ_VOL, errstr));
	    free (vol);
	}
    } else {
	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->obj, sys))) {
		emtd_Broadcast(request, EMTD_LOGERR, "Unknown system: %s\n",
			       sys);
		continue;
	    }
	    vol = make_volume_name(sys, REQ_COLLECTION, REQ_VERSION, OBJ_VOL);
	    IF_RTN(emtd_VolumePurge(request, vol, sys, OBJ_VOL, errstr));
	    IF_RTN(emtd_VolumeUnmount(request, vol, sys, c_sys->path,
				      OBJ_VOL, errstr));
	    free (vol);
	}
    }
    return OK;
}
