/*-----------------------------------
 * this code is used to get the keypad entries 
 * and pass them to the insert.pl script to be 
 * entered in the database.
 *---------------------------------*/

#include <stdio.h>


int main(){
   char key;
   int num;
   int mag;
   int pid;
   char location[32] = "keypad";
   char timestamp[32] = "now"; 
   char command[128]; 
   
   while(1){
      printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      printf("Please enter your pin number and press enter\n");
      mag = 10;
      pid = 0;
      while(1){
	 key =  getc(stdin);//get single key stroke
	   if(key == '\n'){
	      break;
	   }
	 else if(key == 'q'){
	    exit(0);
	 }
	 else{
	    num = (int)key;
	    num = num - 48;
	    pid = pid + (mag * num);
	    mag = mag / 10; 
	    //since mag is an int anything after the first 2 digits gives 0
	 }
      }
      
      //package the values to be passed to insert.pl
      sprintf(command, "./insert.pl %d %s %s", pid, location, timestamp); 
      printf("inserting for pid %d\n", pid);
      //call insert.pl
      system(command);
   }
   
}

