1#!/bin/bash
2
3if [ $# -ne 3 ]; then
4    echo "Usage: $0 <name> <src dir> <obj dir>"
5    exit 1;
6fi
7
8ARM=$1
9SRCDIR=$2
10OBJDIR=$3
11
12APPLET=${ARM/%Arm/Applet}
13BINFILE=$OBJDIR/$ARM.bin
14OBJFILE=$OBJDIR/$ARM.obj
15CPPFILE=$SRCDIR/$ARM.cpp
16HFILE=$SRCDIR/$ARM.h
17SIZE=$(stat -c%s $BINFILE)
18
19if [ ! -f $BINFILE ]; then
20    echo "$BINFILE does not exist"
21    exit -1
22fi
23
24if [ ! -f $OBJFILE ]; then
25    echo "$OBJFILE does not exist"
26    exit -1
27fi
28
29# Generate the header file
30DEFINE=_$(echo $1 | tr '[:lower:]' '[:upper:]')_H
31cat > $HFILE << EOF
32// WARNING!!! DO NOT EDIT - FILE GENERATED BY APPLETGEN
33#ifndef $DEFINE
34#define $DEFINE
35
36#include <stdint.h>
37
38typedef struct
39{
40EOF
41nm -g $OBJFILE | awk '{print "    uint32_t "$3";"}' >> $HFILE
42cat >> $HFILE << EOF
43    uint8_t code[$SIZE];
44} $1;
45
46#endif // $DEFINE
47EOF
48
49# Generate the C file
50cat > $CPPFILE << EOF
51// WARNING!!! DO NOT EDIT - FILE GENERATED BY APPLETGEN
52#include "$ARM.h"
53#include "$APPLET.h"
54
55$ARM $APPLET::applet = {
56EOF
57nm -g $OBJFILE | awk '{print "// "$3"\n0x"$1","}' >> $CPPFILE
58cat >> $CPPFILE << EOF
59// code
60{
61EOF
62od -t x1 $BINFILE | awk '{for(n=2;n<=NF;n++){printf("0x"$n", ")}if(NF>1)print""}' >> $CPPFILE
63cat >> $CPPFILE << EOF
64}
65};
66EOF
67
68