#!/usr/bin/sh
SLOT="/usr/local/synconix/php84"

export PATH="$SLOT/usr/bin:$PATH"
export PHPIZE="$SLOT/usr/bin/phpize"
export PHP_CONFIG="$SLOT/usr/bin/php-config"
export PHP_PEAR_PHP_BIN="$SLOT/usr/bin/php"
export PEAR_SYSTEM_CONFIG="$SLOT/etc/pear.conf"
export PEAR_CONFIG_SYSCONFDIR="$SLOT/etc"

# Isolate from ~/.pearrc completely
export HOME="$SLOT/var/pear-home"
[ -d "$HOME" ] || mkdir -p "$HOME"
unset PEAR_CONFIGFILE

PHP="$SLOT/usr/bin/php"
PHP_INI="$SLOT/etc/php.ini"
PECL_DUMMY_INI="$SLOT/etc/pecl.ini"
INCDIR="$SLOT/usr/share/pear"
CONF="$SLOT/etc/pear.conf"
PEAR="$SLOT/usr/bin/pear"

# Ensure dummy ini exists (so PECL never edits php.ini)
[ -f "$PECL_DUMMY_INI" ] || : > "$PECL_DUMMY_INI" 2>/dev/null || :

# Ensure slot system config exists (recreate if empty/corrupt)
if [ -s "$CONF" ]; then
  "$PEAR" -c "$CONF" -q config-show >/dev/null 2>&1 || rm -f "$CONF"
fi
if [ ! -s "$CONF" ]; then
  "$PEAR" -q config-create "$INCDIR" "$CONF" >/dev/null 2>&1 || :
fi

# Compute this slot's extension dir
EXT_DIR="$(\"$PHP\" -n -r 'echo ini_get(\"extension_dir\");' 2>/dev/null)"

# Repair critical PEAR settings (system scope) so pecl uses this slot
# NOTE: php_prefix MUST point to bin/ WITH trailing slash, otherwise pecl builds "...usrphpize"
"$PEAR" -c "$CONF" -q config-set php_bin     "$SLOT/usr/bin/php"          system >/dev/null 2>&1 || :
"$PEAR" -c "$CONF" -q config-set php_ini     "$PECL_DUMMY_INI"             system >/dev/null 2>&1 || :
"$PEAR" -c "$CONF" -q config-set bin_dir     "$SLOT/usr/bin"               system >/dev/null 2>&1 || :
"$PEAR" -c "$CONF" -q config-set php_dir     "$INCDIR"                     system >/dev/null 2>&1 || :
"$PEAR" -c "$CONF" -q config-set php_prefix  "$SLOT/usr/bin/"              system >/dev/null 2>&1 || :
"$PEAR" -c "$CONF" -q config-set ext_dir     "$EXT_DIR"                    system >/dev/null 2>&1 || :
"$PEAR" -c "$CONF" -q config-set phpize_cmd  "$SLOT/usr/bin/phpize"        system >/dev/null 2>&1 || :
"$PEAR" -c "$CONF" -q config-set php_config  "$SLOT/usr/bin/php-config"    system >/dev/null 2>&1 || :

# Run pecl with the slot php + include_path and the SLOT config (-c "$CONF")
"$PHP" -C -q -c "$PHP_INI" -d include_path="$INCDIR" "$INCDIR/peclcmd.php" -c "$CONF" "$@"
rc=$?

# Auto-enable installed extensions (INI drop-in ONLY)
if [ $rc -eq 0 ] && { [ "${1:-}" = "install" ] || [ "${1:-}" = "upgrade" ]; }; then
  INI_SCAN_DIR="$(\"$PHP\" -n -r 'echo (defined(\"PHP_CONFIG_FILE_SCAN_DIR\") ? PHP_CONFIG_FILE_SCAN_DIR : \"\");' 2>/dev/null)"
  if [ -z "$INI_SCAN_DIR" ] || [ ! -d "$INI_SCAN_DIR" ]; then
    INI_SCAN_DIR="$SLOT/etc/php.d"
  fi
  [ -d "$INI_SCAN_DIR" ] || mkdir -p "$INI_SCAN_DIR"

  for arg in "$@"; do
    case "$arg" in install|upgrade|-*|--*) continue ;; esac
    ext="${arg##*/}"; ext="${ext%%-*}"
    case "$ext" in
      xdebug|pcov) loader="zend_extension"; value="$EXT_DIR/$ext.so" ;;
      *)           loader="extension";      value="$ext.so"           ;;
    esac
    ini_file="$INI_SCAN_DIR/50-$ext.ini"
    if [ ! -f "$ini_file" ]; then
      printf "%s=%s\n" "$loader" "$value" > "$ini_file"
      echo ">>> Created $ini_file to enable $ext"
    fi
  done
fi

# Clean ini on uninstall
if [ $rc -eq 0 ] && [ "${1:-}" = "uninstall" ]; then
  INI_SCAN_DIR="$(\"$PHP\" -n -r 'echo (defined(\"PHP_CONFIG_FILE_SCAN_DIR\") ? PHP_CONFIG_FILE_SCAN_DIR : \"\");' 2>/dev/null)"
  if [ -z "$INI_SCAN_DIR" ] || [ ! -d "$INI_SCAN_DIR" ]; then
    INI_SCAN_DIR="$SLOT/etc/php.d"
  fi
  for arg in "$@"; do
    case "$arg" in uninstall|-*|--*) continue ;; esac
    ext="${arg##*/}"; ext="${ext%%-*}"
    ini_file="$INI_SCAN_DIR/50-$ext.ini"
    [ -f "$ini_file" ] && rm -f "$ini_file" && echo ">>> Removed $ini_file"
  done
fi

exit $rc
