#!/usr/bin/env raku
use v6.d;
use Property::Text::Controls;
use JSON::Fast;

sub MAIN(Str $file, Bool :$html = False, Bool :$annotated = False, Bool :$byte-offsets = False) {
    CATCH { default { note 'Input or report failure: ' ~ .message; exit 2 } }
    die 'Choose either --html or --annotated' if $html && $annotated;
    die 'Byte offsets describe original input; annotation is a normalized view. Choose one.' if $byte-offsets && $annotated;
    die 'Input must be a regular file' unless $file.IO.f;
    die 'Input exceeds 1 MiB' if $file.IO.s > 1_048_576;
    my $text = $byte-offsets ?? '' !! $file.IO.slurp(:enc<utf8>);
    my %report = $byte-offsets ?? audit-utf8($file.IO.slurp(:bin)) !! audit-text($text);
    say $annotated ?? annotate-text($text) !! $html ?? report-html(%report) !! to-json(%report,:sorted-keys);
    exit (%report<status> eq 'no_selected_controls' ?? 0 !! 1);
}
