#****************************************************************************
# Prediction.pm
# By Dan Malone
# See CHANGE THESE below
#****************************************************************************

package Prediction;


sub IDExists
{
  my ($ID) = @_;

  if ($PredictionID{$ID}) {
    return 1;
  } else {
    return 0;
  }
}

sub GetInfo
{
  my ($ID) = @_;

  if ($PredictionID{$ID}) {
    # ID exists, return the info.
    return 
      ($PredictionID{$ID}, $PredictionOwner{$ID}, $PredictionBoyName{$ID},
       $PredictionGirlName{$ID}, $PredictionSex{$ID}, $PredictionMonth{$ID},
       $PredictionDay{$ID}, $PredictionHour{$ID}, $PredictionMinute{$ID},
       $PredictionAMPM{$ID}, $PredictionPounds{$ID}, $PredictionOunces{$ID},
       $PredictionLength{$ID});
  } else {
    # ID does not exist
    return 0;
  }
}

#****************************************************************************
# sub AllIDs
#****************************************************************************


sub AllIDs
{
  return (sort keys %PredictionID);
}


#****************************************************************************
# sub AllIDsByOwner
#****************************************************************************


sub AllIDsByOwner
{
  my (%OwnerID) = ();
  my (@List) = ();
  my ($ID,$Owner) = ();

  for $ID (keys %PredictionID) {
    $OwnerID{$PredictionOwner{$ID}} = $ID;
  }

  for $Owner (sort keys %OwnerID) {
    push(@List,$OwnerID{$Owner});
  }

  return (@List);
}


#****************************************************************************
# sub ReadAll
#****************************************************************************


