/*
 * remove.c
 * 
 * $Author: dm3e $
 * $Date: 1993/02/17 16:24:38 $
 * $Revision: 1.3 $
 * $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_RemoveCommand
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Tue Oct 20 14:12:02 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_RemoveCommand(request, errstr)
struct comreq *request;
char **errstr;

{
    register long       code;
    char                buffer[MAXBUFSIZE];
    llist_t c_system;
    struct timeval tp;

/*
 * 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_RemoveFromDepot(request, errstr));
    emtd_Broadcast(request, EMTD_LOGBASE, "Remove complete.\n");
    return OK;
}

/***************************************************************************
 * [exported] emtd_TitleRemove
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Fri Nov 13 16:38:43 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_TitleRemove(request, errstr)
struct comreq *request;
char **errstr;

{
    char                buffer[MAXBUFSIZE];
    llist_t c_system;

    sprintf(buffer, "Removing %s from %s %s for ", REQ_COLLECTION,
	    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_AddIgnore
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Mon Nov 16 13:54:36 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_AddIgnore(request, base_path, errstr)
comreq_t request;
char *base_path;
char **errstr;
{
    register long       code;
    char in_path[MAXPATHLEN], out_path[MAXPATHLEN];
    char buffer[MAXBUFSIZE];
    char *name;
    FILE *custom_in, *custom_out;
    llist_t ignored = NULL, c_ignore = NULL;
    int pos, already_ignored = 0;

    sprintf(in_path, "%s/depot/custom.depot", base_path);
    if (NULL == (custom_in = fopen(in_path, "r"))) {
	emtd_Broadcast(request, EMTD_LOGERR, "Could not open:\n\t%s\n", in_path);
	return EMTD_OPEN_FILE_ERR;
    }
    sprintf(out_path, "%s/depot/custom.depot.NEW", base_path);
    if (NULL == (custom_out = fopen(out_path, "w"))) {
	emtd_Broadcast(request, EMTD_LOGERR, "Could not open:\n\t%s\n", out_path);
	fclose(custom_in);
	return EMTD_OPEN_FILE_ERR;
    }
    while (NULL != fgets (buffer, MAXBUFSIZE, custom_in)) {
	if (strncmp(buffer, "ignore:", strlen("ignore:"))) {
	    fputs(buffer, custom_out);
	    fflush(custom_out);
	    continue;
	}
	pos = strlen("ignore:");
	skip_whitespace(&pos, buffer);
	while (NULL != (name = next_string_d(&pos, buffer, ','))) {
	    new_llist(&c_ignore, NULL, NULL, NULL);
	    Log("Ignored: %s\n", name);
	    if (NULL == ignored) ignored = c_ignore;
	    c_ignore->buffA = name;
	}
    }
    fclose(custom_in);
    for (c_ignore = ignored; c_ignore; c_ignore = c_ignore->next) {
	fprintf(custom_out, "ignore: %s\n", c_ignore->buffA);
	fflush(custom_out);
	if (! strcmp(c_ignore->buffA, REQ_COLLECTION)) already_ignored++;
    }
    if (! already_ignored) fprintf(custom_out, "ignore: %s\n", REQ_COLLECTION);
    fflush(custom_out);
    fclose(custom_out);
    if (code = rename(out_path, in_path)) {
	emtd_Broadcast(request, EMTD_LOGERR,
		       "Trouble moving custom.depot.NEW to custom.depot (%ld)\n", errno);
	return errno;
    }
    return OK;
}

/***************************************************************************
 * [exported] emtd_RemoveFromDepot
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Mon Nov 16 13:52:24 1992 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_RemoveFromDepot(request, errstr)
comreq_t            request;
char **errstr;
{
    register long       code;
    llist_t c_system;
    char *sys;
    edb_sys_t c_sys;
    edb_rep_t c_rep;
    struct timeval tp;
    long depot_code = 0;
    char path[MAXBUFSIZE];    
    FILE *db;

    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;
	}
	IF_RTN(emtd_AddIgnore(request, c_sys->path, errstr));

#ifndef USE_TIMESTAMPS
	IF_RTN(emtd_VolumeUnmount(request, vol, sys, c_sys->path, 0 ,errstr));
#else	/* USE_TIMESTAMPS */
	gettimeofday(&tp, NULL);
	sprintf(path, "%s/%s/tsdb-%s-%s-%s", request->tree->db_path,
		request->tree->name, request->tree->name, request->env->name, sys);
	if (NULL == (db = fopen(path, "a"))) {
	    emtd_Broadcast(request, EMTD_LOGERR, "Could not open:\n\t%s\n", path);
	    return EMTD_OPEN_FILE_ERR;
	}
	fprintf(db, "%s.-%ld\n", REQ_COLLECTION, tp.tv_sec);
	fflush(db);
	fclose(db);
	sprintf(path, "%s/depot/%s.-%ld", c_sys->path, REQ_COLLECTION, tp.tv_sec);
	IF_RTN(emtd_MakeDir(path, 666));
#endif /* USE_TIMESTAMPS */
	depot_code = emtd_Depot(request, sys, DEPOT_QUICK | DEPOT_VERBOSE |
				DEPOT_DELETE, errstr);
	switch (depot_code) {
	case 0:
	    break;
	default:
	    return code;
	}
	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;
}
