# Validate compilers
validatecompilers()  {
# Validate everything related to distcc if SM_GLOBALDISTCC is set
  if [ -n "$SM_GLOBALDISTCC" ] && [ "$SM_GLOBALDISTCC" = "1" ] ; then
    # Check if distcc exists and is an executable
    if [ ! -x "$SM_DISTCCBINPATH" ]; then
      echo "[ERROR] Oops! Distcc binary was not found but building with it"
      echo "[ERROR] was requested! Either ensure distcc is in your "'$PATH'" or"
      echo "[ERROR] disable this option in bldpkg file. bldpkg file"
      echo "[ERROR] is located in $BUILDVARS ! Aborting!"
      exit 1
    fi

    # Check if the symlinks are right
    if [ ! "$(echo "$PATH" | grep "$SM_DISTCCSYMPATH")" ] ; then
      echo "[ERROR] $SM_DISTCCSYMPATH not found in "'$PATH'"! Fix it please."
      exit 1
    elif [ ! -d "$SM_DISTCCSYMPATH" ] ; then 
      echo "[ERROR] $SM_DISTCCSYMPATH directory containing symlinks to distcc"
      echo "[ERROR] does not exist! Kindly create it and create symlinks" 
      echo "[ERROR] based on instructions in bldpkg.conf! Aborting!"
      exit 1
    fi
     
    # Trace symlinks to the binary
    for f in gcc g++ cc c++ ; do 
      if [ -e "$SM_DISTCCSYMPATH/$f" ] && [ -L "$SM_DISTCCSYMPATH/$f" ]; then
        # We use "realpath" to follow the $SM_DISTCCSYMPATH/$f symlink and act on the exit code.
        if [ "$(realpath -e "$SM_DISTCCSYMPATH/$f")" != "$SM_DISTCCBINPATH" ] ; then
          echo "[ERROR] $SM_DISTCCSYMPATH/$f does not point to $SM_DISTCCBINPATH. "
          echo "[ERROR] Kindly fix this! Aborting!"
          exit 1
        fi
      else
        echo "[ERROR] $f either does not exist or is not a symlink inside"
        echo "[ERROR] $SM_DISTCCSYMPATH. Kindly fix this! Aborting!"
        exit 1
      fi
      
      #echo -n "Validating distcc $f compiler... "

      # This is a really small C program taken from autoconf tests
      #cat << EOF > $SM_PARENTTMP/distcccheck-"$f".c
#int 
#main()
#{
#  ;
#  return 0;
#}
#EOF
      
      #"$SM_DISTCCSYMPATH/$f" -o $SM_PARENTTMP/distcccheck-"$f" $SM_PARENTTMP/distcccheck-"$f".c
      #CHECKSTATUS="$?"

      # Discard the files once the validation passes/fails
      #if [ "$CHECKSTATUS" == "0" ] ; then
      #  echo "[OK]"
      #  rm $SM_PARENTTMP/distcccheck-"$f"{,.c}
      #else
      #  echo "Something's up with distcc $f"
      #  rm $SM_PARENTTMP/distcccheck-"$f"{,.c}
      #  exit 1
      #fi

    done

    # If SM_DISTCC=0 is set in the package build file to disable distcc, remove the value of $SM_DISTCCSYMPATH from 
    # $PATH otherwise export DISTCC_HOSTS and DISTCC_IO_TIMEOUT variables.
    if [ "$SM_DISTCC" = "0" ] ; then
      PATH="$(echo "$PATH" | sed "s@:$SM_DISTCCSYMPATH@@g")"
      export PATH
    else
      export DISTCC_HOSTS DISTCC_IO_TIMEOUT
    fi
  else
    # Remove $SM_DISTCCSYMPATH 
    PATH="$(echo "$PATH" | sed "s@:$SM_DISTCCSYMPATH@@g")"
    export PATH
  fi

  # Validate everything related to ccache if SM_GLOBALCCACHE is set
  if [ -n "$SM_GLOBALCCACHE" ] &&  [ "$SM_GLOBALCCACHE" = "1" ]; then
    if [ ! -x "$SM_CCACHEBINPATH" ] ; then
      echo "[ERROR] Oops! ccache binary was not found but building with it" 
      echo "[ERROR] was requested! Either ensure ccache is in your "'$PATH'" or"
      echo "[ERROR] disable this option in bldpkg.conf. bldpkg.conf"
      echo "[ERROR] file is located in $BUILDVARS . Aborting!" 
      exit 1
    fi

    if [ ! "$(echo $PATH | grep "$SM_CCACHESYMPATH")" ] ; then
      echo "[ERROR] $SM_CCACHESYMPATH not found in "'$PATH!'" Fix it please."
      exit 1
    elif [ ! -d "$SM_CCACHESYMPATH" ] ; then 
      echo "[ERROR] $SM_CCACHESYMPATH directory containing symlinks to ccache"
      echo "[ERROR] does not exist! Kindly create it and create symlinks"
      echo "[ERROR] based on instructions in bldpkg.conf. Aborting!"
      exit 1
    fi

    for f in gcc g++ cc c++ ; do 
      if [ -e "$SM_CCACHESYMPATH/$f" ] && [ -L "$SM_CCACHESYMPATH/$f" ]; then
        # We use "realpath" to follow the $SM_CCACHESYMPATH/$f symlink and
    #    # act on the exit code.
        if [ "$(realpath -e "$SM_CCACHESYMPATH/$f")" != "$SM_CCACHEBINPATH" ] ; then
          echo "[ERROR] $SM_CCACHESYMPATH/$f does not point to $SM_CCACHEBINPATH. "
          echo "[ERROR] Kindly fix this! Aborting!"
          exit 1
        fi
      else
          echo "[ERROR] $f either does not exist or is not a symlink inside $SM_CCACHESYMPATH" 
          echo "[ERROR] Kindly fix this! Aborting!"
          exit 1
      fi

      #echo -n "Validating ccache $f compiler... "

      # This is a really small C program taken from autoconf tests
      #cat << EOF > $SM_PARENTTMP/ccachecheck-"$f".c
#int 
#main()
#{
#  ;
#  return 0;
#}
#EOF
      
      #"$SM_CCACHESYMPATH/$f" -o $SM_PARENTTMP/ccachecheck-"$f" $SM_PARENTTMP/ccachecheck-"$f".c
      #CHECKSTATUS="$?"

      # Discard the files once the validation passes/fails
      #if [ "$CHECKSTATUS" == "0" ] ; then
      #  echo "[OK]"
      #  rm $SM_PARENTTMP/ccachecheck-"$f"{,.c}
      #else
      #  echo "Something's up with ccache $f"
      #  rm $SM_PARENTTMP/ccachecheck-"$f"{,.c}
      #  exit 1
      #fi

    done

    # If SM_CCACHE=0 is set in the package build file to disable ccache, remove the value of SM_CCACHESYMPATH
    # from $PATH and export it again
    if [ "$SM_CCACHE" = "0" ]; then
      PATH="$(echo "$PATH" | sed "s@$SM_CCACHESYMPATH:@@g")"
      export PATH
    fi
  else
     # Remove $SM_CCACHESYMPATH
      PATH="$(echo "$PATH" | sed "s@$SM_CCACHESYMPATH:@@g")"
      export PATH
  fi

  # Validate everything related to sccache if SM_GLOBALCCACHE is set
  if [ -n "$SM_GLOBALSCCACHE" ] &&  [ "$SM_GLOBALSCCACHE" = "1" ]; then
    if [ ! -x "$SM_SCCACHEBINPATH" ] ; then
      echo "[ERROR] Oops! sccache binary was not found but building with it" 
      echo "[ERROR] was requested! Either ensure sccache is in your "'$PATH'" or"
      echo "[ERROR] disable this option in bldpkg.conf. bldpkg.conf"
      echo "[ERROR] file is located in $BUILDVARS . Aborting!" 
      exit 1
    fi

    if [ ! "$(echo $PATH | grep "$SM_SCCACHEPATH")" ] ; then
      echo "[ERROR] $SM_SCCACHEPATH not found in "'$PATH!'" Fix it please."
      exit 1
    elif [ ! -d "$SM_SCCACHEPATH" ] ; then 
      echo "[ERROR] $SM_SCCACHEPATH directory containing symlinks to ccache"
      echo "[ERROR] does not exist! Kindly create it and create symlinks"
      echo "[ERROR] based on instructions in bldpkg.conf. Aborting!"
      exit 1
    fi

    for f in gcc g++ cc c++ ; do 
      # A hard link is basically a copy of a file with the same inode number stored in a different location.
      # We are trying a bit hard to ascertain whether a binary is a hard link or not.
      # First get the inode number of the binary in the original location
      SM_SCCACHE_BINARY_INODE_NUM="$(stat --printf '%i\n' $SM_SCCACHEBINPATH)"
      # Then get the inode number of the file inside the hard link path
      SM_SCCACHE_HARDLINK_FILE_INODE_NUM="$(stat --printf '%i\n' $SM_SCCACHEPATH/$f)"

      if [ ! -e "$SM_SCCACHEPATH/$f" ] ; then
          echo "[ERROR] $f either does not exist inside $SM_SCCACHEPATH" 
          echo "[ERROR] Kindly fix this! Aborting!"
          exit 1
      # If the hard link's inode number does not match the original binary's inode number, throw an error and exit
      elif [ "$SM_SCCACHE_HARDLINK_FILE_INODE_NUM" != "$SM_SCCACHE_BINARY_INODE_NUM" ] ; then
          echo "[ERROR] File '"$f"' inside $SM_SCCACHEPATH is not a hard link!"
	  echo "[ERROR] Kindly fix this! Aborting!"
	  exit 1
      fi

      #echo -n "Validating sccache $f compiler... "

      # This is a really small C program taken from autoconf tests
      #cat << EOF > $SM_PARENTTMP/sccachecheck-"$f".c
#int 
#main()
#{
#  ;
#  return 0;
#}
#EOF
      
      #"$SM_SCCACHEPATH/$f" -o $SM_PARENTTMP/sccachecheck-"$f" $SM_PARENTTMP/sccachecheck-"$f".c
      #CHECKSTATUS="$?"

      # Discard the files once the validation passes/fails
      #if [ "$CHECKSTATUS" == "0" ] ; then
      #  echo "[OK]"
      #  rm $SM_PARENTTMP/sccachecheck-"$f"{,.c}
      #else
      #  echo "Something's up with sccache $f"
      #  rm $SM_PARENTTMP/sccachecheck-"$f"{,.c}
      #  exit 1
      #fi

    done

    # If SM_SCCACHE=0 is set in the package build file to disable sccache, remove the value of SM_SCCACHEPATH
    # from $PATH and export it again
    if [ "$SM_SCCACHE" = "0" ]; then
      PATH="$(echo "$PATH" | sed "s@$SM_SCCACHEPATH:@@g")"
      export PATH
    fi
  else
     # Remove $SM_SCCACHEPATH
      PATH="$(echo "$PATH" | sed "s@$SM_SCCACHEPATH:@@g")"
      export PATH
  fi
}
