#!/bin/bash
PROGNAME=$1
i77 -c $PROGNAME > /dev/null 2>&1
gcc -g -O1 -I /usr/local/include/i2c -o $PROGNAME.o -Wa,-adhln=$PROGNAME.lst -Wno-discarded-qualifiers -c $PROGNAME.c > /dev/null 2>&1
cp $PROGNAME.lst $PROGNAME.dis
# Massage the GCC output to make it easier to extract data from the disassembly:
# I Instruction
# J Jump
# P Procedure
# S Source
# D Directive
# (there's a slight glitch after .align directives that's ignored in the C code)
ecce $PROGNAME.lst -command "(r12f-1/:/l0i/L|/(v/ /e)0t/:/f1/ /(v/ /e)0i/|/e5?m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/L/m,(v/ /r)0(v/ /\r)0i.|.r14i.|.ri.|.l0i.C|.m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/C/\m,t/|/3v/ /\l0ei/X/m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/X/\m,t/|/4v/./\l0ei/G/m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/G/\m,r0v-/:/l0ei/P/m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/X/s/J/m,v/L/s/S/m,v/G/s/D/m,v/C/s/I/m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/S/f1/:/s/|/m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/D/t/|/2u.|.2e2l0t.|.(v. .e)0m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/J/t/|/2u.|.2el0t.|.(v. .e)0m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/I/t/|/3e2m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/I/(t/|/e)2m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/I/t/|/(v/ /e)0m,m)0;%c" > /dev/null 2>&1
ecce $PROGNAME.lst -command "(v/P/t/|/2u.|.2el0t.|.(v. .e)0m,m)0;%c" > /dev/null 2>&1
# We could use tr -d below to remove control-k characters from imp77 listing file...
ecce $PROGNAME.lis -command "(f/"$'\v'"/e)0;%c" > /dev/null 2>&1

SCRIPT_PATH="$(readlink -f "$0")"
DIR_PATH="$(dirname "$SCRIPT_PATH")"
BINARY="$(basename "$SCRIPT_PATH" .sh)"

$DIR_PATH/$BINARY $PROGNAME > $PROGNAME.tmp

mv $PROGNAME.tmp $PROGNAME.lst
cat $PROGNAME.lst
