#!/usr/bin/perl

use Win32::TieRegistry( Delimiter=>"/" );
use Modern::Perl;

unless (@ARGV) {
	say "Usage: $0 path_to_add [another_path, ...]"
	exit
}

my $env =  $Registry->{"LMachine/SYSTEM/CurrentControlSet/Control/Session Manager/Environment"}
    or  die "Can't read HKLM environment: $^E\n",
			"You need to be in an elevated command prompt. Are you?\n";
	
my $path = $env->{"Path"}
        or  die "Can't read path: $^E\n";

my @parts = split /;/, $path;

print "Path: \n", join("\n", @parts), "\n";

foreach my $add_path (@ARGV) {
	if ( grep /$rx/, @parts ) {
		say "Already in path: $add_path";
	}
	else {
		my $rx = qr/^\Q$add_path\E$/i;
		push @parts, $add_path ;
	}
}

print "\n\nNew Path: \n", join("\n", @parts), "\n";

$path = join(";", @parts);

$env->{"Path"} = [ $path, "REG_EXPAND_SZ" ]
	or die "Error setting the path : $^E\n";

say "PATH = $path";

