#!/bin/bash
#
# Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3 only, as
# published by the Free Software Foundation.
#
# This code 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
# version 3 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 3 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
SOURCE="$0"
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

ORIG_FORTRAN=$1
shift

log () {
  if [[ "$LOG_F2CWRAPPER" != "" ]]; then
    echo "$*"
  fi
}

# Find F2C and LABS_LLVM_CC installation
if [ -f "${DIR}/f2c" ]
then
  # For a package build, ${DIR} is FASTR_HOME/bin
  F2C="${DIR}/f2c"
  LABS_LLVM_CC="${LABS_TOOLCHAIN_CC}"
  F2C_H_INCLUDE="${DIR}/../include/"
else
  # For the FastR build, ${DIR} is com.o.t.r.native/run/fastr_tools
  F2C="${DIR}/../../../bin/f2c"
  F2C_H_INCLUDE="${DIR}/../../include/"
fi

# Analyze arguments: capture the output file, input file and include directories
other_args=""
source_file=""
output_file=""
while [[ $# -gt 0 ]]; do
  case $1 in
    -o)
      shift
      output_file=$1
    ;;
    -fPIC)
      other_args="$other_args $1"
    ;;
    -fpic)
      other_args="$other_args -fPIC"
    ;;
    -c)
      other_args="$other_args $1"
    ;;    
    f[0-9]{1,4})
    ;;
    -I)
      shift
    ;;
    -*)
    ;;
    *)
      source_file=$1
    ;;
  esac
  shift
done  

log Parsed args:
log output file: "$output_file"
log source file: "$source_file"
log other args: "$other_args"

target_dir=$(dirname "$output_file")
target_file=$(basename "$source_file")
target_file_no_ext=${target_file%.*}
target_file="${target_dir}/${target_file_no_ext}.c"

# Actual conversion from C to Fortran
if [ -f "${source_file}_c" ]
then
  # Just copy the patched pre-transformed C source to the target dir,
  # simulating so the f2c transformation
  cp "${source_file}_c" "${target_file}"
else  
  log Running: "$F2C" "${source_file}" "-d${target_dir}"
  log_file="${target_dir}/${target_file_no_ext}.f2c.log"
  echo "Running f2c transformation, the log will be in ${log_file}"
  "$F2C" "${source_file}" "-d${target_dir}" > "$log_file" 2>&1
  ecode=$?
  if [[ $ecode -ne 0 ]]; then
    echo "The f2c transformation of ${source_file} failed. Resorting to ${ORIG_FORTRAN} (no LLVM)."
    log Running: "$ORIG_FORTRAN" "$other_args" -I"${F2C_H_INCLUDE}" -o "$output_file" "$source_file"
    # Note: word splitting of $other_args is intentional, i.e., it is intentionally not quoted
    $ORIG_FORTRAN $other_args -I"${F2C_H_INCLUDE}" -o "$output_file" "$source_file"
    ecode=$?
    exit $ecode
  fi
fi

# Compile the C source code
# Note: we turn off warnings for code patterns that are known to be generated by f2c
log Running: "$LABS_LLVM_CC" "$other_args" -I"${F2C_H_INCLUDE}" -o "$output_file" "$target_file"
# Note: word splitting of $other_args is intentional, i.e., it is intentionally not quoted
"$LABS_LLVM_CC" $other_args -I"${F2C_H_INCLUDE}" -Wno-shift-op-parentheses -Wno-logical-op-parentheses -o "$output_file" "$target_file"

# Remove the generated C file to prevent duplicate object files in the linking command
rm "$target_file"
