#!/usr/bin/perl

use strict;
use warnings;

BEGIN
{
  die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
}

# Pragma to autoflush STDOUT
$| = 1;

if (!defined $ARGV[0])
{
  &printUsage();
}
my $collection = $ARGV[0];
my $threads = 1;
if (defined $ARGV[1])
{
  if ($ARGV[1] =~ /^\d+$/)
  {
    $threads = $ARGV[1];
  }
  else
  {
    &printUsage();
  }
}
my $epoc = 100;
if (defined $ARGV[2])
{
  if ($ARGV[2] =~ /^\d+$/)
  {
    $epoc = $ARGV[2];
  }
  else
  {
    &printUsage();
  }
}

# Remove the current archives directory
`rm_archives.pl $collection`;

# Flush the memory disk cache
`sync`;
# - save our current default editor
my $current_editor = $ENV{'EDITOR'};
# - replace default editor with a script that simply clobbers the contents
#   of any file it's handed with the number "3"
$ENV{'EDITOR'} = 'reset_memcache_editor.sh';
# - we now call sudoedit on the system file. How sudoedit works is that it
#   starts by making a temp copy of the system file with appropriate
#   permissions allowing the user to edit. It then passes the path to the
#   temp file to the default editor - typically this would be an interactive
#   editor like 'vi'. However, we've just replaced the editor with a custom
#   script that just writes '3' as the content of the tmp file. Finally, when
#   the editor exits, sudoedit copies the tmp file over the top of the system
#   file, restoring appropriate root-level permissions
`sudoedit /proc/sys/vm/drop_caches`;
# - restore the default editor, just in case something in Greenstone
#   depends on this being a reasonable value
$ENV{'EDITOR'} = $current_editor;

print "===== GS Import with MPSTAT Poll ====\n";

# Forking children... get off my front lawn!
my $child_pid = fork();

# Ensure fork() worked
if (!defined($child_pid))
{
  die("Fork didn't.");
}

# Parent process polls memory until child is done
if ($child_pid > 0)
{
  print " - parent monitoring child process $child_pid\n";

  # Start polling for a fixed amount of time (4 minutes seems long enough)
  my $timeout = 300;
  my $polling_cmd = 'mpstat -P 0,1,2,3,4,5,6,7 1 ' . $timeout . ' > processor-' . $child_pid . '.log 2>&1';
  `$polling_cmd`;
  print "===== Complete! =====\n\n";
  exit(0);
}
# Child process runs import command
else
{
  print " - child process running GS import\n";

  my $import_cmd = 'taskset -c 1 import.pl -keepold -manifest manifest.xml -verbosity 0 ' . $collection . ' 2>&1';
  if ($threads > 1)
  {
    $import_cmd = 'parallel_import.pl -removeold -verbosity 0 -epoch ' . $epoc . ' -jobs ' . $threads . ' ' . $collection . ' 2>&1';
  }
  print STDOUT "Starting Import: " . `date` . "\n";
  my $result = `$import_cmd`;
  print STDOUT "Ending Import: " . `date` . "\n";
  print " - child complete: " . $result . "\n";
  exit(0);
}

# Doubt we'll ever get here, but who knows.
exit(0);

sub printUsage
{
  die("Usage: poll-processor.pl <collection> [<threads> [<epoc>]]\n");
}

1;
