#!/usr/bin/perl -w
# Read and then write report file Feb 6, 2013
# perl PlPrepareRand.txt GradientRating.txt > PreparedGradient.txt

use strict;
srand(time|$$);

sub ReadFile
{
    my ($FileName) = @_;
    unless(open(MYFILE, $FileName))
    {
        print "Cannot open file $FileName\n";
        exit;
    }
    my @Data = <MYFILE>;
    close MYFILE;
    return @Data;
}

my $FileName = $ARGV[0];
my @DataI = ReadFile($FileName);

my @DataO;
my $LineCum = "";
foreach my $Line (@DataI)
{
    if ($Line =~ /\S/)
    {
        $LineCum = $LineCum . $Line;
    }
    else
    {
        push(@DataO, $LineCum);
        $LineCum = "";
    }
}

until ($DataO[0] =~ /\S/)
{
    shift @DataO;
}

my @RandList;
my $Start =  10000;
my $End   = 100000;
my $Count = $Start;

while ($Count++ < $End)
{
    push(@RandList, $Count);
}

my $Num;
my $RandNum;
my $Size;
$Count = 1;
foreach my $Line (@DataO)
{
    $Size = scalar @RandList;
    $RandNum = int rand($Size);
    $Num = $RandList[$RandNum];
    unless ($Line =~ /^Drug:/)
    {
         print "Report: $Num\n";
#        print "Report: $Count\n";
#        $Count++;
    }
    print $Line;
    unless ($Line =~ /^Drug:/)
    {
        print "PA - Creative visuals\nPA - Ego-loss\nPA - Loss of contact with reality\n\n";
    }
    else
    {
        print "\n";
    }
    $RandList[$RandNum] = $RandList[0];
    shift @RandList;
}
