#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include <fcntl.h>
#include <clib/exec_protos.h>
#include <dialog/dlg.h>
#include <dialog/broadcast.h>
#include <proto/dlg.h>
#include <dialog/user.h>
#include <exec/types.h>
#include <proto/exec.h>


#define VERSION "0.05"


void About(void);

void About(void)
{
    printf("\nDLGprotect [Version %s] [John Paul Lonie]\n\n", VERSION);
    printf("   Command Line: DLGprotect <password> <command string>\n\n");
    printf("       <password> up to 14 chars \n");
    printf("       <command string> what to be called when successful\n\n");

}


main(int argc, char *argv[])
{

 char passwd[20];
 struct Library *DLGBase=NULL;
 struct Library *OpenLibrary();
 char   port[4];
 FILE *error;

   if(argc == 1) {

	   About();exit(5);
	   }

   DLGBase = OpenLibrary(DLGNAME,0);
   if(!DLGBase) printf("Unable to open DLG.library");

   if(GetDevName(port)==-1)printf(" Unable to get port\n");

   printf("\n\nDLGprotect [Version %s] Password: ", VERSION);
   scanf("%s", passwd);

   if(!strcmp(passwd, argv[1])) {

	      printf("\n\n Password Correct, proceed ...\n");
	      exit(0);
	      CloseLibrary(DLGBase);
	    }
    else {

	      printf("\n\n Password Incorrect, Logging attempt ... \n");
	      sprintf(passwd, "dlg:tkill -p %s\n", port);
	      system(passwd);
	      CloseLibrary(DLGBase);
	      error = fopen("LOGS:DLGprotect.LOG", "w");

	      fprintf(error, "Failed password attempt on port %s\n", port);

	      fclose(error);

	      exit(50);

	    }

}
