#!/usr/bin/perl -w
use v5.14;
no if $] > 5.017011, warnings => 'experimental::smartmatch';

use File::Copy qw/copy/;
use File::Basename qw/fileparse/;

my ($format, $name) = @ARGV;
my $basename = fileparse $name, qr/\..*/;

given ($format){
	exec 'gcc', qw/-DONLINE_JUDGE -std=gnu11 -Wall -Wextra -lm -O2 -o/, $basename, $name when 'C';
	exec 'g++', qw/-DONLINE_JUDGE -std=gnu++11 -Wall -Wextra -lm -O2 -o/, $basename, $name when 'CPP';
	when ('MONO') {
		system 'gmcs', '-d:ONLINE_JUDGE', $name and die "gmcs failed: errno=$! return=$?";
		rename "$basename.exe", $basename;
		chmod 0755, $basename;
	}
	exec 'gmcs', '-d:ONLINE_JUDGE', $name when  'MONO';
	when ('JAVA') {
		unlink $_ for <*.class>;
		system 'javac', $name and die "javac failed: errno=$! return=$?";
		system 'jar', 'cfe', $basename, $basename, <*.class> and die "jar failed: errno=$! return=$?";
		chmod 0755, $basename;
		exit
	}
	exec 'fpc', qw/-dONLINE_JUDGE -O2/, $name when 'PASCAL';
	exec 'go', qw/build -compiler gc/, $name when 'GOLANG';
	exec 'go', qw/build -compiler gccgo/, $name, when 'GCCGO';
	exec 'ghc', qw/-DONLINE_JUDGE -Wall -O2 -o/, $basename, $name when 'HASKELL';

	when ([qw/GOLFSCRIPT PERL PYTHON RUBY SBCL/]){
		open IN, '<', $name;
		open OUT, '>', $basename;
		print OUT "#!/usr/bin/golfscript\n"    if $_ eq 'GOLFSCRIPT';
		print OUT "#!/usr/bin/perl\n"          if $_ eq 'PERL';
		print OUT "#!/usr/bin/python\n"        if $_ eq 'PYTHON';
		print OUT "#!/usr/bin/ruby\n"          if $_ eq 'RUBY';
		print OUT "#!/usr/bin/sbcl --script\n" if $_ eq 'SBCL';
		print OUT while <IN>;
		close OUT;
		close IN;
		chmod 0755, $basename;
		exit
	}
}

exit 1;
__END__

=encoding utf-8

=head1 NAME

gruntmaster-compile - Gruntmaster 6000 compiler frontend

=head1 SYNOPSIS

  gruntmaster-compile CPP file.cpp
  gruntmaster-compile JAVA file.java

=head1 DESCRIPTION

gruntmaster-compile is a very simple frontend to various comilers. It takes two arguments: the file format and the file name, and produces a compiled executable. The executable's name is the basename of the input file.

Compile commands for each format:

=over

=item C

  gcc -DONLINE_JUDGE -std=gnu11 -Wall -Wextra -O2 -o $output $input

=item CPP

  g++ -DONLINE_JUDGE -std=gnu++11 -fabi-version=6 -Wall -Wextra -O2 -o $output $input

=item MONO

  gmcs -d:ONLINE_JUDGE $input

=item JAVA

  javac $input

=item PASCAL

  fpc -dONLINE_JUDGE -O2 $input

=item GOLANG

  go build -compiler gc $input

=item GCCGO

  go build -compiler gccgo $input

=item HASKELL

  ghc -DONLINE_JUDGE -Wall -O2 -o $output $input

=item PERL

  cp $input $output

=item PYTHON

  cp $input $output

=back

=head1 AUTHOR

Marius Gavrilescu E<lt>marius@ieval.roE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2014 by Marius Gavrilescu

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.


=cut
