Files
mailsystem/migrate_markasjunk2_to_markasjunk.sh
T

161 lines
6.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# =============================================================================
# migrate_markasjunk2_to_markasjunk.sh
#
# Migrates the external roundcube-markasjunk2 plugin to the built-in
# markasjunk plugin included in Roundcube 1.7.x+
#
# Usage: migrate_markasjunk2_to_markasjunk.sh <roundcube-install-dir>
#
# Example:
# migrate_markasjunk2_to_markasjunk.sh /var/www/webmail/roundcubemail-1.7.1
# =============================================================================
RC_DIR="${1}"
# -----------------------------------------------------------------------------
# Colors / output helpers
# -----------------------------------------------------------------------------
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[1;33m'
cyan='\033[0;36m'
reset='\033[0m'
info() { echo -e "${cyan}[INFO]${reset} $*"; }
ok() { echo -e "${green}[OK]${reset} $*"; }
skip() { echo -e "\033[0;37m[SKIP] $*${reset}"; }
warn() { echo -e "${yellow}[WARN]${reset} $*"; }
error() { echo -e "${red}[ERROR]${reset} $*"; }
#fatal() { echo -e "${red}[FATAL]${reset} $*"; exit 1; }
fatal() { echo -e "\n ${red}[FATAL]${reset} $*\n"; exit 1; }
# -----------------------------------------------------------------------------
# Sanity checks
# -----------------------------------------------------------------------------
[[ -z "$RC_DIR" ]] && fatal "Usage: $0 <roundcube-install-dir>"
[[ -d "$RC_DIR" ]] || fatal "Directory not found: $RC_DIR"
RC_CONFIG="${RC_DIR}/config/config.inc.php"
PLUGIN_DIR_NEW="${RC_DIR}/plugins/markasjunk"
PLUGIN_CONFIG_NEW="${PLUGIN_DIR_NEW}/config.inc.php"
[[ -f "$RC_CONFIG" ]] || fatal "Roundcube config not found: $RC_CONFIG"
[[ -d "$PLUGIN_DIR_NEW" ]] || fatal "Built-in markasjunk plugin not found: ${PLUGIN_DIR_NEW}"
echo ""
echo "================================================================="
echo " Migrate markasjunk2 -> markasjunk"
echo " Roundcube: ${RC_DIR}"
echo "================================================================="
echo ""
# -----------------------------------------------------------------------------
# Find markasjunk2 plugin directory (may have different names)
# -----------------------------------------------------------------------------
PLUGIN_DIR_OLD=""
for candidate in \
"${RC_DIR}/plugins/markasjunk2" \
"${RC_DIR}/plugins/roundcube-markasjunk2-master" \
"${RC_DIR}/plugins/roundcube-markasjunk2" ; do
if [[ -d "$candidate" ]]; then
PLUGIN_DIR_OLD="$candidate"
break
fi
done
PLUGIN_CONFIG_OLD=""
if [[ -n "$PLUGIN_DIR_OLD" ]]; then
if [[ -f "${PLUGIN_DIR_OLD}/config.inc.php" ]]; then
PLUGIN_CONFIG_OLD="${PLUGIN_DIR_OLD}/config.inc.php"
elif [[ -f "${PLUGIN_DIR_OLD}/config.inc.php.dist" ]]; then
PLUGIN_CONFIG_OLD="${PLUGIN_DIR_OLD}/config.inc.php.dist"
warn "No config.inc.php found in markasjunk2 plugin, using .dist file"
fi
fi
# -----------------------------------------------------------------------------
# Step 1: Replace plugin name in Roundcube config
# -----------------------------------------------------------------------------
info "Step 1: Replacing 'markasjunk2' with 'markasjunk' in config.inc.php.."
if grep -q "'markasjunk2'" "$RC_CONFIG" ; then
sed -i "s/'markasjunk2'/'markasjunk'/g" "$RC_CONFIG"
if [[ $? -eq 0 ]]; then
ok "Plugin name updated in ${RC_CONFIG}"
else
fatal "Failed to update plugin name in ${RC_CONFIG}"
fi
elif grep -q "'markasjunk'" "$RC_CONFIG" ; then
skip "Plugin 'markasjunk' already active in config"
else
warn "Neither 'markasjunk2' nor 'markasjunk' found in config please check manually"
fi
# -----------------------------------------------------------------------------
# Step 2: Migrate plugin config (markasjunk2_ -> markasjunk_)
# -----------------------------------------------------------------------------
info "Step 2: Migrating plugin config.."
if [[ -f "$PLUGIN_CONFIG_NEW" ]]; then
skip "markasjunk config already exists: ${PLUGIN_CONFIG_NEW}"
elif [[ -n "$PLUGIN_CONFIG_OLD" ]]; then
info "Source config: ${PLUGIN_CONFIG_OLD}"
# Copy and rename all markasjunk2_ keys to markasjunk_
sed "s/markasjunk2_/markasjunk_/g" "$PLUGIN_CONFIG_OLD" > "$PLUGIN_CONFIG_NEW"
if [[ $? -eq 0 ]]; then
ok "Config migrated to ${PLUGIN_CONFIG_NEW}"
info "Key changes applied: markasjunk2_* -> markasjunk_*"
else
fatal "Failed to write ${PLUGIN_CONFIG_NEW}"
fi
else
warn "No markasjunk2 config found using built-in defaults for markasjunk"
info "Defaults are fine if your markasjunk2 config was unmodified"
fi
# -----------------------------------------------------------------------------
# Step 3: Optionally remove old markasjunk2 plugin directory
# -----------------------------------------------------------------------------
if [[ -n "$PLUGIN_DIR_OLD" ]]; then
echo ""
echo -n "Remove old markasjunk2 plugin directory '${PLUGIN_DIR_OLD}'? [yes/no]: "
read REMOVE
REMOVE="$(echo "$REMOVE" | tr '[:upper:]' '[:lower:]')"
while [[ "$REMOVE" != "yes" ]] && [[ "$REMOVE" != "no" ]] ; do
echo -n "Wrong entry! - repeat [yes/no]: "
read REMOVE
done
if [[ "$REMOVE" = "yes" ]]; then
rm -rf "$PLUGIN_DIR_OLD"
if [[ $? -eq 0 ]]; then
ok "Removed: ${PLUGIN_DIR_OLD}"
else
error "Failed to remove: ${PLUGIN_DIR_OLD}"
fi
else
info "Keeping old plugin directory: ${PLUGIN_DIR_OLD}"
fi
else
skip "No markasjunk2 plugin directory found nothing to remove"
fi
# -----------------------------------------------------------------------------
# Summary
# -----------------------------------------------------------------------------
echo ""
echo "================================================================="
echo " Summary"
echo "================================================================="
grep -n "markasjunk" "$RC_CONFIG" | while read line ; do
info "config.inc.php line: $line"
done
[[ -f "$PLUGIN_CONFIG_NEW" ]] && ok "Plugin config: ${PLUGIN_CONFIG_NEW}" \
|| warn "No plugin config using built-in defaults"
echo ""
ok "Migration complete."
echo ""