/* (C) Tim Graves 20th April 1994
   This code is supplied AS IS. no warrantee either expressed or implied 
   is provided. This code may be freeley modified and modified as long as my
   origional authorship is acknowledged. 
   
   Tim Graves
    Sun Microsystems
     
     */
/* this file contains the excludelist for the backup subsystem, it is used to 
remove files from the backup */
#include <stdio.h>
#include "psion.h"
#include "psbackupexclude.h"
#include <ctype.h>
#include <string.h>
extern int debugcall ;
static char vsn[]="@(#) psfilt.c 2.3@(#)" ;
/* the structures to hold the exclude entries */
struct excludestruct * exclude;
initexclude()
{
	if (debugcall >= EXCLUDECALLDEBUG)
		printf("CALL: initexclude()\n") ;
	exclude == NULL ;
}

setupexclude (path)
char * path ;
{
	struct excludestruct * current;
	if (debugcall >= EXCLUDECALLDEBUG)
		printf("CALL: setupexclude (path = %s)\n",path) ;
	current = exclude ;
	/* first of all does the exclude already ? if so ignore it */
	while (current != NULL)
	{
		if (strcmp(current->path, path) == 0)
		{
			return  ;
		}
		current = current->nextexclude ;
	}
	/* we've got here and not found a exclude, install at the end 
	   of the list */
	return (newexclude(path)) ;
}
removeexclude (path)
char * path ;
{
	struct excludestruct * current, *prev;
	if (debugcall >= EXCLUDECALLDEBUG)
		printf("CALL: removeexclude (path = %s)\n",path) ;
	if (exclude == NULL) 
		return(OK) ;
	current = exclude ;
	/* check if the exclude is the first in the list */
	if (strcmp(exclude->path, path) == 0)
	{
		/* make sure we have a start pointer */
		exclude = exclude->nextexclude ;
		/* free the data */
		free(current) ;
		return(OK) ;
	}
	prev = exclude ;
	while (current != NULL)
	{
		if (strcmp(current->path, path) == 0)
		{
			/* remove from the list */
			prev->nextexclude = current->nextexclude ;
			/* free the data */
			free (current);
			return (OK);
		}
		/* next item on the list */
		prev = current ;
		current = current->nextexclude ;
	}
}
	
/* install a exclude into list */
installexclude (path, current)
struct excludestruct *current ;
char * path ;
{
	if (debugcall >= EXCLUDECALLDEBUG)
		printf("CALL: installexclude (path = %s, current = (OMITED))\n",path) ;
	/* lets do the setup */
	strcpy(current->path, path) ;
	return (OK) ;
}

/* newexclude, create a exclude add it to the list then fill it */
newexclude(path)
char * path ;
{
	struct excludestruct *new ;
	if (debugcall >= EXCLUDECALLDEBUG)
		printf("CALL: newexclude (path = %s, )\n",path) ;
	new = (struct excludestruct *) calloc (1, sizeof (struct excludestruct)) ;
	/* add it to the head of the list */
	new->nextexclude = exclude ;
	exclude = new ;
	/* create the exclude */
	return (installexclude(path, exclude));
}
/* print each item in the exclude list */
printexclude()
{
	struct excludestruct *current ;
	if (debugcall >= EXCLUDECALLDEBUG)
		printf("CALL: printexclude ()\n") ;
	/* initialise to the head of the list */
	current = exclude ;
	if (exclude == NULL) 
	{
		printf("No backup excludes currently installed\n") ;
		return(OK) ;
	}
	printf("Current installed backup excludes\n") ;
	/* run through the list */
	while (current != NULL)
	{
		printf("%s\n", current->path)  ;
		/* next item on the list */
		current = current->nextexclude ;
	}
}


/* if the exclude is set return TRUE, otherwise return FALSE */
findexclude(path)
char * path ;
{
	struct excludestruct * current;
	if (debugcall >= EXCLUDECALLDEBUG)
		printf("CALL: findexclude (path = %s)\n",path) ;
	current = exclude ;
	/* lets doit */
	while (current != NULL)
	{
		if  (strcmp(current->path, path) == 0)
		{
			return (TRUE) ;
		}
		current = current->nextexclude ;
	}
	/* we've got here and not found a exclude return FALSE*/
	return (FALSE) ;
}
