#!/bin/sh

set -e

SRC_PATH="/app"
BUILD_PATH="/build"
MODULE_NAME="oc_telegram_shop"
PHAR_PATH="${BUILD_PATH}/${MODULE_NAME}.phar"

echo "PHP Obfuscation: framework"
yakpro-po --config-file /usr/local/yakpro-po/config.php ${SRC_PATH}/framework -o /tmp/obfuscated
rm -rf ${SRC_PATH}/framework
mv /tmp/obfuscated/yakpro-po/obfuscated ${SRC_PATH}/framework

echo "PHP Obfuscation: src"
yakpro-po --config-file /usr/local/yakpro-po/config.php ${SRC_PATH}/src -o /tmp/obfuscated
rm -rf ${SRC_PATH}/src
mv /tmp/obfuscated/yakpro-po/obfuscated ${SRC_PATH}/src

echo "PHP Obfuscation: bastion"
yakpro-po --config-file /usr/local/yakpro-po/config.php ${SRC_PATH}/bastion -o /tmp/obfuscated
rm -rf ${SRC_PATH}/bastion
mv /tmp/obfuscated/yakpro-po/obfuscated ${SRC_PATH}/bastion

echo "📜 Creating Phar archive..."
rm -rf "${BUILD_PATH}/extracted"
mkdir -p "${BUILD_PATH}"
echo "Source path: ${SRC_PATH}"
echo "Destination: ${PHAR_PATH}"
php -d phar.readonly=0 /scripts/ci/create-phar.php "${SRC_PATH}" "${PHAR_PATH}"


echo "Extracting ..."
php -d phar.readonly=0 /scripts/ci/extract-phar.php "${PHAR_PATH}" "${BUILD_PATH}/extracted"
