#!/usr/bin/env bash
#
# This file is part of the Phalcon Framework.
#
# (c) Phalcon Team <team@phalcon.io>
#
# For the full copyright and license information, please view the LICENSE.txt
# file that was distributed with this source code.
#
#  Available params:
#  --phpize
#  --php-config
#
#  Example:
#  ./install --phpize /usr/bin/phpize7.4 --php-config /usr/bin/php-config7.4

# Check best compilation flags for GCC
# By default we compile to be as compatible as possible with all processors.
# If you would like instruct the compiler to generate optimized machine code that matches the processor where it is currently running on you can set your own compile flags by exporting CFLAGS before the build.
#
# Example:
# export CFLAGS="-march=native -O2 -fomit-frame-pointer"
#
# This will generate the best possible code for that chipset but will likely break the compiled object on older chipsets.

export CC="gcc"
if [ -z "$CFLAGS" ]
then
  export CFLAGS="-mtune=native -O2 -fomit-frame-pointer"
fi
export CPPFLAGS="-DPHALCON_RELEASE"

# Set defaults
PHPIZE_BIN=$(command -v phpize 2>/dev/null)
PHPCONFIG_BIN=$(command -v php-config 2>/dev/null)

# Translate long options to short
for arg in "$@"; do
  shift
  case "$arg" in
    "--phpize") set -- "$@" "-i" ;;
    "--php-config") set -- "$@" "-c" ;;
    *) set -- "$@" "$arg"
  esac
done

# Options switcher
while getopts a:i:c: opts; do
   case ${opts} in
      i) PHPIZE_BIN=${OPTARG} ;;
      c) PHPCONFIG_BIN=${OPTARG} ;;
   esac
done

PHP_FULL_VERSION=`${PHPCONFIG_BIN} --version`

if [ $? != 0 ]; then
	echo "php-config is not installed"
	exit 1
fi

if [[ "${PHP_FULL_VERSION:0:1}" == "7" && ${PHP_FULL_VERSION:2:1} < 4 ]]; then
	echo "PHP <= 7.4 is no longer supported"
	exit 1
fi

# Detect possible flags
echo "int main() {}" > t.c
gcc ${CFLAGS} t.c -o t 2> t.t
if [ $? != 0 ]; then
	chmod +x gcccpuopt
	BFLAGS=`./gcccpuopt`
	export CFLAGS="-O2 -fomit-frame-pointer $BFLAGS"
	gcc ${CFLAGS} t.c -o t 2> t.t
	if [ $? != 0 ]; then
		export CFLAGS="-O2"
	fi
fi

# Activate some gcc specific optimizations for gcc >= 4
if [ $(gcc -dumpversion | cut -f1 -d.) -ge 4 ]; then
	gcc ${CFLAGS}-fvisibility=hidden t.c -o t 2> t.t && export CFLAGS="$CFLAGS -fvisibility=hidden"
fi

# gcc $CFLAGS -flto t.c -o t 2> t.t && { export CFLAGS="$CFLAGS -flto"; export LDFLAGS="$LDFLAGS $CFLAGS"; }
rm -f t.t t.c t

cd "phalcon/"

# Clean current compilation
if [ -f Makefile ]; then
	make clean
	${PHPIZE_BIN} --clean
fi

# Perform the compilation
${PHPIZE_BIN}

# For some reason the libtool script being generated by autogen contains lines referring
# to "$echo message" instead of "echo message".
export echo=echo

# Detect Gentoo Linux
if [ -f /etc/gentoo-release ]; then
    LIBTOOLIZE_BIN=$(command -v libtoolize 2>/dev/null)
    aclocal && ${LIBTOOLIZE_BIN} --force && autoheader && autoconf
fi

# Detect macOS
if [ "$(uname -s 2>/dev/null)" = "Darwin" ]; then
    LIBTOOLIZE_BIN=$(command -v glibtoolize 2>/dev/null)
    aclocal && ${LIBTOOLIZE_BIN} --force && autoheader && autoconf
fi

./configure --silent --with-php-config=${PHPCONFIG_BIN} --enable-phalcon

make -s -j"$(getconf _NPROCESSORS_ONLN)"
make -s install

# `make install` copies the module with mode 0755; shared objects only need read permission
chmod 644 "$(${PHPCONFIG_BIN} --extension-dir)/phalcon.so"

echo -e "\nThanks for compiling Phalcon!\nBuild succeed: Please restart your web server to complete the installation\n"
