all: hellow-c
	./hellow-c
	@echo All up to date.

remove-cr: ecce
	@echo '#!/bin/sh'                                >  remove-cr
	@echo 'if [ X"`which dos2unix`" != X"" ] ; then' >> remove-cr
	@echo '    dos2unix -q $$1'                      >> remove-cr
	@echo 'else'                                     >> remove-cr
	@echo '    ./ecce $$1 -command "bj-;%c" > /dev/null 2> /dev/null' >> remove-cr
	@echo 'fi'                                       >> remove-cr
	@chmod +x remove-cr

getfile:
	@echo '#!/bin/sh'                              >  getfile
	@echo 'if   [ X"`which wget`" != X"" ] ; then' >> getfile
	@echo '    wget -q $$1 -O `basename $$1`'      >> getfile
	@echo 'elif [ X"`which curl`" != X"" ] ; then' >> getfile
	@echo '    curl -s -S -L -O $$1'               >> getfile
	@echo 'elif [ X"`which lynx`" != X"" ] ; then' >> getfile
	@echo '    lynx -dump $$1 > `basename $$1`' >> getfile
	@echo 'else'                                   >> getfile
	@echo '    echo "You really need to install a program to fetch files from the web."' >> getfile
	@echo '    echo "Try one of:"'                 >> getfile
	@echo '    echo "  sudo apt-get install wget"' >> getfile
	@echo '    echo "  sudo apt-get install curl"' >> getfile
	@echo '    echo "  sudo apt-get install lynx"' >> getfile
	@echo '    exit 1' >> getfile
	@echo 'fi' >> getfile
	@chmod +x getfile
	./getfile http://www.gtoal.com/imp77-in-C/Release0.1/README.txt

hellow-c: pass1-c pass2-c pass3elf stdperm.imp imp2021-c hellow.imp libimp77.a # ld.i77.script
	@echo "***************************************************************************************************"
	@echo "* If Imp programs don't build, you may need to install the 32-bit libraries, with something like: *"
	@echo "*                                                                                                 *"
	@echo "*    sudo apt-get install gcc-multilib                                                            *"
	@echo "*                                                                                                 *"
	@echo "* See the README.txt for more details.                                                            *"
	@echo "***************************************************************************************************"
	./imp2021-c hellow.imp
	@rm -f i77.lex.debug i77.par.debug *.ibj *.icd *.lis *~

hellow.imp:
	@echo '%begin' > hellow.imp
	@echo '  printstring("You should now be able to use imp2021-c to compile the imp2021 suite."); newline' >> hellow.imp
	@echo '%endofprogram' >> hellow.imp

ld.i77.script:
	@echo 'SECTIONS'                                          >  ld.i77.script
	@echo '  {'                                               >> ld.i77.script
	@echo '  . = ALIGN(32);'                                  >> ld.i77.script
	@echo '  ITRAP : { *(ITRAP$$B) *(ITRAP$$D) *(ITRAP$$F) }' >> ld.i77.script
	@echo '  }'                                               >> ld.i77.script
	@echo 'INSERT BEFORE .data'                               >> ld.i77.script

