#include <exec/types.h>
#include <exec/exec.h>
#include <stdio.h>
#include <stdlib.h>
#include "dh1:source/doors/romconf/doorheader.h"
#define sm sendmessage
#define pm prompt
#define hk hotkey
#define gu getuserstring
#define pu putuserstring

#define ACCESS_WRITE -1L
#define ACCESS_READ -2L
char Name[200];
int FindIt(char *str,char *FileName);
void Prepare();
void GiveIt(char *Name,int opt,char *InputName,char *OutPutName);
#define TX(x) (UBYTE)Number.Text[x]
int PurgeIt(char *str,char *FileName,char *NewName);

long lock=NULL;
struct REGIT
{
   char Text[8];
   long chksum;
};
struct REGIT Number;
void end();
extern int TLock(char *str);
void sr(char *str);
void end()
{
  if(lock)
    UnLock(lock);
  exit(0);
}
char t[200];
main(int argc,char *argv[])
{
  
  if(argc!=2)
  {
    printf("AutoRegister 1.2, written by Joseph Hodge\n");
    printf("This is an AmiExpress 2.0+(XIM)\n");
    printf("\n\n");
    exit(0);
  }
  Register(argv[1][0]-'0');
    sm("",1);
    sm("[36mAutoRegister 1.2[35m,[32m written by Joseph Hodge[0m",1);
    sm("",1);
  if(!(lock=Lock("REGISTERED:Lockme",ACCESS_WRITE)))
  {
    sm("",1);
    sm("[32mSorry but an Update is currently being give on another node[0m",1);
    sm("[32mPlease try again in a few minutes[0m",1);
    ShutDown();
    end();
  }
   
  gu(Name,DT_NAME);strupr(Name);
  if(!FindIt(Name,"REGISTERED:Revision.Dat"))
  {
    
    sm("",1);
    sm("[33mYour terminal must have ANSI capability to go any further.",1);
    sm("[32mIs your terminal configured for ANSI [Y/n][0m",0);
    hk(" >:",t);
    if(t[0]=='n' || t[0]=='N')
    {
      sm("[31mNo.[0m",1);
      ShutDown();
      end();
    }
    sm("[32mYes.[0m",1);
    strcpy(t,"VALIDATE");
    pu(t,RETURNCOMMAND);
    Chain("DOORS:FORMEDIT/FORMEDIT",argv[1][0]-'0',WAIT);
    strcpy(t,"");
    pu(t,RETURNCOMMAND);
    if(!FindIt(Name,"REGISTERED:Validate/Validate.db"))
    {
      sm("[32mYou must complete the validation FORM.[0m",1);
      sm("[32mAborting registration.[0m",1);
      ShutDown();
      end();
    }
      
    if(FindIt(Name,"REGISTERED:BetaTester"))
    GiveIt(Name,1,"REGISTERED:AEBeta","REGISTERED:Update.Dat");
    else
    GiveIt(Name,1,"REGISTERED:AE","REGISTERED:Update.Dat");
if(FindIt(Name,"REGISTERED:Developers"))
  {
     sm("[34mRecords indicate you are a Developer for AmiExpress[0m",1);
     GiveIt(Name,2,"REGISTERED:Develope.LZH",NULL);
  }
    ShutDown();
    end();
  }
if(FindIt(Name,"REGISTERED:Developers"))
  {
     sm("[34mRecords indicate you are a Developer for AmiExpress[0m",1);
     GiveIt(Name,2,"REGISTERED:Develope.LZH",NULL);
  }
 
  
  if(FindIt(Name,"REGISTERED:BetaTester"))
      GiveIt(Name,0,"REGISTERED:AEBETA","REGISTERED:Update.dat");
    else
    {
      if(!FindIt(Name,"REGISTERED:Update.Dat") || FindIt(Name,"REGISTERED:Distributors"))GiveIt(Name,0,"REGISTERED:AE","REGISTERED:Update.Dat");
      else
      {

        sm("[33mSorry but records indicate that you already received[0m",1);
        sm("[33mThe latest version of AmiExpress[0m",1);
        ShutDown();
        end();
      }
    }
  ShutDown();
  end();
}

int FindIt(char *str,char *FileName)
{
  FILE *fi;
  char image[200];
  if(!TLock(FileName)) return(0);
  strupr(str);
  fi=fopen(FileName,"r");
  while(fgets(image,80,fi)!=NULL)
  {
     sr(image);
     strupr(image);
     if(!strcmp(image,str))
     {
       fclose(fi);
       return(1);
     }
   }
   fclose(fi);
   return(0);
}
int PurgeIt(char *str,char *FileName,char *NewName)
{
  FILE *fi,*fo1,*fo2;
  char image[200];
  int returncode=0;
  if(!TLock(FileName)) return(0);
  sprintf(image,"%s.tmp",FileName);
  if(!TLock(image))
  {
     Rename(FileName,image);
  }
  strupr(str);
  fi=fopen(image,"r");
  fo1=fopen(FileName,"w");
  fo2=fopen(NewName,"a");
  while(fgets(image,80,fi)!=NULL)
  {
     sr(image);
     strupr(image);
     if(!strcmp(image,str))
     {
       fprintf(fo2,"%s\n",image);
       returncode=1;
       
     }
     else fprintf(fo1,"%s\n",image);
   }
   fclose(fi);
   fclose(fo1);
   fclose(fo2);
  sprintf(image,"%s.tmp",FileName);

   DeleteFile(image);
   return(returncode);
}

