#!/bin/bash
if [ $# == 0 ] ; then
    echo "syntax: `basename $0` [--i686|--arm7hf|--aarch64|--x86_64|--riscv64|--mips|--powerpc|--sparc64|s390x|m68k] [-O|-O0|-O1|-O2|-O3|-Os|-Og] impfile"
    exit 1
fi

# currently output for all these is empty files: aarch64 arm7hf i686 m68k mips powerpc riscv64 s390x sparc64 x86_64
if [ "$1" == "--i686" ] ; then
    TARGET_ARCH="i686"
    shift
elif [ "$1" == "--arm7hf" ] ; then
    TARGET_ARCH="arm7hf"
    shift
elif [ "$1" == "--aarch64" ] ; then
    TARGET_ARCH="aarch64"
    shift
elif [ "$1" == "--x86_64" ] ; then
    TARGET_ARCH="x86_64"
    shift
elif [ "$1" == "--riscv64" ] ; then
    TARGET_ARCH="riscv64"
    shift
elif [ "$1" == "--mips" ] ; then
    TARGET_ARCH="mips"
    shift
elif [ "$1" == "--powerpc" ] ; then
    TARGET_ARCH="powerpc"
    shift
elif [ "$1" == "--sparc64" ] ; then
    TARGET_ARCH="sparc64"
    shift
elif [ "$1" == "--s390x" ] ; then
    TARGET_ARCH="s390x"
    shift
elif [ "$1" == "--m68k" ] ; then
    TARGET_ARCH="m68k"
    shift
else
    TARGET_ARCH="`arch`"
fi

if [ "$1" == "-O" ] ; then
    OPT="-O"
    shift
elif [ "$1" == "-O0" ] ; then
    OPT="-O0"
    shift
elif [ "$1" == "-O1" ] ; then
    OPT="-O1"
    shift
elif [ "$1" == "-O2" ] ; then
    OPT="-O2"
    shift
elif [ "$1" == "-O3" ] ; then
    OPT="-O3"
    shift
elif [ "$1" == "-Os" ] ; then
    OPT="-Os"
    shift
elif [ "$1" == "-Og" ] ; then
    OPT="-Og"
    shift
else
    OPT="-O0"
fi

OPT="-fno-toplevel-reorder $OPT"

HOST_ARCH="`arch`"

if [ "$1" == "" ] ; then
    echo "syntax: `basename $0` [--i686|--arm7hf|--aarch64|--x86_64] [-O|-O0|-O1|-O2|-O3|-Os|-Og] impfile"
    exit 1
else
    PROGNAME="$1"
fi

if [ "$TARGET_ARCH" == "i686" ] ; then

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then
        #i77 -c $PROGNAME > /dev/null 2>&1
        /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif [ "HOST_ARCH" == "x86_64" ] ; then
        #i77 -m32 -c $PROGNAME > /dev/null 2>&1
        /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1
        gcc -m32 -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v i686-linux-gnu-gcc &> /dev/null; then
        /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1
        i686-linux-gnu-gcc -std=gnu99 -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: i686-linux-gnu-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-i686-linux-gnu"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-i686-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S i686-linux-gnu-gcc"
        echo ""
        exit 1
    fi
    
elif [ "$TARGET_ARCH" == "arm7hf" ] ; then

    #i77 -c $PROGNAME > /dev/null 2>&1
    /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c #2>&1

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then 
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v arm-linux-gnueabihf-gcc &> /dev/null; then
        arm-linux-gnueabihf-gcc -std=gnu99  -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: arm-linux-gnueabihf-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-arm-linux-gnueabihf"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-arm-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S arm-linux-gnueabihf-gcc"
        echo ""
        exit 1
    fi

elif [ "$TARGET_ARCH" == "x86_64" ] ; then

    #i77 -c $PROGNAME > /dev/null 2>&1
    /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v x86_64-linux-gnu-gcc &> /dev/null; then
        x86_64-linux-gnu-gcc -std=gnu99  -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: x86_64-linux-gnu-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-x86-64-linux-gnu"
        echo        "(Note the hyphen instead of an underscore in the package name)"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-x86_64-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S x86_64-linux-gnu-gcc"
        echo ""
        exit 1
    fi

elif [ "$TARGET_ARCH" == "aarch64" ] ; then
        
    #i77 -c $PROGNAME > /dev/null 2>&1
    /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v aarch64-linux-gnu-gcc &> /dev/null; then
        aarch64-linux-gnu-gcc -std=gnu99 -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: aarch64-linux-gnu-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-aarch64-linux-gnu"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-aarch64-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S aarch64-linux-gnu-gcc"
        echo ""
        exit 1
    fi

elif [ "$TARGET_ARCH" == "riscv64" ] ; then
    #i77 -c $PROGNAME > /dev/null 2>&1
    /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v riscv64-linux-gnu-gcc &> /dev/null; then
        riscv64-linux-gnu-gcc -std=gnu99 -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: riscv64-linux-gnu-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-riscv64-linux-gnu"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-riscv64-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S riscv64-linux-gnu-gcc"
        echo ""
        exit 1
    fi

elif [ "$TARGET_ARCH" == "mips" ] ; then
    #i77 -c $PROGNAME > /dev/null 2>&1
    /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v mips-linux-gnu-gcc &> /dev/null; then
        mips-linux-gnu-gcc -std=gnu99 -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: mips-linux-gnu-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-mips-linux-gnu"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-mips-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S mips-linux-gnu-gcc"
        echo ""
        exit 1
    fi

elif [ "$TARGET_ARCH" == "powerpc" ] ; then
    #i77 -c $PROGNAME > /dev/null 2>&1
    /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v powerpc-linux-gnu-gcc &> /dev/null; then
        powerpc-linux-gnu-gcc -std=gnu99 -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: powerpc-linux-gnu-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-powerpc-linux-gnu"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-powerpc-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S powerpc-linux-gnu-gcc"
        echo ""
        exit 1
    fi

elif [ "$TARGET_ARCH" == "sparc64" ] ; then
    #i77 -c $PROGNAME > /dev/null 2>&1
    /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v sparc64-linux-gnu-gcc &> /dev/null; then
        sparc64-linux-gnu-gcc -std=gnu99 -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: sparc64-linux-gnu-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-sparc64-linux-gnu"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-sparc64-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S sparc64-linux-gnu-gcc"
        echo ""
        exit 1
    fi

elif [ "$TARGET_ARCH" == "s390x" ] ; then
    #i77 -c $PROGNAME > /dev/null 2>&1
    /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v s390x-linux-gnu-gcc &> /dev/null; then
        s390x-linux-gnu-gcc -std=gnu99 -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: s390x-linux-gnu-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-s390x-linux-gnu"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-s390x-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S s390x-linux-gnu-gcc"
        echo ""
        exit 1
    fi

elif [ "$TARGET_ARCH" == "m68k" ] ; then
    #i77 -c $PROGNAME > /dev/null 2>&1
    /home/gtoal/src/i2c/icode2ast/icode2ast $PROGNAME.imp > $PROGNAME.c 2>&1

    if [ "HOST_ARCH" == "$TARGET_ARCH" ] ; then
        gcc -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    elif command -v m68k-linux-gnu-gcc &> /dev/null; then
        m68k-linux-gnu-gcc -std=gnu99 -g $OPT -c -o $PROGNAME.o -Wa,-adhln -I /home/gtoal/src/i2c/perms $PROGNAME.c > $PROGNAME.lst #2> /dev/null # -L /home/gtoal/src/i2c/perms -lperms -lm -lbacktrace
    else
        echo "Error: m68k-linux-gnu-gcc could not be found."
        echo ""
        echo "To install it, use the command for your Linux distribution:"
        echo ""
        echo "  Ubuntu / Debian / Mint:"
        echo "    sudo apt update ; sudo apt install gcc-m68k-linux-gnu"
        echo ""
        echo "  Fedora / RHEL / CentOS:"
        echo "    sudo dnf install gcc-m68k-linux-gnu"
        echo ""
        echo "  Arch Linux / Manjaro:"
        echo "    sudo pacman -S m68k-linux-gnu-gcc"
        echo ""
        exit 1
    fi

fi

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)
ecce7 $PROGNAME.lst -command "(r4\m, pv/              /\m, r14pv/ /em, m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(r12f-1/:/l0i/L|/(v/ /e)0t/:/f1/ /(v/ /e)0i/|/e5?m,m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(v/L/m,(v/ /r)0(v/ /\r)0i.|.r14i.|.ri.|.l0i.C|.m,m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(v/C/\m,t/|/3v/ /\l0ei/X/m,m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(v/X/\m,t/|/4v/./\l0ei/G/m,m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(v/G/\m,r0v-/:/l0ei/P/m,m)0;%c" > /dev/null 2>&1
ecce7 $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
ecce7 $PROGNAME.lst -command "(v/S/f1/:/s/|/m,m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(v/D/t/|/2u.|.2e2l0t.|.(v. .e)0m,m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(v/J/t/|/2u.|.2el0t.|.(v. .e)0m,m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(v/I/t/|/3e2m,m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(v/I/(t/|/e)2m,m)0;%c" > /dev/null 2>&1
ecce7 $PROGNAME.lst -command "(v/I/t/|/(v/ /e)0m,m)0;%c" > /dev/null 2>&1
ecce7 $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...
ecce7 $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)"

if [ ! -f $DIR_PATH/$BINARY ] ; then
    if [ ! -f $DIR_PATH/$BINARY.c ] ; then
        echo "We are missing $DIR_PATH/$BINARY - where is $DIR_PATH/$BINARY.c ?"
        exit 1
    fi
    gcc -g -O -o $DIR_PATH/$BINARY $DIR_PATH/$BINARY.c
fi

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

ecce7 $PROGNAME.tmp $PROGNAME.lst <<EOF > /dev/null 2>&1
(v/						/s/		 		/m,m)0
m-0(r0 (v-/	/e-,v-/ /e-)0  l0 rm,j)0
%c
EOF
cat $PROGNAME.lst
