#!/usr/bin/perl

use strict;
use FindBin qw($Bin);
use Getopt::Long;

sub usage {
    die "Usage: dev-indexer [--wipe] <portnumber> -- [other_blobserver_opts]";
}

my $opt_wipe;
GetOptions("wipe" => \$opt_wipe)
    or usage();

my $port = shift || "3200";
usage() unless $port =~ /^\d+$/;

system("./build.pl", "server/go/camlistored") and die "Failed to build camlistored";
system("./build.pl", "clients/go/camdbinit") and die "Failed to build camdbinit";

my $DBNAME = "devcamlistore";
my @opts;
if ($opt_wipe) {
    push @opts, "-wipe";
} else {
    push @opts, "-ignoreexists";
}

system("./clients/go/camdbinit/camdbinit",
       "-user=root",
       "-password=root",
       "-host=localhost",
       "-database=$DBNAME",
       @opts) and die "Failed to run camdbinit.\n";

print "Starting indexer with indexer on http://localhost:$port/indexer/\n";

$ENV{CAMLI_PASSWORD} = "pass$port";
$ENV{CAMLI_PORT} = $port;
exec("$FindBin::Bin/server/go/camlistored/camlistored",
     "-configfile=$Bin/config/dev-indexer-config.json",
     "-listen=:$port",
     @ARGV);
