use alienfile;

use Path::Tiny qw( path );

plugin 'Probe::CommandLine' => (
  command => 'rustc',
  args    => [ '--version' ],
  match   => qr/^rustc ([0-9\.]+)/,
  version => qr/^rustc ([0-9\.]+)/,
);

share {
  my $rustup;
  if( $^O ne 'MSWin32' ) {
    $rustup = {
      url => 'https://static.rust-lang.org/rustup/rustup-init.sh',
      exe => [ qw(sh ./rustup-init.sh) ],
    }
  } else {
    $rustup = {
      url => 'https://static.rust-lang.org/rustup/dist/i686-pc-windows-gnu/rustup-init.exe',
      exe => [ qw(.\\rustup-init.exe) ],
    };
  }
  start_url $rustup->{url};
  plugin 'Download';
  plugin 'Extract::File';
  build [
    sub {
      my ($build) = @_;
      $ENV{CARGO_HOME}  = $build->install_prop->{prefix};
      $ENV{RUSTUP_HOME} = $ENV{ALIEN_RUST_RUSTUP_HOME} || path($build->install_prop->{prefix})->child('.rustup');
      $build->log( "CARGO_HOME  = $ENV{CARGO_HOME}");
      $build->log( "RUSTUP_HOME = $ENV{RUSTUP_HOME}");
    },
    [ @{ $rustup->{exe} },
      qw(--verbose -y),
      qw(--no-modify-path),
      qw(--profile default),
    ],
    sub {
      my ($build) = @_;
      my $from = $build->install_prop->{prefix};
      my $to   = $build->runtime_prop->{prefix};
      $build->log( "Changing contents of env file: $from -> $to");
      my $env = path($from)->child('env');
      $env->edit_utf8(sub {
        s/\Q$from\E/$to/g;
      }) if -f $env;
    },
  ];

  gather sub {
     my ($build) = @_;
     $build->runtime_prop->{'version'} = ( `rustc --version` =~ qr/^rustc ([0-9\.]+)/)[0];
  };
}
