#!/usr/bin/perl

# report_export - Created by James Pattie, PC & Web Xperience, Inc.
# 09/29/2000.  This script will export the system level reports into a text
# file for inclusion in the distribution.

use strict;
use Pg;

use vars '$conn';
use vars '$result';

my $version = "";
my $db_name = "";
my $db_passwd = "";
my $db_host = "";
my $db_user = "";

if (length $db_name <= 0)
{
  print "Database Name:";
  while (length $db_name <= 0)
  {
    $db_name = <STDIN>;
  }
  chomp $db_name;
  print "Database Host(Server) [localhost]:";
  $db_host = <STDIN>;
  chomp $db_host;
  if ($db_host == "")
  {
    $db_host = "localhost";
  }
  
  print "Database User [portal]:";
  $db_user = <STDIN>;
  chomp $db_user;
  if ($db_user == "")
  {
    $db_user = "portal";
  }
  print "    The data you entered was:\n$db_name\n";
  print "$db_host\n$db_user\n";
  print "\nType Ctrl+C to cancel or Enter to continue:\n";
  <STDIN>;
}

#check to see if any report names have commas in them.
my $sql = "SELECT name, id FROM report_type_tb ORDER BY name";
my $check = `/usr/bin/psql -c "$sql" -t -Pformat=unaligned -U$db_user -h$db_host $db_name`;
my @check = split('\n',$check);
foreach my $line (@check)
{
#  print $line;
  my @fields = split /\|/, $line;
  if ($fields[0] =~ /,/)
  {
    print "Warning!  There is a comma in report $fields[1] named '$fields[0]'.\n";
    if ($fields[1] >= 500)
    {
      print("This report will be skipped anyway because it is not a system level report.\n");
    }
    else
    {
      print("This report is a system level report and the comma will cause problems with this data backup!.\nPlease remove commas from all system reports (id < 500)\nExiting . . .\n");
      exit 1;
    }
  }
}

# now call pg_dump on these tables and then massage the resulting data files.
print("Dumping report_type_tb\n");
my $error = `/usr/bin/pg_dump -U$db_user -a -d -D -h $db_host -t report_type_tb $db_name`;
if ($error =~ /([E|e]rror|incorrect password)/)
{
  die "Error:  Failed to dump report_type_tb!\n$error\n";
}
if (length $error == 0)
{
  die "Error:  Failed to dump report_type_tb!\n$error\n";
}

my @reports = split /\n/, $error;

my $call = "/usr/bin/pg_dump -U$db_user -a -d -D -h $db_host -t report_data_tb $db_name";
print("Dumping report_data_tb with\n$call\n");
my $error = `$call`;
if ($error =~ /([E|e]rror|incorrect password)/)
{
  die "Error:  Failed to dump report_data_tb!\n$error\n";
}
if (length $error == 0)
{
  die "Error:  Failed to dump report_data_tb!\n$error\n";
}

my @report_data = split /\n/, $error;

# Open the file system_reports.psql and work with it.
open(SYSREPORTS, ">system_reports.psql") or die "Error: Opening Reports File Failed!\n$!\n";

print SYSREPORTS "DELETE FROM report_type_tb WHERE id < 500;\n";
print SYSREPORTS "DELETE FROM report_data_tb WHERE id < 500;\n";

my $skip;
my $added;
my $debug;
foreach my $line (@reports)
{ 
#  print $line."\n";
  if ($line =~ /^INSERT INTO "?report_type_tb"?/)
  {
    my @line = split ("VALUES", $line);
    @line = split (",", $line[1]);
    my $id = $line[1];
    if ($id =~ /\d+/ && $id < 500)
    {
      print SYSREPORTS $line . "\n";
      $added .= "$id,";
    }
    else
    { 
      if ($id !~ /\d+/) { $debug .= $line; }
      $skip .= "$id,";
    }
  }
}
print SYSREPORTS "UPDATE report_type_tb SET underline_length = '-1' WHERE id < 500;\n";

print "Skipped: $skip\n";
print "Added: $added\n";
# print "##########DEBUG\n$debug\n";
$skip = "";
$added = "";
print "\n\nNow processing data\n";
#print "Press enter to continue";
#<STDIN>;
foreach my $line (@report_data)
{
#  print "####\n$line\n";
  if ($line =~ /^INSERT INTO "?report_data_tb"?/)
  {
    my @line = split ("VALUES", $line);
    $line[1] =~ s/^ \(//;
    $line[1] =~ s/\);$//; 
    @line = split (",", $line[1]);
    my $id = $line[0];
    if ($id =~ /\d+/ && $id < 500)
    {
      #check for underline field on most rows and set to -1
      if ($line[3] =~ /total|compare/)
      {
        $line =~ s/underline=([^|'\s])*/underline=-1/;
#        print $line."\n";
      }
      print SYSREPORTS $line . "\n";
      $added .= "$id,";
    }
    else
    { 
      $skip .= "$id,";
    }
  }
}
print "Skipped: $skip\n";
print "Added: $added\n";

close (SYSREPORTS);

print "\nReport Exportation Succeeded.\n";
