#include <exec/exec.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <clib/exec_protos.h>

void sr(char *s);
void GetLowestKey(void);
struct MailHeader {
	char	Status[1];
	long	MsgNumb;
	char	ToName[31],
		FromName[31],
		Subject[31];
	long	MsgDate,
		Recv;
	char Pad;
	};
struct MailStat {
	long	LowestKey,
		HighMsgNum,
		LowestNotDel;
	char Pad[6];
	};

struct MailHeader MH;
struct MailStat MS;
char oldheadername[200],newheadername[200];
char conflocation[200];
char temp[100];
ULONG lowmsg;
main(int argc,char *argv[])
{
  FILE *fi,*fo;
  ULONG msgnumb=0L;
  ULONG goodtogo=0L;
  printf("\n");
  printf(" .-------------------------------------------------------.\n");
  printf(" | Ami-Express RngMsg Version 2.34 Written by ByteMaster |\n");
  printf(" |      /X Development Team - The Silent Achievers       |\n");
  printf(" `-------------------------------------------------------'\n");
  printf("\n");

  if(argc<2)
  {
    printf("RngMsg version 2.34,  written by Joseph Hodge\n");
    printf("usage: RngMsg <conf location> <max msgs>\n");
    printf("   ie: RngMsg bbs:confs/ 100\n");
    printf("\n");
    exit(0);
  }
  strcpy(conflocation,argv[1]);
  if(argc==3) lowmsg=atol(argv[2]); else lowmsg=100L;
  sr(conflocation);
 GetLowestKey();

  sprintf(oldheadername,"%sMsgBase/HeaderFile",conflocation);
  strcpy(newheadername,oldheadername);
  strcat(oldheadername,".old");
  DeleteFile(oldheadername);
 
  if(Rename(newheadername,oldheadername))
  {
     fi=fopen(oldheadername,"rb");
     fo=fopen(newheadername,"wb");
     while(fread((APTR)&MH,sizeof(struct MailHeader),1,fi)!=NULL)
     {
       msgnumb++;
       MH.MsgNumb=msgnumb-1L+MS.LowestKey;
       if(MH.Status[0]!='D' && msgnumb-1L+MS.LowestKey<lowmsg)
       {
          sprintf(temp,"%sMsgBase/%ld",conflocation,msgnumb-1L+MS.LowestKey);
          DeleteFile(temp);
          MH.Status[0]='D';
          printf("Deleting %s\n",temp);
       }
      sprintf(temp,"%sMsgBase/%ld",conflocation,msgnumb-1L+MS.LowestKey);
       if(access(temp,00))
       {
          if(MH.Status[0]!='D')
          {
            MH.Status[0]='D';
            printf("Can't Locate Msg %ld, Flagging Header for deletion\n",msgnumb-1L+MS.LowestKey);
          }
       }
       if(MH.Status[0]!='D' && goodtogo==0L)
       {  goodtogo=msgnumb-1L+MS.LowestKey;
          printf("Lowest Message Number is %ld\n",goodtogo);
       }
       if(goodtogo)
       fwrite((APTR)&MH,sizeof(struct MailHeader),1,fo);
     }
     fclose(fi);
     fclose(fo);
     msgnumb++;
     sprintf(oldheadername,"%sMsgBase/mailstats",conflocation);
     fi=fopen(oldheadername,"rb");
     if(fi!=NULL)
     {
       printf("Updating MailStats\n");
     fread((APTR)&MS,sizeof(struct MailStat),1,fi);
       printf("Old HighMsg = %ld\n",MS.HighMsgNum);
       printf("Old LowKey  = %ld\n",MS.LowestKey);
     fclose(fi);
     MS.HighMsgNum=msgnumb-1L+MS.LowestKey;
     if(goodtogo!=0L) MS.LowestKey=goodtogo;
     if(MS.LowestNotDel<MS.LowestKey) MS.LowestNotDel=MS.LowestKey;
     fi=fopen(oldheadername,"wb");
     printf("New HighMsg = %ld\n",MS.HighMsgNum);
     printf("New LowKey  = %ld\n",MS.LowestKey);
     fwrite((APTR)&MS,sizeof(struct MailStat),1,fi);
     fclose(fi);
     }

  }
  else
  {
    printf("Error, cannot locate msgbase\n");
  }
  exit(0);
}

void GetLowestKey(void)
{
  FILE *fi;
  char image[200];
  
  sprintf(image,"%sMsgBase/MailStats",conflocation);
  fi=fopen(image,"rb");
  if(fi!=NULL)
  {
    fread((APTR)&MS,sizeof(struct MailStat),1,fi);
    fclose(fi);
    if((LONG)MS.HighMsgNum-MS.LowestKey >lowmsg) lowmsg=MS.HighMsgNum-lowmsg;
    else { printf("MsgBase Size meets specification\n"); exit(0); }
    return;
  }
  else { printf("Error, MailStats file inaccessible\n"); exit(0); }
}
void sr(char *s)
{
  register int i;
  i=strlen(s)-1;
  while(i>-1)
  {
    if(*(s+i)<=32) *(s+i)='\0'; else break;
    i--;
  }
}