imp2021-c:
	@echo '#!/bin/sh' > imp2021-c
	@echo 'DOLINK="yes"' >> imp2021-c
	@echo 'if [ X"`which arch`" = X"" ] ; then' >> imp2021-c
	@echo '    ARCH="`uname -m`"' >> imp2021-c
	@echo '    if [ X"$$ARCH" = X"" ] ; then' >> imp2021-c
	@echo '        ARCH="unknown"' >> imp2021-c
	@echo '    fi' >> imp2021-c
	@echo 'else' >> imp2021-c
	@echo '    ARCH="`arch`"' >> imp2021-c
	@echo 'fi' >> imp2021-c
	@echo 'if [ X"$$1" = X"-c" ] ; then' >> imp2021-c
	@echo '    DOLINK="no"' >> imp2021-c
	@echo '    shift' >> imp2021-c
	@echo 'fi' >> imp2021-c
	@echo 'if [ X"$$1" = X"-h" ] ; then' >> imp2021-c
	@echo '    echo "syntax: imp2021-c {-c} file.imp"' >> imp2021-c
	@echo '    exit 0' >> imp2021-c
	@echo 'fi' >> imp2021-c
	@echo 'if [ X"$$1" = X"" ] ; then' >> imp2021-c
	@echo '    echo "syntax: imp2021-c {-c} file.imp"' >> imp2021-c
	@echo '    exit 0' >> imp2021-c
	@echo 'fi' >> imp2021-c
	@echo 'if [ ! -f "$$1" ] ; then' >> imp2021-c
	@echo '    echo "imp2021-c: file $$1 does not exist' >> imp2021-c
	@echo '"' >> imp2021-c
	@echo '    exit 1' >> imp2021-c
	@echo 'fi' >> imp2021-c
	@echo 'if [ X"$$1" != X"`basename $$1 .imp`.imp" ] ; then' >> imp2021-c
	@echo '    echo "imp2021-c: $$1 is not a .imp file' >> imp2021-c
	@echo '"' >> imp2021-c
	@echo '    exit 1' >> imp2021-c
	@echo 'fi' >> imp2021-c
	@echo 'if [ X"$$2" != X"" ] ; then' >> imp2021-c
	@echo '    echo "imp2021-c: extra parameter $$2?"' >> imp2021-c
	@echo '    exit 1' >> imp2021-c
	@echo 'fi' >> imp2021-c
	@echo './pass1-c $$1,stdperm.imp `basename $$1 .imp`-c.icd,`basename $$1 .imp`-c1.lis' >> imp2021-c
	@echo './pass2-c `basename $$1 .imp`-c.icd,$$1 `basename $$1 .imp`-c.ibj,`basename $$1 .imp`-c2.lis' >> imp2021-c
	@echo './pass3elf `basename $$1 .imp`-c.ibj `basename $$1 .imp`.o' >> imp2021-c
	@echo 'if [ "$$DOLINK" = "yes" ] ; then' >> imp2021-c
	@echo '     if [ "`echo $$ARCH | cut -c -3`" = "x86" ] ; then' >> imp2021-c
	@echo '         :' >> imp2021-c
	@echo '     elif [ X"$$ARCH" = X"i686" ] ; then' >> imp2021-c
	@echo '         :' >> imp2021-c
	@echo '     elif [ "`echo $$ARCH | cut -c -3`" = "amd" ] ; then' >> imp2021-c
	@echo '         :' >> imp2021-c
	@echo '     else' >> imp2021-c
	@echo '         echo "Warning: This Imp77 compiler creates binaries for Intel/AMD architecture machines only."' >> imp2021-c
	@echo '         echo "I am not sure what $$ARCH is, but there is a chance it will not run your compiled Imp code"' >> imp2021-c
	@echo '         echo "(unless you can run under an emulator such as Wine or qemu?)"' >> imp2021-c
	@echo '         echo "Info on installing a cross-compiler at https://opensource.com/article/19/7/cross-compiling-gcc"' >> imp2021-c
	@echo '     fi' >> imp2021-c
	@echo '    if [ X"`uname -s`" = X"Linux" ] ; then' >> imp2021-c
	@echo '        # The bootstrap seems to work fine on my Ubuntu without the -T ld script.' >> imp2021-c
	@echo '        gcc -m32 -o `basename $$1 .imp`-c `basename $$1 .imp`.o -L. -limp77 -lm # -T ld.i77.script' >> imp2021-c
	@echo '    elif [ X"`uname -s`" = X"FreeBSD" ] ; then' >> imp2021-c
	@echo '        FREEBSDOPTS="-B/usr/lib32 -B/usr/local/lib32/gcc9/"  # -B  -   Bob Eagers FreeBSD :-)  (for now)' >> imp2021-c
	@echo '        gcc $$FREEBSDOPTS -m32 -o `basename $$1 .imp`-c `basename $$1 .imp`.o -L. -limp77 -lm # -T ld.i77.script' >> imp2021-c
	@echo '    else' >> imp2021-c
	@echo '        # if you are not using Linux or FreeBSD, you may need to edit this line if it does not just work...:' >> imp2021-c
	@echo '        gcc -m32 -o `basename $$1 .imp`-c `basename $$1 .imp`.o -L. -limp77 -lm # -T ld.i77.script' >> imp2021-c
	@echo '    fi' >> imp2021-c
	@echo 'fi' >> imp2021-c
	@chmod +x ./imp2021-c
	@echo 'If your OS is neither Linux nor FreeBSD, you may need to modify the parameters'
	@echo 'given to gcc when linking.  The script to look at and edit is imp2021-c'
	@echo 'I will set it up to use some default parameters for now.'

libimp77.a: imp2021-c pass1-c pass2-c Makefile-lib remove-cr getfile
	make -f Makefile-lib

pass3elf: pass3elf.c writebig.c ifreader.c pass3.h
	gcc -o pass3elf pass3elf.c writebig.c ifreader.c

pass3.h: getfile remove-cr
	./getfile https://raw.githubusercontent.com/siliconsam/imp2021/main/source/imp/pass3/pass3.h
	@./remove-cr pass3.h

pass3elf.c: getfile remove-cr
	./getfile https://raw.githubusercontent.com/siliconsam/imp2021/main/source/imp/pass3/pass3elf.c
	@./remove-cr pass3elf.c

writebig.c: getfile remove-cr
	./getfile https://raw.githubusercontent.com/siliconsam/imp2021/main/source/imp/pass3/writebig.c
	@./remove-cr writebig.c

