/*@P************************************************************************
 * File           : TSTRESET.C
 * Project        : EVERLOCK EVMOVDX.DLL BCDOS16 Reset Test Application
 * Compiler       : Borland C++ v4.52
 * Description    :
 *
 * Copyright (c) 1994-97 Az-Tech Software, Inc.
 * ALL RIGHTS RESERVED
 ************************************************************************P@*/

#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <dos.h>
#include "sb.h"

/***************************************************************************
 * Function Prototypes.
 ***************************************************************************/
int __far __pascal DoEvmov3( SB __far *lpUserSB );
int NormalizeStatus( int status );

/***************************************************************************
 * Global Variables.
 ***************************************************************************/
SB sb;

//@PB
int main( int argc, char **argv )
{
   int comp_status;

   // announce our presence to the user
   printf( "\nEVERLOCK EVMOVDX.DLL Reset Test Application\n\n" );

   // zero out the contents of the SB structure
   memset( &sb, 0, sizeof( SB ) );

   // initialize the contents of the SB buffer
   strncpy( sb.id, "AZSB", 4 );

   // parse out the command line parameters
   if ( 1 == argc )

      // no parameter so use exe directory for disk to reset
      strncpy( sb.source, argv[0], 2 );

   else

      // copy the first two characters of the parameter (drive & colon)
      strncpy( sb.source, argv[1], 2 );

   // get the reset use-count and product serial numbers
   sb.func = 0;
   comp_status = DoEvmov3( &sb );

   // verify the success of the information gathering
   if ( comp_status )
   {
      // display the error message
      printf( "EVMOVDX.DLL Error %04X!\n", comp_status );

      // return to the OS
      return NormalizeStatus( comp_status );

   } else if ( sb.stat1 ) {

      // display the error message
      printf( "Security Error %04X-%04X-%04X!\n", sb.stat1, sb.stat2,
              sb.stat3 );

      // return to the OS
      return NormalizeStatus( sb.stat1 );
   }

   // display the reset information for the user
   printf( "Serial Number: %lu-%u\n", sb.snp, sb.ruc );
   printf( "         Date: %s\n", sb.cdate );

   // get the reset code
   printf( "   Reset Code: " );
   gets( sb.code );
   printf( "\n" );

   // make sure the user entered a code
   if ( strlen( sb.code ) < 15 )
   {
      printf( "Invalid reset code entered.  Ending TSTREST.\n" );
      return 0;
   }

   // perform the actual reset operation
   sb.func = 2;
   comp_status = DoEvmov3( &sb );

   // verify the reset operation
   if ( comp_status )
   {
      // display the error message
      printf( "EVMOVDX.DLL Error %04X!\n", comp_status );

      // return to the OS
      return NormalizeStatus( comp_status );

   } else if ( sb.stat1 ) {

      // display the error message
      printf( "Security Error %04X-%04X-%04X!\n", sb.stat1, sb.stat2,
              sb.stat3 );

      // return to the OS
      return NormalizeStatus( sb.stat1 );
   }

   // let the user know we were successfull
   printf( "Reset Successfull!\n" );

   // return to the OS
   return 0;
}

//@PB
int NormalizeStatus( int status )
{
   int temp;

   // get the hundreds digit from the error code
   temp = ( ( status & 0x0F00 ) >> 8 ) * 100;

   // add in the tens digit from the error code
   temp += ( ( status & 0x00F0 ) >> 4 ) * 10;

   // finally, add in the ones digit
   temp += status & 0x000F;

   // return the normalized status value to our caller
   return temp;
}
