#/bin/sh
#
# $Id: fwbin,v 1.3 2009/06/07 22:27:50 mjacob Exp $
#
# Copyright (c) 1997-2008 by Matthew Jacob
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# Alternatively, this software may be distributed under the terms of the
# the GNU Public License ("GPL") with platforms where the prevalant license
# is the GNU Public License:
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the Version 2 GNU General Public License as
#  published by the Free Software Foundation
#
#  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 program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# Matthew Jacob
# Feral Software
# 421 Laurel Avenue
# Menlo Park, CA 94025
# USA
#
# gplbsd at feral com
#
dowr()
{
 echo -n $* >> /tmp/x$$.c
 echo "" >> /tmp/x$$.c
}

if [ $# -ne 1 ]; then
 echo "usage: $0 fwfile"
 exit 1
fi
name=`grep 'static const' $1 | head -1 | awk ' { print $4 }' | sed 's/..$//'`
stok=`grep 'static const' $1 | head -1 | awk ' { print $3 }'`

cp /dev/null /tmp/x$$.c
dowr '#include <unistd.h>'
dowr '#include <stdlib.h>'
dowr '#include <stdio.h>'
dowr '#include <stdint.h>'
dowr '#include <memory.h>'
dowr '#include <fcntl.h>'
dowr '#include <errno.h>'
dowr '#define ISP_2400 1'
dowr '#define ISP_2500 1'
cat $1 >> /tmp/x$$.c
dowr '	int main(void)'
dowr '{'
dowr '	size_t i, amt = (sizeof ' $name ') / sizeof (' $name '[0]);'
dowr '	for (i = 0; i != amt; i++) {'
case $stok in
 uint16_t)
	dowr '		putchar('$name '[i] >>  8);'
	dowr '		putchar('$name '[i] >>  0);'
	;;
 uint32_t)
	dowr '		putchar('$name '[i] >> 24);'
	dowr '		putchar('$name '[i] >> 16);'
	dowr '		putchar('$name '[i] >>  8);'
	dowr '		putchar('$name '[i] >>  0);'
	;;
esac
dowr '	}'
dowr '	return (0);'
dowr '}'
cc -g -o /tmp/x$$ /tmp/x$$.c || exit 1
/tmp/x$$ > `echo $1|sed 's/.[ch]//'`.bin
rm -f /tmp/x$$*
echo Result in `echo $1|sed 's/.[ch]//'`.bin
