#include "bbs.h"
#include <dos/dos.h>
#include <dos/dosextens.h>
ULONG ConfBaseLine;
extern int NotRelative;
#define USERDATA 0
#define CONFTOTAL 1
#define CONF      2
#define Tell(x)  Seek(x,0L,OFFSET_CURRENT)
extern struct DiskObject *ConfObj;
extern UBYTE NewSinceFlag;
void GetConfName(char *n,char *l,int Conf)
{
  if(Conf>=0 && Conf<=8)
  {
    strcpy(n,Cmds->ConfName[Conf]);
    strcpy(l,Cmds->ConfLoc[Conf]);
    sprintf(MsgBase_Location,"%sMsgBase/",l);
    sprintf(Upload_Location,"%sUpload/",l);
  }
 
}

int IsTempConf(struct User *p,int Conf)
{
   if(p->Conference_Access[Conf]=='X') return(1); else return(0);
}
int IsValidConf(int Conf)
{
   if(GetAreaBit(Conf)) return(1); else return(0);
}

void SaveMsgPointers(void)
{
   ULONG *loc;
   if(CN-1<0 || CN-1>8) return;
   loc=&User.ConfRead1;
   *(loc+(CN-1))=Last_New_Read_Conf;
   loc=&User.ConfYM1;
   *(loc+(CN-1))=Last_Msg_Read_Conf;
   
}
void LoadMsgPointers(void)
{
   ULONG *loc;
  if(CN-1<0 || CN-1>8) return;
   loc=&User.ConfRead1;
   Last_New_Read_Conf=*(loc+(CN-1));
   loc=&User.ConfYM1;
   Last_Msg_Read_Conf=*(loc+(CN-1));
}
int RelConf(int CN)
{
  register int i=0;
  register int count=0;
    while(i<CN)
    {
       if(GetAreaBit(i) || NotRelative) count++;
       i++;
    }
  return(count);
}
int GetInverse(int CN)
{
  register int i=0;
  register int j=0;
  if(CN -1< 0) return(0);
  if(NotRelative) return(CN);
    while(i<CN)
    {
      if(j<Boards_Are_Active)
      {
          if(GetAreaBit(j)) i++;
      } else { return(0); }
      j++;
    }
  return(j);

}

void Save_ConfDB(int account,int conf,APTR address)
{
   BPTR bi;
   struct ConfBase *t;
   char temp[100];
   char temp1[100];
   t=address;
   GetConfName(temp,temp1,conf-1);
   sprintf(temp,"%sConf.DB",temp1);
   bi=Open(temp,MODE_OLDFILE);
   if(bi==NULL)
   {
   CallersLog("\tError can't open >:"); CallersLog(temp); CallersLog("\n");
  
     return;
   }

   if(Seek(bi,(ULONG)(account-1)*(ULONG)(sizeof(struct ConfBase)),OFFSET_BEGINNING)==-1)
   {
      CallersLog("\tError Saving MsgBase Pointer\n"); Close(bi); return;
   }
   Write(bi,(APTR)t,sizeof(struct ConfBase));
   Close(bi);
   GetConfName(temp,temp1,conf-1);
}
void Load_ConfDB(int account,int conf,APTR address)
{
   BPTR bi;
   struct ConfBase *t;
   char temp[100];
   char temp1[100];
   t=address;
   GetConfName(temp,temp1,conf-1);
   sprintf(temp,"%sConf.DB",temp1);
   bi=Open(temp,MODE_OLDFILE);
   if(bi==NULL)
   {
   CallersLog("\tError can't open >:"); CallersLog(temp); CallersLog("\n");
  
     return;
   }

   if(Seek(bi,(ULONG)(account-1)*(ULONG)(sizeof(struct ConfBase)),OFFSET_BEGINNING)==-1)
   {
      CallersLog("\tError Saving MsgBase Pointer\n"); Close(bi); return;
   }
   FRead(bi,(APTR)t,sizeof(struct ConfBase),1L);
   Close(bi);
   GetConfName(temp,temp1,CN-1);
}