sub ReadAll 
{
  # The current line read from the input file.
  my ($CurrentLine);

  # The data items for the current computer being read in.
  my ($ID, $Owner, $BoyName, $GirlName, $Sex, $Month, $Day, $Hour, $Minute);
  my ($AMPM, $Pounds, $Ounces, $Length);

  # Open the file to read the data from, and check if open succeeds.
  open(INFILE,"<$DataFile") || 
    die "Could not open file $DataFile \n";

  # Read the input file line by line.
  while ($CurrentLine = <INFILE>) 
  {
    # Remove the newline character from the end of the input line.
    chop($CurrentLine);

    # If the line does not start with a comment character, a whitespace
    # character, or is not empty, we read in the information.
    if (not(($CurrentLine =~ /^[#\s]/) || ($CurrentLine eq ""))) 
    {
      # Extract the data items for the current prediction from the input 
      # line and store them in the corresponding global associative arrays.
      ($ID, $Owner, $BoyName, $GirlName, $Sex, $Month, $Day, $Hour, $Minute,
       $AMPM, $Pounds, $Ounces, $Length) = split(/$Seperator/, $CurrentLine);
      $PredictionID{$ID} = $ID;
      $PredictionOwner{$ID} = $Owner; 
      $PredictionBoyName{$ID} = $BoyName;
      $PredictionGirlName{$ID} = $GirlName;
      $PredictionSex{$ID} = $Sex;
      $PredictionMonth{$ID} = $Month;
      $PredictionDay{$ID} = $Day;
      $PredictionHour{$ID} = $Hour;
      $PredictionMinute{$ID} = $Minute;
      $PredictionAMPM{$ID} = $AMPM;
      $PredictionPounds{$ID} = $Pounds;
      $PredictionOunces{$ID} = $Ounces;
      $PredictionLength{$ID} = $Length;
    }
  }

  close(INFILE);
}





#****************************************************************************
# sub WriteAll
#****************************************************************************


sub WriteAll
{
  # The ID for the current computer being written to the output file.
  my ($ID);

  # Open the output file and test for success.
  open(OUTFILE,">$DataFile") || 
    die "Cannot open file $DataFile \n";

  print OUTFILE "# Autogenerated table of predictions\n";
  print OUTFILE "# This file is auto-generated by Prediction.pm\n";
  print OUTFILE "# Comments added will be lost\n";

  for $ID (sort keys %PredictionID) 
  {
    print OUTFILE $ID;
    print OUTFILE $Seperator;
    print OUTFILE $PredictionOwner{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionBoyName{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionGirlName{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionSex{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionMonth{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionDay{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionHour{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionMinute{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionAMPM{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionPounds{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionOunces{$ID};
    print OUTFILE $Seperator;
    print OUTFILE $PredictionLength{$ID};
    print OUTFILE "\n";
  }

  close(OUTFILE);
}


#****************************************************************************
# sub Add
#****************************************************************************


sub Add 
{
  my ($ID);
  my ($MaxID) = (100);

  # Get next ID
  for $ID (keys %PredictionID) {
    if ($ID > $MaxID) {
      $MaxID = $ID;
    }
  }
  $ID = $MaxID + 1;

  &Modify($ID, @_);
  return 1;
}

#****************************************************************************
# sub Modify
#****************************************************************************


sub Modify 
{
  my ($ID, $Owner, $BoyName, $GirlName, $Sex, $Month, $Day, 
      $Hour, $Minute, $AMPM, $Pounds, $Ounces, $Length) = @_;

  $PredictionID{$ID} = $ID;
  $PredictionOwner{$ID} = $Owner; 
  $PredictionBoyName{$ID} = $BoyName;
  $PredictionGirlName{$ID} = $GirlName;
  $PredictionSex{$ID} = $Sex;
  $PredictionMonth{$ID} = $Month;
  $PredictionDay{$ID} = $Day;
  $PredictionHour{$ID} = $Hour;
  $PredictionMinute{$ID} = $Minute;
  $PredictionAMPM{$ID} = $AMPM;
  $PredictionPounds{$ID} = $Pounds;
  $PredictionOunces{$ID} = $Ounces;
  $PredictionLength{$ID} = $Length;

  $Changed = 1;
  return 1;
}

#****************************************************************************
# sub Delete
#****************************************************************************


sub Delete 
{
  my ($ID) = @_;

  # Test if the ID exists. 
  if ($PredictionID{$ID}) {
    # this ID exists. 
    delete $PredictionID{$ID};

    $Changed = 1;
    return 1;
  } else {
    # There is no such ID. 

    return 0;
  }
}





#****************************************************************************
# sub DebugPrint
#****************************************************************************


sub DebugPrint 
{
  # The ID of the current computer.
  my ($ID);

  for $ID (sort keys %PredictionID) 
  {
    print $ID;
    print $Seperator;
    print $PredictionOwner{$ID};
    print $Seperator;
    print $PredictionBoyName{$ID};
    print $Seperator;
    print $PredictionGirlName{$ID};
    print $Seperator;
    print $PredictionSex{$ID};
    print $Seperator;
    print $PredictionMonth{$ID};
    print $Seperator;
    print $PredictionDay{$ID};
    print $Seperator;
    print $PredictionHour{$ID};
    print $Seperator;
    print $PredictionMinute{$ID};
    print $Seperator;
    print $PredictionAMPM{$ID};
    print $Seperator;
    print $PredictionPounds{$ID};
    print $Seperator;
    print $PredictionOunces{$ID};
    print $Seperator;
    print $PredictionLength{$ID};
    print "\n";
  }
}





#****************************************************************************
# sub BEGIN
#****************************************************************************


sub BEGIN 
{
  #....................................................................
  # Declarations of constants, variables and arrays that are global for 
  # package computer.
  #....................................................................

  # Associative arrays that hold lists of the various data items, where the 
  # unique ID for each computer serves as the index key.
  %PredictionID = ();  
  %PredictionOwner = ();  
  %PredictionBoyName = (); 
  %PredictionGirlName = ();
  %PredictionSex = ();
  %PredictionMonth = ();
  %PredictionDay = ();
  %PredictionHour = ();
  %PredictionMinute = ();
  %PredictionAMPM = ();
  %PredictionPounds = ();
  %PredictionOunces = ();
  %PredictionLength = ();

  # Variable for defermining writeout of files, 1 equals write
  $Changed = 0;

  # Define the file where the computer data will be stored, and the
  # method of separation between the computer data items.
  # CHANGE THESE ****
  $HomeDir = "/home/www/public_html/Malone2.0";
  $DataFile = "$HomeDir/Predictions.csv";
  # CHANGE THESE ****
  $Seperator = ",";

  $LockFile = "$HomeDir/lockfile";
  my ($Counter) = 10;
  while ($Counter > 0) {
    if (-e $LockFile) {
      sleep(1);
      $Counter--;
    } else {
      $Counter = 0;
    }
  }
  system("/usr/bin/touch $LockFile");

  # Read the data file into the global associative arrays.
  &ReadAll;
}





#****************************************************************************
# sub END
#****************************************************************************


sub END 
{
  # Write the global associative arrays into the data file.
  if($Changed eq "1") {
     &WriteAll;
  }
  system("/usr/bin/rm $LockFile");
}

1;
