#!/opt/bin/perl

use strict;
use warnings;
use Digest::SHA ();
use File::Copy ();
use File::Path ();
use File::Temp ();

my $version = shift || "9.0.4";
$version =~ /^9\.\d+\.\d+$/ && !@ARGV
   or die "Usage: perl fetch-binaries [9.x.y]\n";

my %platforms = (
   "linux-x86_64"     => "Linux64-intel-dyn",
   "linux-aarch64"    => "Linux64-arm-dyn",
   "linux-riscv64"    => "Linux64-riscv-dyn",
   "darwin-universal" => "Darwin64-universal-dyn",
   "mswin32-x86_64"   => "win64-intel-dyn.exe",
   "mswin32-x86"      => "win32-intel-dyn.exe",
);

my %bundled = map { $_ => 1 } qw(linux-x86_64 darwin-universal mswin32-x86_64);
my $tmp = File::Temp::tempdir (CLEANUP => 1);
my $url = "https://www.bawt.tcl3d.org/download/Tclkits/tclkits-$version.7z";

system "curl", "-fL", "-o", "$tmp/kits.7z", $url
   and die "Download failed: $url\n";

system "7z", "e", "-y", "-o$tmp", "$tmp/kits.7z",
   map "tclkits-$version/tclkit-$version-$_", values %platforms
   and die "Extraction failed\n";

# Remove unsafe build-host search paths before computing the inventory.
my $kit = "$tmp/tclkit-$version-$platforms{\"linux-x86_64\"}";
File::Copy::copy ($kit, "$tmp/patch-kit") or die "copy $kit: $!\n";
chmod 0700, "$tmp/patch-kit" or die "chmod: $!\n";

for my $platform (sort grep /^linux-/, keys %platforms) {
   my $file = "$tmp/tclkit-$version-$platforms{$platform}";
   system "$tmp/patch-kit", "patch-tclkit.tcl", $file, "$file.patched", "$tmp/tk.so"
      and die "Patching $platform failed\n";
   rename "$file.patched", $file or die "rename $file: $!\n";
}

mkdir "$tmp/prebuilt" or die $!;
mkdir "$tmp/wwwdist" or die $!;

my %digests;

for my $platform (sort keys %platforms) {
   my $file = "$tmp/tclkit-$version-$platforms{$platform}";

   open my $fh, "<:raw", $file or die "$file: $!\n";
   my $digest = $digests{$platform} = Digest::SHA->new (384)->addfile ($fh)->hexdigest;
   close $fh;

   my $dir = $bundled{$platform} ? "prebuilt" : "wwwdist";
   my $name = "alien-tclsh9-$platform-$digest";

   File::Copy::copy ($file, "$tmp/$dir/$name") or die "copy $file: $!\n";
   print "$dir/$name\n";
}

# replace only our generated binaries, not unrelated files in these directories
for my $dir (qw(prebuilt wwwdist)) {
   File::Path::make_path ($dir);

   for my $file (glob "$dir/alien-tclsh9-*") {
      unlink $file or die "unlink $file: $!\n";
   }

   for my $file (glob "$tmp/$dir/*") {
      File::Copy::copy ($file, $dir) or die "copy $file: $!\n";
   }
}

open my $fh, ">", "binaries.pl" or die "binaries.pl: $!\n";
print $fh "# generated by fetch-binaries\n+{\n   version => \"$version\",\n   binaries => {\n";
printf $fh "      %-20s => \"%s\",\n", "\"$_\"", $digests{$_} for sort keys %digests;
print $fh "   },\n}\n";
close $fh or die "binaries.pl: $!\n";

open $fh, "<", "MANIFEST" or die "MANIFEST: $!\n";
my @manifest = grep !m{^prebuilt/alien-tclsh9-}, <$fh>;
close $fh;

open $fh, ">", "MANIFEST" or die "MANIFEST: $!\n";
print $fh @manifest, map "$_\n", sort glob "prebuilt/alien-tclsh9-*";
close $fh or die "MANIFEST: $!\n";
