#!/usr/bin/env perl
use strict;
use warnings;
use 5.014;

use Future;
use Path::Class;
use File::Which qw(which);
use Cwd qw(getcwd);
use App::Multigit;

sub usage;

my $cmd = shift or say usage and exit 1;

if ($cmd eq 'help') {
    # mg help is just usage
    $cmd = shift or say usage and exit 0;

    # mg help command is mg-command --help
    my $mg_cmd = which("mg-$cmd") // die "$cmd is not an mg command.\n";
    exec $mg_cmd, "--help";
}
elsif ($cmd eq 'init') {
    App::Multigit::init(getcwd);
    exit;
}

my $mg_cmd = which("mg-$cmd") // die "$cmd is not an mg command.\n";

exec $mg_cmd, '--workdir', App::Multigit::mg_parent, @ARGV
    or die "Failed to exec $mg_cmd: $!";

sub usage {
<<EOU;
Usage:
    mg [options] command [command-options]
    mg help [command]
EOU
}
