#!/bin/bash

source debian/vars.sh

set -x 

MYPWD=`pwd`

# pulled from apr-util
mkdir -p config
cp $ea_apr_config config/apr-1-config
cp $ea_apr_config config/apr-config
cp /usr/share/pkgconfig/ea-apr16-1.pc config/apr-1.pc
cp /usr/share/pkgconfig/ea-apr16-util-1.pc config/apr-util-1.pc
cp /usr/share/pkgconfig/ea-apr16-1.pc config
cp /usr/share/pkgconfig/ea-apr16-util-1.pc config

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:`pwd`/config"
echo "PKG_CONFIG_PATH :$PKG_CONFIG_PATH:"

mkdir -p $DEB_INSTALL_ROOT

cp -rf /opt/cpanel/ea-passenger-src/passenger-*/ .
cd passenger-release-$version

echo "RERAKE"

# rerun rake to make sure the code our patches applied and are used
rm -f src/apache2_module/ConfigGeneral/AutoGeneratedDefinitions.cpp
rm -f src/apache2_module/ConfigGeneral/AutoGeneratedSetterFuncs.cpp
rm -f src/apache2_module/ServerConfig/AutoGeneratedStruct.h
rm -f src/apache2_module/DirConfig/AutoGeneratedStruct.h
rm -f src/nginx_module/MainConfig/AutoGeneratedStruct.h
rm -f src/nginx_module/LocationConfig/AutoGeneratedStruct.h

rake src/apache2_module/ConfigGeneral/AutoGeneratedDefinitions.cpp
rake src/apache2_module/ConfigGeneral/AutoGeneratedSetterFuncs.cpp
rake src/apache2_module/ServerConfig/AutoGeneratedStruct.h
rake src/apache2_module/DirConfig/AutoGeneratedStruct.h
rake src/nginx_module/MainConfig/AutoGeneratedStruct.h
rake src/nginx_module/LocationConfig/AutoGeneratedStruct.h

echo "SPECIALVARS"
echo _httpd_mmn           $_httpd_mmn
echo _httpd_confdir       $_httpd_confdir
echo _httpd_modconfdir    $_httpd_modconfdir
echo _httpd_moddir        $_httpd_moddir
echo SOURCE1             $SOURCE1
echo SOURCE2             $SOURCE2
echo _bindir              $_bindir
echo _localstatedir       $_localstatedir
echo passenger_libdir     $passenger_libdir
echo passenger_archdir    $passenger_archdir
echo passenger_agentsdir  $passenger_agentsdir
echo _sbindir             $_sbindir
echo _mandir              $_mandir

echo _sbindir             %{_sbindir}
echo _mandir              %{_mandir}
echo _datadir             %{_datadir}
echo _docdir              %{_docdir}
echo _libdir              %{_libdir}
echo _libexecdir          %{_libexecdir}

echo _prefix              %{_prefix}
echo _sysconfdir          %{_sysconfdir}

mkdir -p build/support/vendor/cxx_hinted_parser

export LD_LIBRARY_PATH=$_libdir:$LD_LIBRARY_PATH
export USE_VENDORED_LIBEV=true
export USE_VENDORED_LIBUV=true
export GEM_PATH=$gem_dir:${GEM_PATH:+${GEM_PATH}}${GEM_PATH:-`ruby -e "print Gem.path.join(':')"`}
CFLAGS="${CFLAGS:-%optflags} -I/usr/include" ; export CFLAGS ;
CFLAGS="$CFLAGS -I/opt/cpanel/ea-apr16/include" ; export CFLAGS ;
CXXFLAGS="${CXXFLAGS:-%optflags}" ; export CXXFLAGS ;

RPATH=""
RPATH="$RPATH -Wl,-rpath=/opt/cpanel/ea-apr16/lib64"
EXTRA_CXX_LDFLAGS="-L/usr/lib64 -lcurl -lssl -lcrypto -lgssapi_krb5 -lkrb5 -lk5crypto -lkrb5support -lssl -lcrypto -lssl -lcrypto -lbrotlidec -lbrotlienc -lbrotlicommon -lssl -lcrypto -lssl -lcrypto -lssl -lcrypto -lssl -lcrypto -lssl -lcrypto "; export EXTRA_CXX_LDFLAGS;
EXTRA_CXX_LDFLAGS="$EXTRA_CXX_LDFLAGS -L/opt/cpanel/ea-apr16/lib64"; export EXTRA_CXX_LDFLAGS;
FFLAGS="${FFLAGS:-%optflags}" ; export FFLAGS;

export LANG=en_US.utf8
export LANGUAGE=en_US.utf8
export LC_ALL=en_US.utf8

rake fakeroot \
    NATIVE_PACKAGING_METHOD=rpm \
    FS_PREFIX=$_prefix \
    FS_BINDIR=$_bindir \
    FS_SBINDIR=$_sbindir \
    FS_DATADIR=$_datadir \
    FS_LIBDIR=$_libdir \
    FS_DOCDIR=$_docdir \
    RUBYLIBDIR=$passenger_libdir \
    RUBYARCHDIR=$passenger_archdir \
    APACHE2_MODULE_PATH=$_httpd_moddir/mod_passenger.so

# find python and ruby scripts to change their shebang
find . -name "*.py" -print | xargs sed -i '1s:^#!.*python.*$:#!/usr/bin/python3:'

cd $MYPWD

echo "CONFIG DONE"

