/*
 *  This file is part of DVS, a simple distributed versioning system
 *  See http://www.cs.colorado.edu/~carzanig/dvs/
 *
 *  Author: Antonio Carzaniga <carzanig@cs.colorado.edu>
 *
 *  Copyright (C) 1998-2002 University of Colorado
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
 *  USA, or send email to carzanig@cs.colorado.edu.
 *
 *
 *  $Id: ws_list.c,v 1.5 2002/05/01 17:39:10 carzanig Exp $
 */
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <nc.h>

#include "ws_list.h"

void ws_list_free(dvs_ws_list l)
{
  dvs_ws_list aux;
  while (l != NULL) {
    aux = l;
    l = l->next;
    free(aux);
  }
};

void ws_list_print_element(dvs_ws_list l, int verbose)
{
  if (!l) return;
  if (verbose) {
    if (l->workspace_version[0] == '-') {
      if (l->collection_version[0] == '-') {
	printf("* ");
      }
      else printf("- ");
    }
    else {
      if (l->collection_version[0] == '-') {
	printf("+ ");
      }
      else {
	if (strcmp(l->collection_version, l->workspace_version)!=0) {
	  printf("! ");
	}
	else {
	  printf("  ");
	}
      }
    }
    printf("%-30s %s  (%s)\n", l->name, 
	   l->workspace_version, l->collection_version);
  }
  else {
    printf("%s\n", l->name);
  }
};

void ws_list_print(dvs_ws_list l, int verbose, const char *format)
{
  const char *curs; 
  while (l != NULL) {
    int do_print = 0;
    if (format == NULL) {
      do_print = 1;
    }
    else 
      for(curs = format; *curs != '\0'; curs++) 
	switch (*curs) {
	case 'c':
	  if (l->collection_version[0] != '-')
	    do_print = 1;
	  break;
	case 'w':
	  if (l->workspace_version[0] != '-')
	    do_print = 1;
	  break;
	case 'b':
	  if (l->collection_version[0] != '-'  &&
	      l->workspace_version[0] != '-')
	    do_print = 1;
	  break;
	case 'o':
	  if (l->collection_version[0] == '-'  &&
	      l->workspace_version[0] == '-')
	    do_print = 1;
	  break;
	}
    if (do_print)
      ws_list_print_element(l, verbose);
    l = l->next;
  }
};


dvs_ws_list ws_list_add_v(dvs_ws_list *l, const char *name) 
{
  dvs_ws_list new_element;
  int cmpres;

  while (*l != NULL) {
    cmpres=strcmp((*l)->name, name);
    if (cmpres == 0) { 
      return *l;
    }
    else if (cmpres > 0) {
      break;
    }
    else
      l = &((*l)->next);
  }
  new_element = malloc(sizeof(struct dvs_ws_list_s));
  strcpy(new_element->name, name);
  strcpy(new_element->workspace_version, "-");
  strcpy(new_element->collection_version, "-");
  new_element->next = *l;
  *l = new_element;
  
  return new_element;
};

void ws_list_add_c(dvs_ws_list *l, const char *name, const char *version)
{
  dvs_ws_list new_element;
  
  new_element = ws_list_add_v(l, name);
  strcpy(new_element->collection_version, version);
}

void ws_list_add_d(dvs_ws_list *l, const char *name, const char *version)
{
  dvs_ws_list new_element;
  
  new_element = ws_list_add_v(l, name);
  strcpy(new_element->workspace_version, version);
}
