#!/usr/bin/perl
#################################################################################
#										#
# Build script for native AVR programs						#
# copyright (c) 2009 Joerg Wolfram (joerg@jcwolfram.de)				#
#										#
#										#
# This program is free software; you can redistribute it and/or			#
# modify it under the terms of the GNU General Public License			#
# as published by the Free Software Foundation; either version 3		#
# of the License, or (at your option) any later version.			#
#										#
# This program is distributed in the hope that it will be useful,		#
# but WITHOUT ANY WARRANTY; without even the implied warranty of		#
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU		#
# General Public License for more details.					#
#										#
# You should have received a copy of the GNU General Public			#
# License along with this library; if not, write to the				#
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,			#
# Boston, MA 02111-1307, USA.							#
#										#
#################################################################################
$infile=$ARGV[0];
$outfile="CB2_".$ARGV[1]."_CH8";
$pname=$ARGV[1];

open(HFILE,">$outfile");
	print HFILE $pname;
close(HFILE);

$dlen=(-s $outfile);
print $dlen;
$zlen=15-$dlen;
$cmd="dd if=/dev/zero bs=1 count=".$zlen.">>$outfile";
$dummy=`$cmd`;

print $outfile."\n";

open(HFILE,">>$outfile");
#add speed byte
$speed=$ARGV[3]+0;

	print HFILE chr($speed);

#add keymap
if ($ARGV[2]="key1")
{
	print HFILE "\x30\x31\x32\x33\x34\x35\x36\xe2";
	print HFILE "\xe3\x39\xc3/*-+\xea";
}

elsif ($ARGV[2]="lrud")
{
	print HFILE "\x30\x31\xe4\x33\xe2\x35\xe3\x37";
	print HFILE "\xe5\x39\xc3/*-+\xea";
}

#default
else
{
	print HFILE "\x30\x31\x32\x33\x34\x35\x36\x37";
	print HFILE "\x38\x39\xc3/*-+\xea";
}

close(HFILE);



$dlen=(-s $outfile);
print $dlen;
$zlen=512-$dlen;
$cmd="dd if=/dev/zero bs=1 count=".$zlen.">>$outfile";
$dummy=`$cmd`;
print $dummy;

$dummy=`cat $infile >> $outfile`;

$dlen=(-s $outfile);
print $dlen;
$zlen=4096-$dlen;
$cmd="dd if=/dev/zero bs=1 count=".$zlen.">>$outfile";
$dummy=`$cmd`;

