#!/bin/bash
#
# Post-Transfer Cleanup Script for cPanel Live Migration
# Clears mail contents and document root files while preserving account structure
#
# Usage: ./post_transfer_cleanup.sh <username> [--dry-run]
# Nikolay Ilchev 09/01/2026
CPUSER="${1:-}"
DRY_RUN=0
[[ "${2:-}" == "--dry-run" ]] && DRY_RUN=1
if [[ -z "$CPUSER" ]]; then
    echo "Usage: $0 <username> [--dry-run]"
    exit 1
fi
if [[ ! -f "/var/cpanel/users/$CPUSER" ]]; then
    echo "Error: $CPUSER is not a valid cPanel account"
    exit 1
fi
BACKUP_FOUND=0
if [[ -d "/var/backuply" ]]; then
    BACKUPLY=$(find /var/backuply/backups/$CPUSER -name "*_backup_*_[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_*.json" -mtime -7 -type f 2>/dev/null | head -1)
    if [[ -n "$BACKUPLY" ]]; then
        BACKUP_FOUND=1
        echo "Backuply: $(basename "$BACKUPLY")"
    else
        echo "Backuply: No backup found, checking JetBackup..."
    fi
else
    echo "Backuply: Not installed, checking JetBackup..."
fi
if [[ $BACKUP_FOUND -eq 0 ]] && command -v jetbackup5api &>/dev/null; then
    JB_DATA=$(jetbackup5api -F listAccounts -D "limit=1000" -O json 2>/dev/null | jq -r ".data.accounts[] | select(.username==\"$CPUSER\")")
    if [[ -n "$JB_DATA" ]]; then
        JB_ID=$(echo "$JB_DATA" | jq -r '._id')
        JB_FULL=$(echo "$JB_DATA" | jq -r '.backup_stats["511"]')
        if [[ -n "$JB_FULL" && "$JB_FULL" -gt 0 ]]; then
            BACKUP_FOUND=1
            JB_LATEST=$(jetbackup5api -F listBackups -D "account_id=$JB_ID&type=1&contains=511&limit=200" -O json 2>/dev/null | jq -r '.data.backups | last | .created')
            echo "JetBackup: $JB_FULL full backups, latest: $JB_LATEST"
        fi
    fi
fi
if [[ $BACKUP_FOUND -eq 0 ]]; then
    echo "Error: No backup found for $CPUSER"
    exit 1
fi
if [[ ! -f "/var/cpanel/suspended/$CPUSER" ]]; then
    echo "Error: Account $CPUSER is not suspended"
    exit 1
fi
SUSPEND_REASON=$(cat "/var/cpanel/suspended/$CPUSER" 2>/dev/null)
if [[ "$SUSPEND_REASON" != "User transferred to another server" ]]; then
    echo "Error: Wrong suspension reason: $SUSPEND_REASON"
    exit 1
fi
HOMEDIR=$(grep "^$CPUSER:" /etc/passwd | cut -d: -f6)
if [[ -z "$HOMEDIR" || ! -d "$HOMEDIR" ]]; then
    echo "Error: Cannot find home directory"
    exit 1
fi
du -sh "$HOMEDIR" 2>/dev/null | awk '{print "Current usage: "$1}'
DOCROOTS=$(grep DocumentRoot /etc/apache2/conf/httpd.conf | grep "/home/$CPUSER" | awk '{print $2}' | tr -d '"' | sort -u)
echo "Cleaning document roots..."
for docroot in $DOCROOTS; do
    [[ -d "$docroot" ]] && echo "  $docroot"
done
echo "Cleaning mail directory..."
[[ -d "$HOMEDIR/mail" ]] && echo "  $HOMEDIR/mail"

if [[ $DRY_RUN -eq 1 ]]; then
    echo "[DRY RUN] No changes made"
    exit 0
fi

read -p "Proceed with deletion? (y/n): " CONFIRM
if [[ "$CONFIRM" != "y" ]]; then
    echo "Aborted."
    exit 1
fi

echo "Deleting..."
for docroot in $DOCROOTS; do
    if [[ -d "$docroot" ]]; then
        su - "$CPUSER" -s /bin/bash -c "find '$docroot' -type f ! -name '.htaccess' -delete 2>/dev/null"
        su - "$CPUSER" -s /bin/bash -c "find '$docroot' -mindepth 1 -type d -empty -delete 2>/dev/null"
    fi
done
if [[ -d "$HOMEDIR/mail" ]]; then
    su - "$CPUSER" -s /bin/bash -c "find '$HOMEDIR/mail' -type f -delete 2>/dev/null"
fi
du -sh "$HOMEDIR" 2>/dev/null | awk '{print "New usage: "$1}'
echo "Done."

