#!/bin/bash set -euo pipefail PLUGIN_DIR="$(cd "$(dirname "$0")/.." && pwd)" CPIF="/usr/local/directadmin/data/admin/custom_package_items.conf" CPIL='db_manager=type=checkbox&string=Włącz zarządzanie backupami baz danych&desc=Włącz zarządzanie backupami baz danych&default=no' plugin_always_enabled() { local settings_file="$PLUGIN_DIR/plugin-settings.conf" local line key value [ -r "$settings_file" ] || return 1 while IFS= read -r line; do line="${line%%#*}" key="${line%%=*}" value="${line#*=}" key="$(printf '%s' "$key" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')" if [ "$key" != "always_enable" ]; then continue fi value="$(printf '%s' "$value" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]\"'"'"'')" case "$value" in 1|true|yes|y|on) return 0 ;; *) return 1 ;; esac done < "$settings_file" return 1 } bootstrap_custom_package_item() { local install_cpif=0 local count if [ "$(id -u)" -ne 0 ]; then echo "db-manager: warning: not running as root, cannot update $CPIF" >&2 return 0 fi if [ -f "$CPIF" ]; then sed -i '/^db-manager=/d' "$CPIF" || true sed -i '/^db_manager=/d' "$CPIF" || true count="$(grep -m1 -c '^db_manager=' "$CPIF" || true)" if [ "${count:-0}" -eq 0 ] && ! plugin_always_enabled; then install_cpif=1 fi else if ! plugin_always_enabled; then install_cpif=1 fi fi if [ "$install_cpif" -eq 1 ]; then printf '%s\n' "$CPIL" >> "$CPIF" fi if [ -f "$CPIF" ]; then chown diradmin:diradmin "$CPIF" 2>/dev/null || true chmod 640 "$CPIF" 2>/dev/null || true fi } if [ "${DB_MANAGER_INSTALL_LIB_ONLY:-0}" = "1" ]; then return 0 2>/dev/null || exit 0 fi # Ensure expected permissions and ownership so DirectAdmin can update plugin.conf. if [ "$(id -u)" -eq 0 ]; then chown -R diradmin:diradmin "$PLUGIN_DIR" || true else echo "db-manager: warning: not running as root, cannot chown to diradmin" >&2 fi chmod 755 "$PLUGIN_DIR" "$PLUGIN_DIR/hooks" "$PLUGIN_DIR/user" "$PLUGIN_DIR/scripts" "$PLUGIN_DIR/images" || true chmod 755 "$PLUGIN_DIR/user/index.html" "$PLUGIN_DIR/scripts/install.sh" "$PLUGIN_DIR/scripts/uninstall.sh" "$PLUGIN_DIR/scripts/restore.sh" "$PLUGIN_DIR/scripts/backup.sh" "$PLUGIN_DIR/scripts/worker.sh" || true chmod 644 "$PLUGIN_DIR/plugin.conf" "$PLUGIN_DIR/hooks/user_txt.html" "$PLUGIN_DIR/plugin-settings.conf" "$PLUGIN_DIR/user/enhanced.css" "$PLUGIN_DIR/user/evolution.css" "$PLUGIN_DIR/images/user_icon.svg" || true DATA_DIR="$PLUGIN_DIR/data" mkdir -p "$DATA_DIR/jobs/pending" "$DATA_DIR/jobs/processing" "$DATA_DIR/jobs/done" "$DATA_DIR/logs" "$DATA_DIR/pids" "$DATA_DIR/cancel" "$DATA_DIR/csrf" "$DATA_DIR/lock" chmod 700 "$DATA_DIR" "$DATA_DIR/jobs" "$DATA_DIR/jobs/pending" "$DATA_DIR/jobs/processing" "$DATA_DIR/jobs/done" "$DATA_DIR/logs" "$DATA_DIR/pids" "$DATA_DIR/cancel" "$DATA_DIR/csrf" "$DATA_DIR/lock" || true if [ "$(id -u)" -eq 0 ]; then chown -R diradmin:diradmin "$DATA_DIR" || true fi CRON_FILE="/etc/cron.d/db-manager" if [ "$(id -u)" -eq 0 ]; then echo "* * * * * root /usr/local/directadmin/plugins/db-manager/scripts/worker.sh >/dev/null 2>&1" > "$CRON_FILE" chmod 644 "$CRON_FILE" || true else echo "db-manager: warning: not running as root, cannot install cron job" >&2 fi bootstrap_custom_package_item echo "db-manager: install complete"