APACHE_VERSION := 2.4.46
PHP_VERSION := 8.0.2
PREFIX := /var/cfengine

.PHONY: all
all: build

.PHONY: build
build:

.PHONY: install
install:
	# install MP files
	mkdir -p ${PREFIX}/share/GUI ${PREFIX}/share/GUI/application/ ${PREFIX}/share/GUI/ldap/ ${PREFIX}/share/GUI/public/ ${PREFIX}/share/GUI/api/
	cp -axp composer.json composer.lock favicon.ico ${PREFIX}/share/GUI
	cp -axp application/* ${PREFIX}/share/GUI/application/
	cp -axp ldap/* ${PREFIX}/share/GUI/ldap/
	cp -axp public/* ${PREFIX}/share/GUI/public/
	cp -axp Apache-htaccess ${PREFIX}/share/GUI/Apache-htaccess
	cp -axp ../nova/api/http/* ${PREFIX}/share/GUI/api/
	# install PHP dependencies using composer
	if ! [ -f "${PREFIX}/share/GUI/composer" ]; then \
		wget -qO - https://getcomposer.org/installer | ${PREFIX}/httpd/php/bin/php -- --install-dir ${PREFIX}/share/GUI --filename composer; \
	fi
	if ! [ -d "${PREFIX}/share/GUI/vendor" ]; then \
		cd ${PREFIX}/share/GUI && COMPOSER_ALLOW_SUPERUSER=1 ${PREFIX}/httpd/php/bin/php ${PREFIX}/share/GUI/composer install -o; \
	fi
	# set the correct PostgreSQL host
	sed -i -e 's,/tmp,localhost,g' ${PREFIX}/share/GUI/api/config/config.php
	sed -i -e 's,/tmp,localhost,g' ${PREFIX}/share/GUI/application/config/database.php
	# install JS dependencies using npm
	if ! [ -d "${PREFIX}/share/GUI/public/scripts/node_modules" ]; then \
		cd ${PREFIX}/share/GUI/public/scripts; \
		npm install; \
		cd ${PREFIX}/share/GUI/public/scripts/node_modules/jquery-ui; \
		npm install; \
		node node_modules/grunt-cli/bin/grunt sizer concat; \
	fi
	# install the files running the bundle cfe_internal_setup_knowledge
	[ -f "${PREFIX}/bin/cf-agent" ] && ${PREFIX}/bin/cf-agent -K -b cfe_internal_setup_knowledge || true
	# fix permissions
	if [ -d "${PREFIX}/httpd/htdocs/application" ]; then \
		find ${PREFIX}/httpd/htdocs/application/ -type d -exec chmod 750 {} \; ; \
		find ${PREFIX}/httpd/htdocs/application/ -type f -exec chmod 640 {} \; ; \
		chown root:cfapache ${PREFIX}/httpd/htdocs/application/ -R; \
	fi

.PHONY: deps
deps: deps-apache2 deps-php

.PHONY: deps-apache2
deps-apache2:
	[ -f /tmp/httpd-${APACHE_VERSION}.tar.bz2 ] || wget -q -O /tmp/httpd-${APACHE_VERSION}.tar.bz2 http://archive.apache.org/dist/httpd/httpd-${APACHE_VERSION}.tar.bz2
	[ -d /tmp/httpd-${APACHE_VERSION} ] || (cd /tmp && tar -jxf /tmp/httpd-${APACHE_VERSION}.tar.bz2)
	cd /tmp/httpd-${APACHE_VERSION} && ./configure \
		--prefix=${PREFIX}/httpd \
		--enable-so \
		--enable-mods-shared="all ssl ldap authnz_ldap" \
		--with-mpm=prefork
	make -C /tmp/httpd-${APACHE_VERSION}
	make -C /tmp/httpd-${APACHE_VERSION} install
	wget https://raw.githubusercontent.com/cfengine/buildscripts/master/deps-packaging/apache/httpd.conf -O ${PREFIX}/httpd/conf/httpd.conf
	sed -i "s,INSERT_FQDN_HERE,hub,g" ${PREFIX}/httpd/conf/httpd.conf
	sed -i "s,INSERT_CERT_HERE,/etc/ssl/certs/ssl-cert-snakeoil.pem,g" ${PREFIX}/httpd/conf/httpd.conf
	sed -i "s,INSERT_CERT_KEY_HERE,/etc/ssl/private/ssl-cert-snakeoil.key,g" ${PREFIX}/httpd/conf/httpd.conf

.PHONY: deps-php
deps-php:
	[ -f /tmp/php-${PHP_VERSION}.tar.gz ] || wget -q -O /tmp/php-${PHP_VERSION}.tar.gz http://no2.php.net/distributions/php-${PHP_VERSION}.tar.gz
	[ -d /tmp/php-${PHP_VERSION} ] || (cd /tmp && tar -zxf /tmp/php-${PHP_VERSION}.tar.gz)
	cd /tmp/php-${PHP_VERSION} && ./configure \
		--prefix=${PREFIX}/httpd/php \
		--with-config-file-scan-dir=${PREFIX}/httpd/php/lib \
		--with-apxs2=${PREFIX}/httpd/bin/apxs \
		--with-openssl \
		--with-curl \
		--with-ldap \
		--with-pdo-pgsql \
		--with-iconv \
		--with-zlib \
		--with-libmbfl \
		--enable-mbstring \
		--enable-sockets \
		--disable-mbregex \
		--without-fpm-user \
		--without-fpm-group \
		--without-fpm-systemd \
		--without-fpm-acl \
		--without-layout \
		--without-sqlite3 \
		--without-bz2 \
		--without-qdbm \
		--without-ndbm \
		--without-db4 \
		--without-db3 \
		--without-db2 \
		--without-db1 \
		--without-dbm \
		--without-tcadb \
		--without-cdb \
		--without-enchant \
		--without-gettext \
		--without-gmp \
		--without-mhash \
		--without-imap \
		--without-imap-ssl \
		--without-oci8 \
		--without-odbcver \
		--without-adabas \
		--without-sapdb \
		--without-solid \
		--without-ibm-db2 \
		--without-empress \
		--without-empress-bcs \
		--without-custom-odbc \
		--without-iodbc \
		--without-esoob \
		--without-unixODBC \
		--without-dbmaker \
		--without-pdo-dblib \
		--without-pdo-firebird \
		--without-pdo-mysql \
		--without-pdo-oci \
		--without-pdo-oci \
		--without-pdo-oci \
		--without-pdo-odbc \
		--without-pdo-odbc \
		--without-pdo-odbc \
		--without-pdo-sqlite \
		--without-pgsql \
		--without-pspell \
		--without-libedit \
		--without-readline \
		--without-mm \
		--without-snmp \
		--without-tidy \
		--without-xmlrpc \
		--without-xsl \
		--without-pear \
		--without-pear \
		--without-tsrm-pth \
		--without-tsrm-st \
		--without-tsrm-pthreads
	make -C /tmp/php-${PHP_VERSION}
	make -C /tmp/php-${PHP_VERSION} install
	cp /tmp/php-${PHP_VERSION}/php.ini-production ${PREFIX}/httpd/php/lib/php.ini
	sed -ri 's/^\s*expose_php\s*=.*/expose_php = Off/g' ${PREFIX}/httpd/php/lib/php.ini
	sed -ri 's/^\s*memory_limit\s*=.*/memory_limit = 256M/g' ${PREFIX}/httpd/php/lib/php.ini
	sed -ri 's/^(\s|;)*date.timezone\s*=.*/date.timezone = "UTC"/g' ${PREFIX}/httpd/php/lib/php.ini
	sed -ri 's/^(\s|;)phar.readonly = On/phar.readonly = Off/' ${PREFIX}/httpd/php/lib/php.ini
	echo "extension=${PREFIX}/lib/php/cfmod.so" > ${PREFIX}/httpd/php/lib/cfmod.ini
	echo "extension=${PREFIX}/lib/php/cfengine-enterprise-api.so" > ${PREFIX}/httpd/php/lib/cfengine-enterprise-api.ini

.PHONY: setup
setup:
	bash ./scripts/setup.sh

	# cleanup bits left by containers using NTECH_ROOT as /data and writing bits and pieces for selenium tests
clean:
	sudo rm -rf api
	sudo rm -rf vendor
	sudo rm -rf tests/vendor
	sudo rm -rf ldap/vendor
	sudo rm -rf public/tmp

.PHONY: lint
lint:
	echo "For master branch, mission-portal, we expect at least 8.2.12, see ../buildscripts/deps-packaging/php/distfiles for definitive version for each branch"
	php --version
	echo "=== xargs without -L1 (one line at a time) seems to fail in github actions ==="
	time find . -name '*.php' -not -path './vendor/*' -not -path './ldap/vendor/*' -print0 | xargs -L1 -0 php -l 2>&1 >/dev/null