ifreader.c: getfile remove-cr
	./getfile https://raw.githubusercontent.com/siliconsam/imp2021/main/source/imp/pass3/ifreader.c
	@./remove-cr ifreader.c

# I've spared you the extra complication of the version with backtrace support, but it's
# easy to add.  I'll document it later.  Not needed for a simple scripted bootstrap, but
# useful if doing development on the compiler.

pass2-c: pass1-c pass2.c impsup-signals.c crc32.h crc32.c imptoc.h impsig.h Makefile
	gcc -o pass2-c pass2.c impsup-signals.c  # -lbacktrace

pass1-c: pass1.c impsup-signals.c imptoc.h impsig.h tables.h Makefile
	gcc -o pass1-c pass1.c impsup-signals.c  # -lbacktrace

tables.h: i77.grammar takeon-c ecce tables.ecc Makefile
	./takeon-c i77.grammar
	./ecce i77.tables-new.imp tables.h < tables.ecc > /dev/null 2> /dev/null
	@rm -f i77.tables-new.imp

# tables.h now differs from the one I was using as follows.  I need to work out why.
# (it may be that I was using the version from before the ~ fix.)

#66,68c66,68
#<  40, 68, 55,104, 60, 93, 25, 40,
#<  93, 23, 25, 57, 25, 90,  0,  0,
#<   0,  0
#---
#>   40,  68,  55, 104,  60,  93,  25,  40,
#>   93,  23,  25,  57,  25,  90, 176, 177,
#>  178, 179

#287c290
#<     131,    131,    131,    131,   2451,    131,   2459, -32351,
#---
#>    131,   131,   131,   131,   2451,   131,   2423, -32351,

i77.grammar: getfile remove-cr
	./getfile https://raw.githubusercontent.com/siliconsam/imp2021/main/source/imp/compiler/i77.grammar
	@./remove-cr i77.grammar

takeon-c: takeon.c Makefile
	gcc -o takeon-c takeon.c

crc32.h: getfile
	./getfile http://www.gtoal.com/imp77-in-C/crc32.h

crc32.c: getfile
	./getfile http://www.gtoal.com/imp77-in-C/crc32.c

ecce: ecce.c Makefile
	gcc -o ecce ecce.c

ecce.c: getfile
	./getfile http://ecce.sourceforge.net/ecce-v2.10b.c
	mv ecce-v2.10b.c ecce.c

stdperm.imp: getfile remove-cr
	./getfile https://raw.githubusercontent.com/siliconsam/imp2021/main/source/imp/lib/stdperm.imp
	@./remove-cr stdperm.imp

impsig.h: getfile
	./getfile http://www.gtoal.com/imp77-in-C/Release0.1/impsig.h

impsup-signals.c: getfile
	./getfile http://www.gtoal.com/imp77-in-C/Release0.1/impsup-signals.c

imptoc.h: getfile
	./getfile http://www.gtoal.com/imp77-in-C/Release0.1/imptoc.h

pass1.c: getfile
	./getfile http://www.gtoal.com/imp77-in-C/Release0.1/pass1.c

pass2.c: getfile
	./getfile http://www.gtoal.com/imp77-in-C/Release0.1/pass2.c

#This is a TEMP hack until takeon is modified to optionally output C:
tables.ecc: getfile
	./getfile http://www.gtoal.com/imp77-in-C/Release0.1/tables.ecc

takeon.c: getfile
	./getfile http://www.gtoal.com/imp77-in-C/Release0.1/takeon.c

Makefile-lib: getfile
	./getfile http://www.gtoal.com/imp77-in-C/Release0.1/Makefile-lib

clean: Makefile-lib
	@rm -f *.o *~ *.ibj* *.icd* tables.h i77.tables-new.imp i77.par.debug \
	    i77.lex.debug *-c[12].lis takeon-c  pass1-c  pass2-c  pass3elf  \
	    hellow-c  ecce make.log bootstrap.tar.gz
	@make -f Makefile-lib clean

veryclean: clean Makefile-lib
	@make -f Makefile-lib veryclean	
	@echo these will all be re-fetched from github on the next make.
	@rm -f i77.grammar ifreader.c pass3elf.c pass3.h writebig.c stdperm.imp \
	    ld.i77.script ecce.c crc32.h crc32.c imp2021-c README.txt \
	    hellow.imp impsig.h impsup-signals.c imptoc.h pass1.c pass2.c \
	    tables.ecc takeon.c Makefile-lib getfile remove-cr

refresh: getfile
	./getfile http://gtoal.com/imp77-in-C/Release0.1/Makefile
	make veryclean

release: veryclean
	tar --exclude=RCS -cvzf ../bootstrap.tar.gz .
	@mv ../bootstrap.tar.gz .

tidy:
	@rm -f crc32.c crc32.h ecce.c i77.grammar ifreader.c impcore-*.imp implib-*.imp imprtl-*.imp inc.386.registers prim-rtl-file.c *~
