#!/usr/bin/env perl6

use JSON::Infer;

multi sub MAIN(Str :$file, Str :$uri, Str :$out-dir = "lib", Str :$class-name!, Bool :$kebab = False) {

    CATCH {
        when X::Infer {
            note $_.message;
            exit 5;
        }
    }
    my IO::Path $base-dir = do if $out-dir.IO.is-relative {
        $*CWD.child($out-dir);
    }
    else {
        $out-dir.IO;
    }

    my $infer = JSON::Infer.new;

    my $class = do if $uri.defined {
        $infer.infer(:$uri, :$class-name, :$kebab);
    }
    elsif $file.defined {
        $infer.infer(:$file, :$class-name, :$kebab);
    }
    else {
        X::Infer.new(message => "one of file or uri must be supplied").throw;
    }

    my $out-file = $base-dir.child($class.file-path);

    my $parent = $out-file.parent;

    if not $parent.d {
        $parent.mkdir;
    }

    my $out = $out-file.open(:w);

    $out.print($class.make-class);
    $out.close;
}


# vim: expandtab shiftwidth=4 ft=perl6
