/* Copyright (C) 1995 NetWin World, New Zealand, All rights reserved. */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#define ISCONF TRUE
#include "emsg.h"
#include "lib.h"
#include "dnews.h"
#include "sary.h"
#include "nntp.h"
#include "conf.h"

char *get_dconfig(void);

#define MAX_CONF 250
static char *conf_value[MAX_CONF];
static int conf_true[MAX_CONF];
static char dnews_conf[200];

static Sary *config;
int config_true(int x)
{
        if (config==NULL) config_load();
        return conf_true[x];
}

int conf_item_max(void)
{
        static int max;
        if (max==0) max = atoi(config_get(C_ITEM_MAX));
        if (max==0) max = 1000000;
        return max;
}
void config_set(int x, char *s)
{
	char bf[100];
 	conf_value[x] = strdup(s);
	ncpy(bf,s,99); strlwr(bf);
	conf_true[x] = FALSE;
	if (strstr(bf,"true")!=NULL) conf_true[x] = TRUE;
}
char *config_get(int x)
{
        if (config==NULL) config_load();
        
        if (conf_value[x]==NULL) {
                return "";
        } else {
                return conf_value[x];
        }
}
void config_load(void)
{
        int i,j;
        for (i=0; i<MAX_CONF; i++) conf_true[i] = FALSE;
}
#ifndef NETWARE
#define dirfix(x) (x)
#endif

char *config_getu(int x)
{
#ifdef unix
        int len;
        static char bf[300];
        ncpy(bf,config_get(x),299);
        len = strlen(bf);
/*RHP mac uses : not slash*/
#ifdef Mac
        if (len>0) if (bf[len-1]!=':') strcat(bf,":");
#else
        if (len>0) if (bf[len-1]!='/') strcat(bf,"/");
#endif
        return dirfix(bf);
#else
        return config_get(x);
#endif  
}
char *config_getu(int x);
char *file_config(char *s)
{
        static char bf[300];
        sprintf(bf,"%s%s",config_getu(C_CONFIG),s);
        return dirfix(bf);
}
char *file_hist(char *s)
{
        static char bf[300];
        sprintf(bf,"%s%s",config_getu(C_HISTORY),s);
        return bf;
}
char *file_work(char *s)
{
        static char bf[300];
        sprintf(bf,"%s%s",config_getu(C_WORKAREA),s);
        return bf;
}
char *file_log(char *s)
{
        static char bf[300];
        sprintf(bf,"%s%s",config_getu(C_LOGDIR),s);
        return bf;
}
char *conf_getpass(void)
{
	return "ppp";
}
int conf_isat(char *at)
{       
        time_t ttime;
        struct tm *t;
	struct tm atm;
        char mins[100],hours[100];
        ncpy(mins,get_word(at,0),99);
        ncpy(hours,get_word(at,1),99);
        ttime = time(NULL);
        t = safe_localtime(&ttime,&atm);
        if (time_match(t->tm_min,mins) && time_match(t->tm_hour,hours)) 
                return TRUE;
        return FALSE;   
}
int conf_isat_hour(char *at)
{       
        time_t ttime;
        struct tm *t;
	struct tm atm;
        char mins[100],hours[100];
        strcpy(mins,"*");
        ncpy(hours,get_word(at,1),99);
        ttime = time(NULL);
        t = safe_localtime(&ttime,&atm);
        if (time_match(t->tm_min,mins) && time_match(t->tm_hour,hours)) 
                return TRUE;
        return FALSE;   
}
int time_match(int t, char *at)
{
        int a,b;
        char *s,*w;
        char tmp[200],tstr[200];

        sprintf(tstr,"%d",t);
        ncpy(tmp,at,100);
        w = strtok(tmp,",");
        for (;w!=NULL;) {
                s = strchr(w,'-');
                if (s!=NULL) {
                        a = atoi(w);
                        b = atoi(s+1);
                        if (a<b) {
                                if (t>=a && t<=b) return TRUE;
                        } else {
                                if (t>=a || t<=b) return TRUE;
                        }
                } else {
                        if (match_wild(w,tstr)) return TRUE;
                }       
                w = strtok(NULL,",");
        }
        return FALSE;
}
#ifdef WIN32
#include <windows.h>
char *get_dconfig(void)
{
        static char sysdir[MAX_PATH];
        static char binpath[MAX_PATH];
        GetSystemDirectory(sysdir,MAX_PATH);
        sprintf(binpath,"%s\\dnews.conf",sysdir);
        return binpath;
}
#else


#ifdef Mac
char *get_dconfig(void)
{
        if (strlen(dnews_conf)==0) sprintf(dnews_conf,":dnews_top:%s.conf",dnews_name);
        return dnews_conf;
}
#else
char *get_dconfig(void)
{
        if (strlen(dnews_conf)==0) sprintf(dnews_conf,"/etc/%s.conf",dnews_name);
        return dnews_conf;
}
#endif
#endif

