/*  This program serves as a stand-alone module.
 *
 *  It's function is to display the copyright.
 *  It also verifys that we can read/write on the network.
 *    o Error: Couldn't do a directory search.
 *    o ERROR: Couldn't Create/Open Big-Shell Test file.
 *    o ERROR: Couldn't Write to Big-Shell test file.
 *    o Error: Couldn't Delete Big-Shell test file.
 *    o Error: Can't Open BSHELL.OV2.
 *    o Error: Can't Read BSHELL.OV2.
 *    o Error: Invalid SHELL_PROG= in DOS Environment.
 */

// Copyright 1992, Alfred J. Heyman and Spectrum Research, Inc.


#pragma check_stack(off)

#include 
#include 
#include 
#include 
#include 
#include 
//#include 
#include 
#include 
#include 
#include 
#include 

char zero = 0;

char thisprog[80];
char adi_bat[80];

char drive[5];                      // Used for drive storage by SPLIT_PATH.
char dir[80];
char fname[20];
char ext[10];

int  exitcode = 240;
char lockname[80] = {"BSHELL.OV2"};

struct find_t fbuffer;

struct mb
{ char block;
  int  psp;
  int  size;
  char space[11];
}*mem_block;

struct
{ char jump[3];
  int  crc_code;
  int  max_user;
  char serial[10];
  char name[50];
  char company[50];
  char address[50];
  char city[50];
  char state[50];
  char country[50];
  char zip[50];
  char msg1[50];
  char msg2[50];
  char msg3[50];
  char msg4[50];
  char msg5[50];
  char locks;
} lockfile;

/*----------------------------------------------------------------*/
void copyright()
{  int delayx;

   clearwindow(0,0,79,24,7);
   gotoxy(0,0);
   clearwindow(0,0,28,11,0x70);
   clearwindow(2,2,26,9,7);
   clearwindow(1,1,24,8,0x1f);
   setcolor(0x1f);
   gotoxy(2,2);  sprintstr("BIG-SHELL 2.10");
   gotoxy(2,3);  sprintstr("Copyright (c) 1990-92");
   gotoxy(2,4);  sprintstr("Alfred J. Heyman");
   gotoxy(2,5);  sprintstr("All rights reserved");
   setcolor(0xf0);
   gotoxy(2,10); sprintstr("Program LOADING");
   gotoxy(2,11); sprintstr("PLEASE WAIT    ");
   gotoxy(0,14);
   setcolor(7);

   gotoxy(0,13); sprintstr("This  software is licensed  for use only on a SINGLE computer and only");
   gotoxy(0,14); sprintstr("in accordance with the terms of the software license agreement. Title");
   gotoxy(0,15); sprintstr("to this software remains with Alfred Heyman at all times. This software");
   gotoxy(0,16); sprintstr("serialized and may only be used by the registered user.  It may not be");
   gotoxy(0,17); sprintstr("transferred to anyone without the permission of Alfred Heyman.  Unauthorized.");
   gotoxy(0,18); sprintstr("reproduction, transfer, or use of this material may be a civil and criminal");
   gotoxy(0,19); sprintstr("offense under Federal Copyright Law.");
   gotoxy(0,20);

   if(run_reg()!=1)
    {  clearwindow(0,13,79,24,7);
       gotoxy(0,14);
       sprintstr("Error: BSREG.COM not found, or Not Personalized. Reinstall.");
       getch();
       exit(-1);
    };
   for(delayx = 0 ; delayx < 2 ; delayx++)  { delay(); };
   clearwindow(0,13,79,20,7);
};

/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
run_reg()
{  char *NULLX;
   char runregpath[80];
   int  retcode;
   union REGS inregs,outregs;

   NULLX=NULL;
   strcpy(runregpath,thisprog);
   _splitpath(runregpath,drive,dir,fname,ext);
   strcpy(fname,"BSREG");
   strcpy(ext,".COM");
   _makepath(runregpath,drive,dir,fname,ext);
   retcode = _spawnle(P_WAIT,runregpath,NULL, (char*) &NULLX);
   retcode=1;
   return(retcode);
};

/*--------------------------------------------------------------------*/
adi_load()
{  strcpy(adi_bat,thisprog);
   _splitpath(adi_bat,drive,dir,fname,ext);
   strcpy(fname,"TSR-LOAD");
   strcpy(ext,"BAT");
   _makepath(adi_bat,drive,dir,fname,ext);
   if(_dos_findfirst(adi_bat,_A_NORMAL,&fbuffer)==0)
     { clearwindow(0,0,79,24,7);
       clearwindow(0,0,79,0,0x70);
       setcolor(0x70);
       gotoxy(0,0); sprintstr(" Running TSR-LOAD.BAT for ADI/TSR Loadup.");
       setcolor(7);
       gotoxy(0,3);
       system("TSR-LOAD.BAT");
       copyright();
       return(1);
     }
    else
     { return(0); };
};

