#!perl

use strict;
use warnings;

use SPVM::Builder::Util;
use SPVM::Global;
use FindBin;
use blib ();

SPVM::Builder::Util::getopt
  # spvmcc and spvm shared
  'h|help'          => \my $help,
  'v|version'       => \my $show_version,
  'I=s' => \my @spvm_include_dirs,
  'B|build-dir=s'   => \my $build_dir,
  'w'               => \my $warning,
  'Mblib' => \my $blib,
  
  # spvm only
  'e=s' => \my $source,
  'c' => \my $syntax_ok,
  'M=s' => \my @use_class_names,
;

if ($help) {
  print SPVM::Builder::Util::extract_usage;
  exit 0;
}
elsif ($show_version) {
  my $version_string = "spvm v$SPVM::VERSION";
  print "$version_string\n";
  exit 0;
}

if (defined $build_dir) {
  $ENV{SPVM_BUILD_DIR} = $build_dir;
}

my $script_name = shift;

if (!defined $script_name && !defined $source && !defined $source) {
  die SPVM::Builder::Util::extract_usage;
}

if (defined $script_name && defined $source) {
  die "[spvm command]Specifying both SCRIPT_NAME and -e option is not allowed.";
}

if (defined $source) {
  $0 = "-e";
}
elsif (defined $script_name) {
  
  $0 = $script_name;
  
  open my $script_fh, '<', $script_name
    or die "Can't open file \"$script_name\":$!";
    
  $source = do { local $/; <$script_fh> };
  
  $source = "#file \"$script_name\"\x{A}$source";
}

FindBin::again();

@INC = @{SPVM::Builder::Util::resolve_inc(\@INC, $source, $blib, \@spvm_include_dirs)};

if ($warning) {
  $^W = 1;
}

for my $use_class_name (@use_class_names) {
  SPVM::Global::build_class($use_class_name, __FILE__, __LINE__);
}

SPVM::Global::build_class('Native', __FILE__, __LINE__);
SPVM::Global::build_class('Native::MethodCall', __FILE__, __LINE__);

my $anon_class_name = SPVM::Global::build_anon_class($source, __FILE__, __LINE__);

SPVM::Native->check_bootstrap_method($anon_class_name);

my $spvm_anon_class_name = "SPVM::$anon_class_name";

if ($syntax_ok) {
  print STDERR "syntax OK\n";
  exit 0;
}

$spvm_anon_class_name->main;

=encoding utf8

=head1 Name

spvm - Executing SPVM programs

=head1 Description

The spvm command executes SPVM programs.

=head1 Usage

  Usage: spvm [OPTIONS] SCRIPT_NAME
    
    spvm myapp.spvm
    
  Options:
    -h, --help                     Shows this message
    -v, --version                  Shows the version
    -I DIRECTORY    Adds a include directory
    -B, --build-dir DIRECTORY      Build diretory
    -e SOURCE                      Executes a program source code in main method.
    -c                             Check syntx only
    -w                             Enables warning flag
    -M CLASS_NAME                  Loads the SPVM class
    -Mblib                         Same as "-Mblib" in perl

=head1 Details

  spvm [OPTIONS] SCRIPT_NAME

The C<spvm> command executes a SPVM program.

I<OPTIONS> are L<options|/"Options">.

I<SCRIPT_NAME> is a script path that contains an SPVM script.
  
  # myapp.spvm
  use Fn;
  my $var = 1;
  say $var;

Anon class that contains a L<bootstrap method|SPVM::Document::Language::Class/"Bootstrap Method"> is allowed as an SPVM script.

  # myapp.spvm
  class {
    
    version "1.001";
    
    our $FOO : int;
    
    use Fn;
    
    static method main : void () {
      my $var = 1;
      say $var;
    }
  }

The base name of I<SCRIPT_NAME> must be a L</"Script Base Name">. Otherwise an exception is thrown.

See L<Class Search Directories|SPVM::Document::Language::Class/"Class Search Directories"> about default class search directories.

See L<SPVM::Document::EnvironmentVariables> about available environment variables.

=head2 Script Base Name

A script base name ends with C<.spvm>.

A script base name without C<.spvm> consists of L<word characters|SPVM::Document::Language::Tokenization/"Word Characters"> and C<->.

It dose not contains C<__>.

It dose not begin with C<0-9>.

It dose not begin with C<->.

It dose not end with C<->.

It dose not contains C<-->.

Examples:
  
  # Script Base Names
  foo.spvm
  foo_bar2.spvm
  myapp-foo.spvm
  
  # Invalid Script Base Names
  2foo.spvm
  foo__bar.spvm
  -myapp.spvm
  myapp-.spvm
  myapp--foo.spvm

=head1 Options

=head2 --help

Outputs how to use the C<spvm> command to standard output.

=head2 -h

  -h

Same as L</"--help">.

=head2 --version

Outputs the version of the C<spvm> command to standard output. This version is the same as the version of L<SPVM>.

=head2 -v

  -v

Same as L</"--version">.

=head2 -I

  -I DIRECTORY

Prepends I<DIRECTORY> to L<class search directories|SPVM::Document::Language::Class/"Class Search Directories">

This option can be specified multiple times.

  -I dir1 -I dir2

In this case, class search directories becomes the following.

  [dir1, dir2, default_dirs]

=head2 --build-dir

  --build-dir DIRECTORY

Sets L<SPVM_BUILD_DIR|SPVM::Document::EnvironmentVariables/"SPVM_BUILD_DIR"> environment variable to I<DIRECTORY>.

=head2 -B

  -B DIRECTORY

Same as L</"--build-dir">.

=head2 -w

  -w

Enables warning flag.

Implementation:

Sets L<CommandInfo#WARNING|SPVM::CommandInfo> class variable to 1.

=head2 -Mblib

  -Mblib

Uses Perl's L<blib> module.

This is useful for testing a native module before it is installed.

When this option is specified, C<blib/lib> and C<blib/arch> are prepended to C<@INC> and the library search paths.

=head2 -e

  -e SOURCE

Executes a source code I<SOURCE> in C<main> method.

Examples:

  spvm -e 'say "Hello World!";';
  
=head2 -c

  -c

Checks syntax only.

=head2 -M

  -M CLASS_NAME

Loads the SPVM class I<CLASS_NAME>.

This is useful for pre-loading classes or ensuring that a class is compiled.

This option can be specified multiple times.

  spvm -MFoo -MBar myapp.spvm

Implementation:

Calls L</"build_class"> method for I<CLASS_NAME>.


=head1 lib Directive

If the source code specified by I<SCRIPT_NAME>, or C<-e> option contains lib directives, The directories specified by lib directive is prepeneded to L<class search directories|SPVM::Document::Language::Class/"Class Search Directories">.
  
  #lib "$FindBin::Bin/lib"
  
This directories specified by lib directive is placed after the directories specified by L</"-I"> option.

=head1 Copyright & License

Copyright 2023 Yuki Kimoto. All Rights Reserved.

MIT License.
