#! /bin/sh

# Post fetch action script for the muntsos_raspberrypi3 crate

# Symlink subordinate project files to those in the MuntsOS Embedded Linux
# toolchain

INSTALLDIR=/usr/local/gcc*aarch64*muntsos*
find ${INSTALLDIR} -name aws.gpr         -exec ln -s -f {} ";" >/dev/null 2>&1
find ${INSTALLDIR} -name libsimpleio.gpr -exec ln -s -f {} ";" >/dev/null 2>&1

# Make changes to the superordinate program project

i=1
P=`echo "${GPR_PROJECT_PATH}" | awk -F ':' -v i=${i} '{ printf("%s\n", $i) }'`

while [ -n "${P}" ]; do
  grep -q executables "${P}"/alire.toml && grep -q Exec_Dir "${P}"/*.gpr

  if [ $? -eq 0 ]; then
    # Symlink default configuration project file to that in the MuntsOS
    # Embedded Linux toochain

    find ${INSTALLDIR} -name aarch64-linux-gnu.cgpr         -exec ln -s -f {} "${P}"/default.cgpr ";" >/dev/null 2>&1
    find ${INSTALLDIR} -name aarch64-muntsos-linux-gnu.cgpr -exec ln -s -f {} "${P}"/default.cgpr ";" >/dev/null 2>&1

    # Disable style checks if requested

    if [ "$ALIRE_DISABLESTYLECHECKS" = "yes" ]; then
      sed -i 's/Compiler_Switches;$/Compiler_Switches \& ("-gnaty-g");/g' "${P}"/*.gpr
    fi

    # Install Makefile if requested

    if [ "$ALIRE_INSTALLMAKEFILE" = "yes" ]; then
      cp Makefile "${P}"
    fi
  fi

  i=$((i+1))
  P=`echo "${GPR_PROJECT_PATH}" | awk -F ':' -v i=${i} '{ printf("%s\n", $i) }'`
done

exit 0
