#!/usr/bin/perl -w

use File::Basename;

my $wine = $ENV{WINE} // "wine";
my $fxc = $ENV{FXC} // "fxc.exe";

sub translate_path($)
{
    my $in = shift;

    open PATH, "-|", $wine, "winepath", "-w", $in or die "Failed to translate path";
    chomp(my $out = <PATH>);
    close PATH;

    return $out;
}

my $outfile = translate_path($ARGV[0]);
my $infile = translate_path($ARGV[1]);
my $name = fileparse($ARGV[1], qr/\.[^.]*/);
my $type = $name;
$type =~ s/_[0-9]+$//;
$type = lc(substr($type, -2));

die("Invalid shader type \"$type\"\n") if ($type !~ /[cdghpv]s/);
system($wine,
        $fxc,
        "/Emain",
        "/Vng_p${name}",
        "/T${type}_5_0",
        "/Fh${outfile}",
        "/nologo",
        "${infile}");
system("dos2unix", "-q", $outfile) if (!$?);

exit($? >> 8);
