###################################################
# See layout.txt for explanation of what this does.
###################################################

open(hLAYOUT, $ARGV[0]) || die "Can't open $ARGV[0]";
open(hPLACEFIL, ">placefil.txt") || die "Can't open placefil.txt";
open(hMAKE, ">layout.inc") || die "Can't open layout.inc";

open(hPOSTMAKE, ">sdkmake.inc") || die "Can't open sdkmake.inc";

print(hPLACEFIL "# --------- DO NOT EDIT THIS FILE ------------ DO NOT EDIT THIS FILE ---------\n");
print(hPLACEFIL "# --------- DO NOT EDIT --- see layout.txt --- DO NOT EDIT -------------------\n");
print(hPLACEFIL "# --------- DO NOT EDIT THIS FILE ------------ DO NOT EDIT THIS FILE ---------\n");

print(hMAKE "# --------- DO NOT EDIT THIS FILE ------------ DO NOT EDIT THIS FILE ---------\n");
print(hMAKE "# --------- DO NOT EDIT --- see layout.txt --- DO NOT EDIT -------------------\n");
print(hMAKE "# --------- DO NOT EDIT THIS FILE ------------ DO NOT EDIT THIS FILE ---------\n");

print(hLAYOUT "# --------- DO NOT EDIT THIS FILE ------------ DO NOT EDIT THIS FILE ---------\n");
print(hLAYOUT "# --------- DO NOT EDIT --- see layout.txt --- DO NOT EDIT -------------------\n");
print(hLAYOUT "# --------- DO NOT EDIT THIS FILE ------------ DO NOT EDIT THIS FILE ---------\n");

while($_ = <hLAYOUT>) {

	if (/^[ \t]*REM/i) {
		s/-+//g;
		print $_;
	}
	elsif (/^[ \t]*$/) {
	}
	else {
		$DUPEFILE="";
		if (/^[0-9]/)
		{
			$DUPEFILE=substr($_,0,1);
			print "Creating special rules for same-name file ($DUPEFILE) in multiple locations.\n";
			s/^.//;
		}
		$PLACEFILE=$_;
		$SOURCE=$_;
		$PLACEFILE =~ s/.*[ \t]+//;
		$PLACEFILE =~ s/\\$//;
		$PLACEFILE =~ s/\n//;
		$PLACEFILE =~ s/^.$//;
		$_ = $PLACEFILE;
		if (!(/^retail$/i)) {

			if (/^$/) {
				$PLACEFILE =~ s/^/sdk/;
			}
			else {
				$PLACEFILE =~ s/^/sdk\\/ ;
			}
		}

		$SOURCE =~ s/[ \t]+.*\n//;

		# replace environment variable syntax with makefile. %var% -> $(var)
		$SOURCEMAKE = $SOURCEENV = $SOURCE;
		$SOURCEMAKE =~ s/%([_A-Z-]+)%/\$($1)/gi;		

		#print SOURCE."\=".$SOURCE."\n";
		#print PLACEFILE."\=".$PLACEFILE."\n";
		#print "\n";


		print "Source: $SOURCEENV\n";
		open(FILELIST, "dir /on /b $SOURCEENV|");	

		$ONCE = 0;
		while (($FILENAME = <FILELIST>) || !$ONCE) {
			if (! $ONCE)
			{
				$ONCE = 1;
				#
				# We need to handle lines in layout.txt that have wildcard characters in them. e.g. ActionTe.*, Error.*
				# In order to do that, we need to extract the dir for the files. This needs to be done only once for each
				# line because the dir will be the same for all the files that match the wildcard character. Then all we
				# need to do during each iteration of the loop is tag on the name of the file to the end of the dir.
				#
				$SOURCEDIR = $SOURCEMAKE;
				$SOURCEDIR =~ s/[^\\]+$//;
			}

			$_ = $FILENAME =~ s/\n$//;
			if (/^$/)
			{
				print "File has not been built yet.\n";
				$FILENAME = $SOURCEMAKE;
				$FILENAME =~ s/.+\\//g;
			}

			#
			# Append the filename to the dir to get the fullpath to the file.
			#
			$SOURCEMAKE = $SOURCEDIR.$FILENAME;
			$POSTMAKE=0;
			$_ = $SOURCEMAKE;
		
			if (/_NTTREE/i | /_NTPOSTBLD/i) {
				$SOURCEMAKE =~ s/_NTTREE/_NTPOSTBLD/i;
				$POSTDEPENDENCY ="$POSTDEPENDENCY $PLACEFILE\\$FILENAME$DUPEFILE";
				print(hPOSTMAKE "$PLACEFILE\\$FILENAME$DUPEFILE : \n\tif not exist $PLACEFILE md $PLACEFILE\n\tcopy $SOURCEMAKE \$@\n");
				$POSTMAKE=1;
			}
			else {
				$DEPENDENCY ="$DEPENDENCY \$(O)\\$FILENAME$DUPEFILE";
				print(hMAKE "\$(O)\\$FILENAME$DUPEFILE : $SOURCEMAKE\n\tcopy $SOURCEMAKE \$@\n\t\$(BINPLACE_CMD)\n");
				print(hPLACEFIL "$FILENAME$DUPEFILE      $DUPEFILE$PLACEFILE\n");
			}
			if ($DUPEFILE) {
				print(hPOSTMAKE "$PLACEFILE\\$FILENAME : $DUPEFILE$PLACEFILE\\$FILENAME$DUPEFILE\n\tif not exist $PLACEFILE md $PLACEFILE\n\tcopy $DUPEFILE$PLACEFILE\\$FILENAME$DUPEFILE \$@\n");
				$POSTDEPENDENCY = "$POSTDEPENDENCY $PLACEFILE\\$FILENAME"
			}
			print $FILENAME."\n";
	        }
		close FILELIST;
	}
}

print(hPOSTMAKE "sdklayout : $POSTDEPENDENCY\n");
print(hMAKE "layout : $DEPENDENCY\n");

close hMAKE;
close hPOSTMAKE;
close hLAYOUT;