#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;
use File::Find;

use lib $ENV{'pbench_lib_dir'};
use JSON;
use Data::Dumper;
use PbenchCDM qw(get_cdm_ver get_cdm_rel);
use PbenchBase qw(get_json_file put_json_file remove_element);

my $coder = JSON->new;
my $count = 0;

open(FH, ">import.log") || die "Could not open import.log";;

sub process_dir {
	my $dir = shift;
	my $index = shift;
	my $host = shift;
	opendir(my $dh, $dir) || die "process_dir(): could not open directory: $!\n";
	for my $entry (grep(!/^\./, readdir($dh))) {
		if (-d $dir . "/" . $entry) {
			process_dir($dir . "/" . $entry, $index, $host);
		} else {
			# separate the UUID (like -23316532-b21a-448c-8634-fa91c2a3410c) from the index name)
			if ($entry =~ /(\D+)\d*-[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}[^\.]*\.(ndjson|json$)/) {
				my $doc_type = $1;
				my $file_type = $2;
				my $curl_cmd = "";
				if ($file_type eq "json") {
					$curl_cmd = sprintf "curl --stderr /dev/null -X POST \"%s/%s\" -H \'Content-Type: application/json\' -d@%s", $host, "cdmv" . get_cdm_ver . get_cdm_rel . "-" . $doc_type . "/" . $doc_type, $dir . "/" . $entry;
				} else {
					$curl_cmd = sprintf "curl --stderr /dev/null -X POST \"%s/%s\" -H \'Content-Type: application/x-ndjson\' --data-binary @%s", $host, "cdmv" . get_cdm_ver . get_cdm_rel . "-" . $doc_type . "/" . $doc_type . "/_bulk", $dir . "/" . $entry;
				}
				my $output = `$curl_cmd`;
				if ($? > 0) {
					printf "curl failed, so aborting now\n";
					die;
				}
				my $json_ref = $coder->decode($output);
				printf FH "%s\n", $output;
				if ($file_type eq "ndjson" and $$json_ref{'errors'} eq JSON::true) {
					print "import of $entry failed:\n\n$output\n\n";
					exit;
				}
				if ($file_type eq "json" and exists $$json_ref{'error'}) {
					print "import of $entry failed:\n\n$output\n\n";
					exit;
				}
				$count++;
			} else {
				printf "file %s did not match the name format\n", $entry;
			}
		}
	}
	closedir $dh;
}

my $script_name = basename($0);
my $es_dir = shift(@ARGV);
my $es_host = shift(@ARGV);

# under $es_dir, process the CDM files
opendir(my $es_dh, $es_dir) || die "$script_name: could not open directory clients: $!\n";
my @es_dirs = grep(!/^\./, (sort readdir($es_dh)));
closedir $es_dh;

for my $index (@es_dirs) {
	if ( -d $index) {
		process_dir($es_dir . "/" . $index, $index, $es_host);
	}
}
close(FH);
printf "Processed %d files\n", $count;
