#!/usr/bin/perl 

use strict;
use warnings;
use File::Find;

die unless -d $ARGV[0];

sub wanted {
	my $file = $_;
	if ($file eq 'Makefile') {
		open(FH, $file) || die "Failed to open $file";
		open(FH2, ">$file.new") || die "Failed to open $file";
		while(<FH>) {
			s/avr-gcc\.exe/avr-gcc/g;
			s/avr-size -C --mcu=\S+/avr-size/g;
			print FH2 $_;
		};
		close(FH);
		close(FH2);
		rename("$file.new", $file);
	} elsif ($file =~ /\.[hc]$/) {
		open(FH, $file) || die "Failed to open $file";
		open(FH2, ">$file.new") || die "Failed to open $file";
		while(<FH>) {
			tr#\\#/# if ($_ =~ /#include /);
			tr#\\#/# if ($_ =~ /#define\s+LUN_.*?_INCLUDE/);
			print FH2 $_;
		};
		close(FH);
		close(FH2);
		rename("$file.new", $file);
	};
};

find(\&wanted, @ARGV);

