#!/bin/bash set -Eeuo pipefail PLUGIN_DIR="$(cd "$(dirname "$0")/../.." && pwd)" HOOK_ROOT="${DA_CUSTOM_HOOK_DIR:-/usr/local/directadmin/scripts/custom}" ACTION="${1:-install}" MARKER="managed by DirectAdmin MySQL plugin hook dispatcher" log() { printf 'mysql da-integration: %s\n' "$*" } die() { printf 'mysql da-integration: %s\n' "$*" >&2 exit 1 } write_dispatcher() { local target="$1" local tmp tmp="$(mktemp)" cat > "$tmp" <<'EOF' #!/bin/bash # managed by DirectAdmin MySQL plugin hook dispatcher set -u hook_name="$(basename "$0")" hook_dir="${0}.d" status=0 if [ ! -d "$hook_dir" ]; then exit 0 fi for hook in "$hook_dir"/*.sh; do [ -e "$hook" ] || continue [ -x "$hook" ] || continue DA_HOOK_NAME="$hook_name" "$hook" "$@" status=$? if [ "$status" -ne 0 ]; then exit "$status" fi done exit 0 EOF install -m 0755 "$tmp" "$target" rm -f "$tmp" } install_hook() { local hook_name="$1" local source_script="$2" local link_name="${3:-10-alt-mysql.sh}" local target="$HOOK_ROOT/$hook_name" local hook_dir="$HOOK_ROOT/${hook_name}.d" local preserved="" [ -f "$source_script" ] || die "missing hook source: $source_script" chmod 0755 "$source_script" 2>/dev/null || true mkdir -p "$hook_dir" if [ -e "$target" ] && ! grep -Fq "$MARKER" "$target" 2>/dev/null; then if [ -d "$target" ]; then die "cannot preserve hook because target is a directory: $target" fi preserved="$hook_dir/00-existing.sh" if [ -e "$preserved" ]; then preserved="$hook_dir/00-existing.$(date +%Y%m%d%H%M%S).sh" fi mv "$target" "$preserved" chmod 0755 "$preserved" 2>/dev/null || true log "preserved existing hook $hook_name as $preserved" fi write_dispatcher "$target" ln -sfn "$source_script" "$hook_dir/$link_name" chmod 0755 "$target" "$source_script" 2>/dev/null || true log "installed hook $hook_name -> $source_script" } remove_hook_link() { local hook_name="$1" local link_name="${2:-10-alt-mysql.sh}" local target="$HOOK_ROOT/$hook_name" local hook_dir="$HOOK_ROOT/${hook_name}.d" rm -f "$hook_dir/$link_name" if [ -d "$hook_dir" ] && ! find "$hook_dir" -mindepth 1 -maxdepth 1 -print -quit | grep -q .; then rmdir "$hook_dir" 2>/dev/null || true if [ -f "$target" ] && grep -Fq "$MARKER" "$target" 2>/dev/null; then rm -f "$target" fi fi } install_all() { mkdir -p "$HOOK_ROOT" install_hook "user_backup_compress_pre.sh" "$PLUGIN_DIR/scripts/da-integration/alt_mysql_user_backup_compress_pre.sh" install_hook "user_restore_post_pre_cleanup.sh" "$PLUGIN_DIR/scripts/da-integration/alt_mysql_user_restore_post_pre_cleanup.sh" install_hook "database_create_pre.sh" "$PLUGIN_DIR/scripts/da-integration/block_native_database_action.sh" "20-alt-mysql-block-native-db.sh" install_hook "database_delete_pre.sh" "$PLUGIN_DIR/scripts/da-integration/block_native_database_action.sh" "20-alt-mysql-block-native-db.sh" install_hook "database_user_password_change_pre.sh" "$PLUGIN_DIR/scripts/da-integration/block_native_database_action.sh" "20-alt-mysql-block-native-db.sh" install_hook "database_user_create_post.sh" "$PLUGIN_DIR/scripts/da-integration/block_native_database_action.sh" "20-alt-mysql-observe-native-db.sh" install_hook "database_destroy_user_post.sh" "$PLUGIN_DIR/scripts/da-integration/block_native_database_action.sh" "20-alt-mysql-observe-native-db.sh" } uninstall_all() { remove_hook_link "user_backup_compress_pre.sh" remove_hook_link "user_restore_post_pre_cleanup.sh" remove_hook_link "database_create_pre.sh" "20-alt-mysql-block-native-db.sh" remove_hook_link "database_delete_pre.sh" "20-alt-mysql-block-native-db.sh" remove_hook_link "database_user_password_change_pre.sh" "20-alt-mysql-block-native-db.sh" remove_hook_link "database_user_create_post.sh" "20-alt-mysql-observe-native-db.sh" remove_hook_link "database_destroy_user_post.sh" "20-alt-mysql-observe-native-db.sh" } case "$ACTION" in install) install_all ;; uninstall) uninstall_all ;; *) die "unknown action: $ACTION" ;; esac