#! /bin/bash
#
# Copyright (C) 2014,2015,2016,2017,2018,2019,2020,2021  John Gatewood Ham
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# version 2 of the Free Software Foundation License.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
AC=${#}
if ((AC==2)) && [[ "${1}" = "-a" ]]
then
  shift
  COMPILE=0
  ((AC--))
else
  COMPILE=1
fi
if ((AC!=1))
then
  printf "Error: you must give exactly 1 argument, the file name to\n"
  printf "compile, which must have the suffix '.BAS'.\n"
  exit 1
fi
if [[ ! "${1}" =~ \.BAS$ ]]
then
  printf "Error: The source file to compile must have the suffix\n"
  printf "'.BAS'.\n"
  exit 1
fi
if [ ! -r "${1}" ]
then
  printf "Error: The source file to compile is not readable or does\n"
  printf "not exist.\n"
  exit 1
fi
if [[ -z "${LD:-}" ]]
then
  builtin hash -d gold ld >/dev/null 2>&1
  if builtin hash gold >/dev/null 2>&1
  then
    LD=$(builtin hash -t gold)
  elif builtin hash ld >/dev/null 2>&1
  then
    LD=$(builtin hash -t ld)
  else
    printf "Error: Linker not found or is not executable.\n"
    printf "Environment variable LD specified the executable as '%s'\n" "${LD:-Unknown}"
    exit 1
  fi
fi
if [[ -z "${AS:-}" ]]
then
  builtin hash -d as >/dev/null 2>&1
  if builtin hash as >/dev/null 2>&1
  then
    AS=$(builtin hash -t as)
  else
    printf "Error: assembler not found or is not executable.\n"
    printf "Environment variable AS specified the executable as '%s'\n" "${AS:-Unknown}"
    exit 1
  fi
fi
if [ -r dumpregs.o ]
then
  XTRA="dumpregs.o -g"
  XTRAASMFLAG="-L -g"
else
  XTRA=""
  XTRAASMFLAG=""
fi
BASENAME="${1%.BAS}"
if ((COMPILE==1))
then
  if [[ -z "${ECMA55:-}" ]]
  then
    builtin hash -d ecma55 >/dev/null 2>&1
    if builtin hash ecma55 >/dev/null 2>&1
    then
      ECMA55=$(builtin hash -t ecma55)
    elif [[ ! -x "${ECMA55:=./ecma55}" ]]
    then
      printf "Error: Compiler not found or is not executable.\n"
      printf "Location was supposed to be '%s'\n" "${ECMA55:-Unknown}"
      exit 1
    fi
  elif [[ ! -x "${ECMA55}" ]]
  then
    printf "Error: Compiler not found or is not executable.\n"
    printf "Environment variable ECMAA55 specified the executable as '%s'\n" "${ECMA55}"
    exit 1
  fi
  ${ECMA55} ${ECMA55FLAGS:-} "${1}" || exit 1
else
  XTRAASMFLAG="-L"
fi
# had to add no-warn since there is no way, even if I add the .d32
# instruction suffixes, to shut up the warnings about long jumps
# if nojumps is specified
${AS} -a="${1}.s.lst" ${XTRAASMFLAG:-} -alms \
  --no-warn \
  --size-check=error \
  --listing-lhs-width=3 \
  --listing-lhs-width2=3 \
  --listing-rhs-width=120 \
  --64 \
  -n \
  --divide \
  --reduce-memory-overheads \
  -mmnemonic=att \
  -msyntax=att \
  "${1}.s" \
  -o "${BASENAME}.o" || exit 1
${LD} -nostdlib -z defs -z nodefaultlib -z nodlopen -z noexecstack -z relro -z now \
   -Bstatic --no-omagic -m elf_x86_64 -o "${BASENAME}" "${BASENAME}.o" ${XTRA:-}
exit ${?}
