#! /bin/sh
#
# helper -- Copyright (c) 2002 Eberhard Mattes
#
# This file is part of epocemx.
#
# epocemx 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 2, or (at your option)
# any later version.
#
# epocemx 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 epocemx; see the file COPYING.  If not, write to the
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

die()
{
  echo "$@"
  exit 2
}

test $# = 5 || die "Usage: helper libestlib.a emx.def estlib.o estlib.def missing.h"

emxobj -i -m0 "$1" >tmp.imp || exit 2

arm-epoc-pe-nm "$3" >tmp.nm || exit 2
awk '$2 == "T" { printf "%s DEFINED\n", $3 }' tmp.nm >tmp.defined || exit 2

echo EXPORTS >tmp.estlib.def || exit 2
awk '$2 == "DEFINED" { defined[$1] = 1 } \
      /^[0-9]+ / && defined[$2] { printf "  %-59s @ %s\n", $2, $1 } \
      /^[0-9]+ / && !defined[$2] { printf "  emx_%-55s @ %s\n", $2, $1 }' \
  tmp.defined tmp.imp >>tmp.estlib.def || exit 2

awk '/^[0-9]+ / { print $2 }' tmp.imp | sort >tmp.est || exit 2
awk '$2 == "@" { print $1 }' "$2" | sort >tmp.emx || exit 2
comm -23 tmp.est tmp.emx >tmp.missing || exit 2
awk '/^[0-9]+ / && NF == 2 { ord[$2] = $1 } \
     $2 == "DEFINED" { defined[$1] = 1 } \
     NF == 1 { if (!defined[$1]) \
                 printf "MISSING (emx_%s, %s)\n", $1, ord[$1] }' \
  tmp.imp tmp.defined tmp.missing >tmp.h || exit 2

mv tmp.estlib.def "$4" || exit 2
mv tmp.h "$5" || exit 2
rm tmp.imp tmp.nm tmp.defined tmp.emx tmp.est tmp.missing