/*--------------------------------------------------------------------*/
void net_test()
{ char testfile1[80];
  char testfile2[80];
  int  result, thandle;
  struct find_t  fbuf;

  strcpy(testfile1,thisprog);
  strcpy(testfile2,thisprog);
  _splitpath(testfile1,drive,dir,fname,ext);
  strcpy(fname,"BSHELL");
  strcpy(ext,"TST");
  _makepath(testfile1,drive,dir,fname,ext);
  _makepath(testfile2,drive,dir,"*","*");

  result = _dos_findfirst(testfile2,_A_NORMAL | _A_HIDDEN,&fbuf);
  if(result != 0)
    { bsabc();
      sprintstr("do a directory search.");
      bsabort();
    };

  unlink(testfile1);     // Erase it if it exists.

  thandle = result = _open(testfile1,O_RDWR | O_BINARY | O_CREAT, S_IWRITE | S_IREAD);
  if(result == -1)
    { bsabc();
      sprintstr("create/open Big-Shell test file.");
      bsabort();
    };

  result = write(thandle,testfile1,10);

  if(result != 10)
    { bsabc();
      sprintstr("write to Big-Shell test file.");
      bsabort();
    };

  close(thandle);

  result = unlink(testfile1);
  if(result != 0)
    { bsabc();
      sprintstr("delete Big-Shell test file.");
      bsabort();
    };
};

/*--------------------------------------------------------------------*/
bsabc()
{ clearwindow(0,13,79,24,7);
  gotoxy(0,14);  sprintstr("ERROR:");
  gotoxy(0,16);  sprintstr("Couldn't ");
};

bsabort()
{ gotoxy(0,18);  sprintstr("Big-Shell can not execute.");
  gotoxy(0,19);  sprintstr("Network security too tight?!");
  gotoxy(0,20);  sprintstr("Press any key to abort run.");
  exit(-1);
};

/*--------------------------------------------------------------------*/
void run_lock()
{ int handle;
  int lresult;

  //return(0);    // VERSION 2.00 DOES NOT USE NETWORK LOCKING.

  strcpy(lockname,thisprog);
  _splitpath(lockname,drive,dir,fname,ext);
  strcpy(fname,"BSHELL");
  strcpy(ext,"OV2");
  _makepath(lockname,drive,dir,fname,ext);

  lresult = _dos_open(lockname,O_RDWR | SH_DENYNO,&handle);
  if(lresult != 0)
   { clearwindow(0,0,79,24,7);
     gotoxy(0,0);
     sprintstr("ERROR: Can't Open BSHELL.OV2");
     getch();
     exit(-1);
   };

  lresult = _read(handle,&lockfile.jump,256);
  if(lresult == 0)
   { clearwindow(0,0,79,24,7);
     gotoxy(0,0);
     sprintstr("ERROR: Can't Read BSHELL.OV2");
     getch();
     exit(-1);
   };
  _close(handle);
};

/*--------------------------------------------------------------------*/
int test_shellprog()
{   char *sp;

    sp=getenv("SHELL_PROG");
    if(sp==NULL)
      { return(0); }
     else
      {
        _splitpath(sp,drive,dir,fname,ext);
        strupr(ext);
        strupr(fname);
        if(strcmp(ext,".COM")==0) { return(0); };
        if(strcmp(ext,".EXE")==0) { return(0); };
        clearwindow(0,0,79,24,7);
        gotoxy(0,0);
        sprintstr("ERROR: Invalid SHELL_PROG= in DOS Environment.");
        getch();
        exit(-1);
      };
return(0);
};

/*--------------------------------------------------------------------*/
int test_shellswap()
{   char *sp;
    char swapp[80];

    sp=getenv("SHELL_SWAP");
    if(sp==NULL) { return(0); }
     else
      { strcpy(swapp,sp);
//      if(swapp[strlen(swapp)-1]!='\\')
//  BARF
//        _splitpath(sp,drive,dir,fname,ext);
//        strupr(ext);
//        strupr(fname);
//        if(strcmp(ext,".COM")==0) { return(0); };
//        if(strcmp(ext,".EXE")==0) { return(0); };
//        clearwindow(0,0,79,24,7);
//        gotoxy(0,0);
//        sprintstr("ERROR: Invalid SHELL_PROG= in DOS Environment.");
//        getch();
//        exit(-1);
      };
};

/*--------------------------------------------------------------------*/
void main(argc, argv, envp)
int argc;
char **argv;
char **envp;
{ char **p;
  int  adiret;
  int  tspret;

  argc--;       /* Decriment the number of command lines to look through. */
  p = argv;     /* Point to Executed file name.                           */
  strcpy(thisprog,*p);

  copyright();             // Print copyright
  run_lock();              // Load lock file, display user name
  tspret=test_shellprog(); // Test validity of SHELL_PROG environment var.
  net_test();              // Test Create, Read, Write, Delete on network
  adiret = adi_load();     // Load ADI drivers
  exit(240);
  exit(exitcode + tspret + adiret);
};