#!/usr/bin/perl -w
#0123456789012345678901234567890123456789

system("sync");
#`wmsetbg /home/mann/seatsale/agree.pgm`;
######`xv /home/mann/seatsale/agree.pgm`;
`outport37A 16`; # write 0x10 to 37A to enable IRQ, in case biomouse reset it
# above line is in case biomouse was run

while (1) {
  `outport37A 16`; # write 0x10 to 37A to enable IRQ, in case biomouse reset it
  system("clear"); # second time around loop, we'll want to start clear screen
  print "Swipe credit card or government issued\n";
   print "photo ID card to download a FREE Seating\n";
   print "License.\n";
  print "\n";
  print "Your card is for identification\n";
   print "purposes only. The seating is FREE!!!\n";
         #0123456789012345678901234567890123456789
  print "\n";
  print "By swiping your card, you agree to be\n";
   print "bound by our Terms and Conditions.\n";
  print "\n";
  print "Your swipe indicates your agreement to\n";
   print "these Terms and Conditions of use.\n";
  print "\n";
  print "If you do not agree to our Terms and\n";
   printf "Conditions, remain standing and do not\n";
   printf "swipe your card through the card reader!\n";
  $data = `/home/mann/seatsale/readcard_once.pl`;

  #check if $data is already in logfile
  #$identifier = $data;
  #$data =~ m/Identifier: /;
  $garbage = $data;
  $garbage =~ s/Identifier: (.*)//;
  $identifier = $1; # keep everything after "Identifier: "
  #$identifier =~ s/.*Identifier: //; # sed out everything before "Identifier: "
  chomp ($identifier);
  # get rid of any white spaces as per "strangeness.pl" example
  #$identifier =~ s/\s{2,}/ /; # two or more white spaces
  #$identifier =~ s/(\s){2,}/ /; # same as above
  #$identifier =~ s/ \s*/ /; # one space and then zero or more
  #$identifier =~ s/  \s*/ /; # two spaces and then zero or more
  #$identifier =~ s/\s\s*/ /; # one white space and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  $identifier =~ s/\s\s\s*/ /; # two white spaces and then zero or more
  #$identifier =~ s/ *$//; # remove trailing space
  $identifier =~ s/\s*$//; # remove trailing whitespace

  # save identifier into vindex number association file for picture reference
  system("./seatsafety.pl 80"); # take picture of customer
  $lastvindex = `./lastv.pl`;
  chomp($lastvindex);
  #$nextvindex = $lastvindex + 1;
  #print "lastvindex = $lastvindex and identifier = $identifier";
  system("echo v$lastvindex.jpg $identifier >> logfilev.txt");
  # system("sleep 100");

  print "\n";
  print "Identifier recovered from card parse:\n".$identifier."\n";
        #0123456789012345678901234567890123456789
  # system("sleep 5");
  #reading from logfile will ensure only one free seat per applicant card
  #open (LOGFILEhandle, "logfile.txt");
  open (LOGFILEhandle, "< logfile.txt"); # open logfile for read only
  $previouslyswiped = 0;
  while (defined ($identifieri = <LOGFILEhandle>)) {
    chomp ($identifieri);
    #print "identifier =".$identifier."end\n";
    #print "identifieri=".$identifieri."end\n";
    if ($identifier eq $identifieri) {
      #$identifierprevious = $identifieri;
      $previouslyswiped = $previouslyswiped + 1;
      #there is no break command to break out of while loop to save some time 
      #last;# is this the same as "break" would be if they had the sense to use?
    }#end if
  }
  close (LOGFILEhandle);
  print "\n";
  #print "Identifier matched from previous use:\n".$identifierprevious."\n";
  #print "previouslyswiped = $previouslyswiped\n";
  #system("sleep 100");
  if ($previouslyswiped <= 1) {
    system("clear");
    print $data;
    print "________________________________________\n";
    print "\n";
    print "Thank you for agreeing to our Terms and\n";
     printf "Conditions of use.\n";
    `/home/mann/seatsale/retract.pl`;
    print "________________________________________\n";
    print "\n";
    print "Enjoy Quality of Seating Services (TM)\n";
     print "Middleware and QoS Provisioning.\n";
           #0123456789012345678901234567890123456789
     ###print "ParaSite (TM)\n";
    `/home/mann/seatsale/retract.pl`;
    `sleep 5`; # time to read the thank-you notice
    #`plaympeg --double --fullscreen /home/mann/seatsale/shootingback/nn.mpg`;
    `./movieselector.pl`; # randomly plaympeg one of movies in ...shootingback/
    for ($annoy =0; $annoy<10; $annoy++) {
      system("xv /home/mann/seatsale/expiring5seconds.jpg &"); # `xv &` doesn't backgnd
      system("usleep 100000");
      system("xv /home/mann/seatsale/expiring5secondsneg.jpg &");
    } # end for
    print "Your Seating License will expire in 4 seconds\n";
    print "Please swipe your credit card or\n";
    print "contact the SeatWorks to renew your license\n";
    `sleep 1`;
    `/home/mann/seatsale/buzzeron.pl`;
    print "WARNING: Your Seating License will\n";
    print "expire in 3 seconds!\n";
    print "Please get off the chair!\n";
    print "contact the SeatWorks to renew your license.\n";
    `sleep 5`;
    print "License Expired\n";
    system("killall xv");
    `/home/mann/seatsale/unretract.pl`; 
    `/home/mann/seatsale/buzzeroff.pl`; 
    # intro to give solenoid capacitor time to discharge approx 5 RC=5*R*1farad
    system("xv seatingmadesimple.jpg &");
    system("sleep 8");
    system("killall xv");
    system("sync");
  }#end if not previously seated
  elsif ($previouslyswiped <=2) {
    print "\n";
    print $data;
    print "\n";
    #system("sleep 2");
    print "________________________________________\n";
    print "\n";
    print "You already had a FREE Seating License\n";
          #0123456789012345678901234567890123456789
    print "with ".$identifier."\n";
    print "\n";
    print "Since you already had your free trial\n";
    print "license, please let SOMEONE ELSE\n";
    print "download their free Seating License.\n";
    system("sleep 7");
    system("clear"); print "Goodbye\n"; system("sleep 1");
    print "Resetting\n"; system("sleep 1");
  }# end else if (e.g. if previously seated)
  else {
    print "\n";
    print $data;
    #system("sleep 2");
    system("clear");
    print "You are attempting to obtain an\n";
    print "unauthorized ADDITITIONAL License.\n";
    print "You have previously swiped this card\n";
    $previouslyseated = $previouslyswiped - 1;
    print "$previouslyseated times!!!\n";
    print "\n";
    print "Please note that obtaining unauthorized\n";
    print "Reality Services (TM) is THEFT and may\n";
    print "result in Legal Action or Criminal\n";
    print "Prosecution under the Uniform Security\n";
          #0123456789012345678901234567890123456789
    print "Ecommerce Accounting Transactions Act\n";
    print "(UseatA).\n";
    print "\n";
    print "Your identifier and photograph are being\n";
    print "transmitted to Investigators...\n";
    print "\n";
    print $identifier."\n";
    system("sleep 8");
    system("clear"); print "Goodbye\n"; system("sleep 1");
    print "Resetting\n"; system("sleep 1");
  }# end else if (e.g. if previously seated)
  # system("sleep 100");
}#end while 1

