#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "debug.h"
/*This file is used by dprocess as well*/


/* Make these functions local*/
#define ncpy w_ncpy
#define token_split w_token_split

char *ncpy(char *dst, char *src, int len);
int token_split(char *s, char *p[], char *sep, int tmax);

/*
Example rules:
	abc*def*gef -> x%1y%2z
	*@*.netwin -> %1@%2.netwin.co.nz
*/

int wildrep(char *in, char *wild, char *tpl, char *result);

#ifdef unix
char *strlwr(char *s); /*added from lib.c for dprocess*/
char *strupr(char *s); /*added from lib.c for dprocess*/
#endif


#include "defines.h"
#define MAXWORDS 20

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
int wildrep(char *in, char *wild, char *tpl, char *result)
{
	/* Split up wild based on astrix'es */
	char wbuff[BFSZ];
	char lwrin[BFSZ];
	char *words[MAXWORDS];
	int nwords;
	int w;
	char matches[MAXWORDS][1024];
	char *s;
	int nmatches;
	char *out;
	char *upto,*last;
	int len;
	int n, startwild;

	/* Split wild into list of non wild cards to find */

	nmatches=0;

	ncpy(wbuff,wild,BFSZ-1);
	ncpy(lwrin,in,BFSZ-1);
	ncpy(result,in,BFSZ-1);
	strlwr(wbuff);
	strlwr(lwrin);
	startwild = FALSE;
	if (wbuff[0]=='*') startwild = TRUE;
	if (strcmp(wbuff,"*")==0) {
		ncpy(matches[nmatches++],in,BFSZ-1);
		nwords = 0;
		goto done;
	} else {
		nwords = token_split(wbuff,words,"*",MAXWORDS-2);
	}
	if (nwords==0) return FALSE;

	/* Search for each word */
	upto = lwrin;
	last = in;

	s=strstr(upto, words[0]);
	if(s==NULL)
		return FALSE;
	if((s!=upto)&&(!startwild))
		return FALSE;
	for (w=0; w<nwords; w++) {
		s = strstr(upto,words[w]);
		if (s==NULL) return FALSE;
		len = (int)(s-upto); if (len>1000) len=1000;
		if(s!=lwrin)
		{
			ncpy(matches[nmatches],last,len);
			matches[nmatches++][len]=0;
		}
		last = (in+(s-lwrin)+(int)strlen(words[w]));
		upto = s + (int)strlen(words[w]);
	}
	if (wild[(int)strlen(wild)-1]=='*') {
		ncpy(matches[nmatches++],last,999);
	}
done:
	/* Process template */
	out = result;
	for (s=tpl; *s!=0; s++) {
		if ((out-result)>500) break;
		if (*s=='%') {
		  n = s[1]-'0';
		  if ((n>0) && (n<=nmatches)) {
			strcpy(out,matches[n-1]);
			out += (int)strlen(out);
			s++;
			continue;
		  }
		}
		*out++ = *s;
	}
	*out++ = 0;

	return TRUE;
}
char *ncpy(char *dst, char *src, int len)
{
        char *xdst = dst;
        int xlen = len;

        for (;(*src !=0) && (len>0); ) {*dst++  = *src++; len--;}
        *dst++ = 0;
        xdst[xlen] = 0;
        return xdst;
}
int lib_saferep(char *in, char *wild, char *tpl, char *result)
{
	char word1[BFSZ];
	char *s;
	ncpy(word1,wild,BFSZ-1);
	s = strchr(word1,',');
	if (s!=NULL) *s = 0;
	return wildrep(in,word1,tpl,result);
}

int token_split(char *s, char *p[], char *sep, int tmax)
{
        int n=0;

        for (;*s!=0;) {
                s += strspn(s,sep);  /* Skip spaces */
                if (*s==0) break;
                p[n++] = s;
                s += strcspn(s,sep);  /* Find end of word */
                if (*s==0) break;
                *s = 0; s++;
                if ((n+5)>=tmax) {
                        break;
                }
        }
        p[n] = NULL;
        return n;
}
