#!/bin/sh
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# create the makevars for unix like systems (tested on debian and ubuntu)
#
# NOTES:
# The system variable USESPECIALPYTHONVERSION can be used to link to a special
# python version.
# export USESPECIALPYTHONVERSION="python3.4"
# it can be very usefull especially for testing but one has to ensure  that
# the file PYVERSION-config is present at the provided location else it will
# fail.
# But when using python3 also python3-dev has to be installed
# (apt-get install python3-dev)
#
# gcc:
# there seems to be a new option which doesn't allows me to compile
# -fstack-protector-strong so I just remove it
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if ! [ -z "$USESPECIALPYTHONVERSION" ]; then
  echo "use special version"
  alias python3="$USESPECIALPYTHONVERSION"
fi

if ! [ -z "$(python3 -V 2>&1 | grep 'python')" ]; then
  if ! [ -z "$(python -V 2>&1 | grep 'python')" ]; then
    echo "Could not find python!"
    exit 1
  else
    alias python3=python
  fi
fi

PYVERSION=`python3 -c "import sys;import distutils.sysconfig as sc;sys.stdout.write(sc.get_config_var('VERSION'))"`
PYINCLUDE=`python3 -c "import sys;import distutils.sysconfig as sc;sys.stdout.write(sc.get_config_var('INCLUDEPY'))"`
PYBINPATH=`python3 -c "import sys;import os;sys.stdout.write(os.path.dirname(sys.executable))"`
PYLDLIBRARY=`python3 -c "import sys;import distutils.sysconfig as sc;sys.stdout.write(sc.get_config_var('LDLIBRARY'))"`
# sys.version_info.major would be a alternative to hexversion
PY3FLAG=`python3 -c 'import sys; print("%i" % (sys.hexversion<0x03000000))'`
PYABOVE38FLAG=`python3 -c 'import sys; print("%i" % (sys.hexversion>0x03080000))'`
PICFLAGS=`"${R_HOME}/bin/R" CMD config CXX11PICFLAGS`
PYXY="PYTHONLIBXY="$PYLDLIBRARY""

if [ $PYABOVE38FLAG -eq 1 ]; then
  echo "detected python3.8 or newer"
  PYEMBEDPKGCONF="-embed"
  PYEMBEDPYCONF="--embed"
fi

## echo $PYVERSION
## echo $PYBINPATH
## echo $PY3FLAG
## echo $PICFLAGS
## echo ""

PKG_LIBS=`$PYBINPATH/python$PYVERSION-config --ldflags $PYEMBEDPYCONF`

##
## PKG_CFLAGS
##
if [ -z "$(pkg-config --version 2>&1 | grep 'pkg-config')" ]; then
  if (pkg-config --exists python-$PYVERSION$PYEMBEDPKGCONF); then
    echo "pkg-config"
    PKG_CFLAGS=`pkg-config --cflags python-$PYVERSION$PYEMBEDPKGCONF`
  else
    #echo "python-config"
    #PKG_CFLAGS=`$PYBINPATH/python$PYVERSION-config --cflags $PYEMBEDPYCONF`
    PKG_CFLAGS="-I$PYINCLUDE"
  fi
else
  #echo "python-config"
  #PKG_CFLAGS=`$PYBINPATH/python$PYVERSION-config --cflags $PYEMBEDPYCONF`
  PKG_CFLAGS="-I$PYINCLUDE"
fi


PKG_CFLAGS=$(echo $PKG_CFLAGS | sed 's/ -Wstrict-prototypes//')
PKG_CFLAGS=$(echo $PKG_CFLAGS | sed 's/ -fstack-protector-strong//')
PKG_CFLAGS=$(echo $PKG_CFLAGS | sed 's/ -Wdate-time//')
PKG_CFLAGS=$(echo $PKG_CFLAGS | sed 's/ -Werror=format-security//')
PKG_CFLAGS=$(echo $PKG_CFLAGS | sed 's/ -Wformat//')

## added 27.07.2018
PKG_CFLAGS=$(echo $PKG_CFLAGS | sed 's/ -fstack-clash-protection//')
PKG_CFLAGS=$(echo $PKG_CFLAGS | sed 's/ -fcf-protection//')

if ! [ -z "$PICFLAGS" ]; then
  PKG_CFLAGS=$(echo $PKG_CFLAGS | sed 's/ -fpic//')
fi



sed -e "s|@PKG_CFLAGS@|$PKG_CFLAGS|" -e "s|@PKG_LIBS@|$PKG_LIBS|" -e "s|@PYXY@|$PYXY|" src/Makevars.in > src/Makevars

echo ""
echo "-----------------------------------"
echo "  Makevars:"
echo ""
cat src/Makevars
echo ""
echo "-----------------------------------"
echo ""

exit 0
