#! /usr/bin/perl -w
if( defined(@ARGV) )
{
	foreach $arg (@ARGV) {
		($type, $file) = split(/=/, $arg);
		if ($type eq "BootImage") {
			$BootImageFile = $file;
		} elsif ($type eq "RescueLinux") {
			$RescueLinuxFile = $file;
		} elsif ($type eq "Logo") {
			$LogoFile = $file;
		} elsif ($type eq "Logo2") {
			$Logo2File = $file;
		} elsif ($type eq "Logo3") {
			$Logo3File = $file;
		}
	}




	if (defined $BootImageFile) {
		open BINARY, $BootImageFile or die $!;

		# in the for();
		# (map ord, split //, $bytes) will generate a list
		# $byte is stream data read from file
		# (split //, $byte) will separate $byte into char list (maybe)
		# map will apply ord (to number) to each one in char list
		# then each result after map will put in "$_"


		my $bytes;
		$current = 0;
		$size = -s BINARY;
		($size>>=2); # word number -1
		print ".data\n.globl array\n.globl array_end\narray:\n.word \\\n";
		while ($current < $size) {
			read(BINARY, $bytes, 4);
			@a = map ord, split //, $bytes;
			printf "0x%02x%02x%02x%02x", @a;

			$current++;
			if($current < $size)
				{ print ","}
			else
				{ last }

			if($current%4==0)
				{ print "\\\n";}
		}
		print "\narray_end:\n.word 0\n";
	}
    
	if (defined $RescueLinuxFile) {
		open BINARY, $RescueLinuxFile or die $!;
		$current = 0;
		$size = -s BINARY;
		$size--;
		($size>>=2); # word number 
		$size++;
		print ".globl linux\n.globl linux_end\nlinux:\n.word \\\n";
		while ($current < $size) {
			read(BINARY, $bytes, 1);
			@a = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@b = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@c = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@d = map ord, split //, $bytes;

			printf "0x%02x", @d;
			printf "%02x", @c;
			printf "%02x", @b;
			printf "%02x", @a;


			$current++;
			if($current < $size)
				{ print ","}
			else
				{ last }

			if($current%4==0)
				{ print "\\\n";}
		}
		print "\nlinux_end:\n.word 0\n";
	} else {
		print ".data\n.globl linux\n.globl linux_end\nlinux:\n.word \\\n";
		print "\nlinux_end:\n.word 0\n";
	}
    
	if (defined $LogoFile) {
		open BINARY, $LogoFile or die $!;
		$current = 0;
		$size = -s BINARY;
		$size--;
		($size>>=2); # word number 
		$size++;
		print ".globl logo\n.globl logo_end\nlogo:\n.word \\\n";
		while ($current < $size) {
			read(BINARY, $bytes, 1);
			@a = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@b = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@c = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@d = map ord, split //, $bytes;

			printf "0x%02x", @d;
			printf "%02x", @c;
			printf "%02x", @b;
			printf "%02x", @a;


			$current++;
			if($current < $size)
				{ print ","}
			else
				{ last }

			if($current%4==0)
				{ print "\\\n";}
		}

		print "\nlogo_end:\n.word 0\n";
	} else {
		print ".data\n.globl logo\n.globl logo_end\nlogo:\n.word \\\n";
		print "\nlogo_end:\n.word 0\n";
	}

	if (defined $Logo2File) {
		open BINARY, $Logo2File or die $!;
		$current = 0;
		$size = -s BINARY;
		$size--;
		($size>>=2); # word number 
		$size++;
		print ".globl logo2\n.globl logo2_end\nlogo2:\n.word \\\n";
		while ($current < $size) {
			read(BINARY, $bytes, 1);
			@a = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@b = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@c = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@d = map ord, split //, $bytes;

			printf "0x%02x", @d;
			printf "%02x", @c;
			printf "%02x", @b;
			printf "%02x", @a;


			$current++;
			if($current < $size)
				{ print ","}
			else
				{ last }

			if($current%4==0)
				{ print "\\\n";}
		}

		print "\nlogo2_end:\n.word 0\n";
	} else {
		print ".data\n.globl logo2\n.globl logo2_end\nlogo2:\n.word \\\n";
		print "\nlogo2_end:\n.word 0\n";
	}


	if (defined $Logo3File) {
		open BINARY, $Logo3File or die $!;
		$current = 0;
		$size = -s BINARY;
		$size--;
		($size>>=2); # word number 
		$size++;
		print ".globl logo3\n.globl logo3_end\nlogo3:\n.word \\\n";
		while ($current < $size) {
			read(BINARY, $bytes, 1);
			@a = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@b = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@c = map ord, split //, $bytes;
			read(BINARY, $bytes, 1);
			@d = map ord, split //, $bytes;

			printf "0x%02x", @d;
			printf "%02x", @c;
			printf "%02x", @b;
			printf "%02x", @a;


			$current++;
			if($current < $size)
				{ print ","}
			else
				{ last }

			if($current%4==0)
				{ print "\\\n";}
		}

		print "\nlogo3_end:\n.word 0\n";
	} else {
		print ".data\n.globl logo3\n.globl logo3_end\nlogo3:\n.word \\\n";
		print "\nlogo3_end:\n.word 0\n";
	}
}
else
{
    print "binary image file need!! \n";
}
