#!/bin/sh

rm -f all.h
rm -f getmapper.cpp

#========================================
# make Makefile
cat > hoge.tmp <<EOF
# this file is generated by makemisc
OBJS = getmapper.o
EOF
for f in ???.h; do echo "OBJS += $(basename $f .h).o" >> hoge.tmp; done
cat >> hoge.tmp <<EOF
SUBDIRS = 

include \$(NESTER_BASE)/Makefile.prefab


EOF
for f in ???.h; do echo "$(basename $f .h).o: $f ../../types.h ../nes.h ../nes_mapper.h" >> hoge.tmp; done
cat >> hoge.tmp <<EOF
getmapper.o: ../nes_mapper.h nes_mapper_all.h

EOF
mv hoge.tmp Makefile


#========================================
# make nes_mapper_decl.h
cat > hoge.tmp <<EOF
/* this file is generated by makemisc */
#ifndef __NES_MAPPER_DECL_H
#define __NES_MAPPER_DECL_H

EOF
grep '^class' ???.h | ruby -ne 'print "#{$1};\n" if /...\.h:(.+?) :/ =~ $_' >> hoge.tmp

cat >> hoge.tmp <<EOF

#endif
EOF
mv hoge.tmp nes_mapper_decl.h


#========================================
# make nes_mapper_friend_decl.h
ruby -ne 'print "friend #{$_}" if /^class/ =~ $_' < nes_mapper_decl.h > nes_mapper_friend_decl.h

#========================================
# make nes_mapper_all.h
echo '/* this file is generated by makemisc */' > hoge.tmp
for f in ???.h; do echo "#include \"$f\"" >> hoge.tmp; done
mv hoge.tmp nes_mapper_all.h


#========================================
# make getmapper.cpp
ruby make_getmapper.rb nes_mapper_decl.h > getmapper.cpp