void GiveIt(char *Name,int opt,char *InputName,char *OutPutName)
{ 
  char Num[200];
  char Temp[10];
  char Test[80];
  long Position;
  char p[200];
  char TypeCode[200];
  char RegNum[200];
  long TruePosition;
  int Got=0;
  int records=0;
  int i;
  int Distributor=0;
  FILE *fi,*st;
  strcpy(t,InputName);
  
  
  if(opt==2)
  {
    sm("",1);
    if(!TLock(InputName))
    {
      sm("[36mPlease inform the Sysop that the developer package is[0m",1);
      sm("[36mnot available.[0m",1);
      return;
    }
    sm("[33mPreparing Developer Package[0m",1);
    Prepare();
    pu(t,ZMODEMSEND);
    return;
  }
   sm("[33mAmiExpress can be released for specific machine types[0m",1);
   sm("[33m68000, 68030 or 68040.[0m",1);
   if(FindIt(Name,"REGISTERED:Distributors")) Distributor=1;
   if(Distributor)
   {
     sm("[36mRecords indicate that you are a distributor.[0m",1);
   }
  if(!Got && !opt){pm("[32mPlease Enter registration number >:[36m",RegNum,30);
         sprintf(t,"REG:%s-%s",Name,RegNum); strupr(t);}
         if(!Distributor && !opt)
         {
           if(!PurgeIt(t,"Registered:Update.Dat","REGISTERED:Update.old"))
           {
             sm("",1);
             sm("[31mRecords indicate that this is an invalid Registration.",1);
             sm("[31mPlease leave a message with the sysop for further assistance.",1);
             sm("[0m",1);
             ShutDown();
             end();
           }
         }
         else
         if(Distributor && !Got && !opt)
         {
             if(!FindIt(t,"Registered:Update.Dat"))
             {
             sm("",1);
             sm("[31mRecords indicate that this is an invalid Registration.",1);
             sm("[31mPlease leave a message with the sysop for further assistance.",1);
             sm("[0m",1);
             ShutDown();
             end();
             } 
         }
   while(1)
   { 
     while(1)
     {
       
       sm("[34mPlease select machine type:[0m",1);
       sm(" [33m1[36m> 68000[0m",1);
       sm(" [33m2[36m> 68030[0m",1);
       sm(" [33m3[36m> 68040[0m",1);
       if(Distributor)sm(" [33m4[36m> Abort[0m",1);
       hk(" >:",TypeCode);
       TypeCode[1]='\0';
       if(TypeCode[0]=='1'){ sm("[32m68000.[0m",1);sprintf(t,"%s000",InputName); break;}
       if(TypeCode[0]=='2'){ sm("[32m68030.[0m",1);sprintf(t,"%s030",InputName); break;}
       if(TypeCode[0]=='3'){ sm("[32m68040.[0m",1);sprintf(t,"%s040",InputName); break;}
       if(TypeCode[0]=='4' && Distributor) { sm("",1); break; }
       
     }
     if(TypeCode[0]=='4') break;
     if(TypeCode[0]=='1' || TypeCode[0]=='2' || TypeCode[0]=='3')
       {
         if(!records)
         {
           if(opt==1)
           {
            sm("[32mPreparing Revision.[0m",1);
            fi=fopen("REGISTERED:Revision.Dat","a");
            fprintf(fi,"%s\n",Name);
            fclose(fi);
           }
           records=1;
         }
       }
     strcat(t,".LZH");
     sm("",1);
     sm("[34mPreparing main program, please standby.[0m",1);
    if(!TLock(t))
    {
      sm("",1);
      sm("[31mError! Can't locate Files, please notify sysop![0m",1);
      sm("",1);
      ShutDown();
      end();
    }
         
        if((!Got && Distributor && opt) || !Distributor)
        {
      
           Number.chksum=time(NULL);
           st=fopen(OutPutName,"a");
           fprintf(st,"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
           fprintf(st,"%s\n",Name);
           fprintf(st,"CheckSum %ld ",Number.chksum);
           fprintf(st,"SumKickDate %s",ctime(&Number.chksum));
           fprintf(st,"REG:%s-%u%u%u%u%u%u%u%u%u-%u.%u%u\n",Name,TX(11),TX(1),TX(9),TX(3),TX(4),TX(8),TX(6),TX(7),TX(5),TX(2),TX(10),TX(0));
           fclose(st); 
           sm("",1);
           sm("[32mPlease standby, serializing package..[0m",1); 
           sprintf(p,"%u%u%u%u%u%u%u%u%u-%u.%u%u",TX(11),TX(1),TX(9),TX(3),TX(4),TX(8),TX(6),TX(7),TX(5),TX(2),TX(10),TX(0));
           sm("[33mYour Registration number is >:[31m",0);
           sm(p,0);sm("",1);
   sm("[32m****************************************************************",1);
   sm("[33mPlease record your Registration number, It will be needed to get",1);
   sm("[33mproduct assistance and future updates.",1);
   sm("[32m****************************************************************",1);
   sm("[0m",1);
               
         } 
         if(Distributor) Got=1;   
          Prepare();
          
           pu(t,ZMODEMSEND);
           sm("",1); if(!Distributor) break;
      
     }
     
   sm("",1);
  /* if((records && !Distributor) || (Distributor && opt && Got))
   {
  
   }*/
}
 
void Prepare()
{
  char temp[10];
  sm("",1);
  sm("[33mPlease prepare for Zmodem Download.[0m",1);
  sm("[36mpress anykey to continue[0m",0);
  hk("",temp);
  sm("",1);
}

void sr(char *str)
{
  register int i;
  i=strlen(str)-1;
  while(i>-1)
  {
    if(*(str+i)<=32) *(str+i)='\0'; else break;
    i--;
  }
}
