#!/usr/bin/perl

use strict;
use warnings;

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

# For WNOHANG flag
use POSIX ":sys_wait_h";
# For intervals less than 1 second
use Time::HiRes qw(usleep gettimeofday);

# 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
# - 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;

my $import_cmd = 'taskset -c 0 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';
}
my $polling_cmd = 'ps -u jmt12 -o pid,psr,pcpu,pmem,cmd --sort pid';

print "===== GS Import with PS 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";

  open(POLLOUT, '>:utf8', 'memory-' . $child_pid . '.log') or die('Failed to open file for writing: memory-' . $child_pid . ".log\n");
  my $result = 0;
  while ($result >= 0)
  {
    my ($seconds, $useconds) = gettimeofday();
    print POLLOUT '[' . $seconds . '.' . $useconds . "]\n";
    my $result = `$polling_cmd 2>&1`;
    print POLLOUT $result . "\n";
    usleep(500000); # Half a second
    # I believe result is non-zero iff the child process has completed, in
    # which case result should be the child process id (again)
    $result = waitpid($child_pid, WNOHANG);
    # Watch for errors in waitpid
    print "Result: " . $result . "\n";
  }
  close(POLLOUT);
  print "===== Complete! =====\n\n";
  exit(0);
}
# Child process runs import command
else
{
  print " - child process running GS import\n";
  my $result = `$import_cmd`;
  print $result . "\n";
  exit(0);
}

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

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

1;
