#!/bin/bash
#
# Targeted Data Discovery Script  
# Fast identification of critical data locations for migration planning
# Avoids filesystem traversal bottlenecks
#

set -euo pipefail

TIMESTAMP=$(date +%Y%m%d_%H%M%S)
HOSTNAME=$(hostname -f)
OUTPUT_DIR="/tmp/data_discovery_${HOSTNAME}_${TIMESTAMP}"
mkdir -p "$OUTPUT_DIR"
LOG_FILE="${OUTPUT_DIR}/data.log"

exec > >(tee -a "$LOG_FILE") 2>&1
echo "Starting Data Discovery on ${HOSTNAME} at $(date)"
echo "Output: $OUTPUT_DIR"
echo "========================================"

# Database locations (common paths only)
echo "1. Database Locations"
echo "--- PostgreSQL ---" > "$OUTPUT_DIR/databases.txt"
find /var/lib/postgresql /opt/postgresql -name "*.conf" -o -name "postgresql.conf" 2>/dev/null >> "$OUTPUT_DIR/databases.txt" || true
echo "--- MySQL/MariaDB ---" >> "$OUTPUT_DIR/databases.txt"
find /var/lib/mysql /etc/mysql -name "my.cnf" -o -name "*.cnf" 2>/dev/null >> "$OUTPUT_DIR/databases.txt" || true
echo "--- SQLite ---" >> "$OUTPUT_DIR/databases.txt"
find /var/lib /opt -maxdepth 3 -name "*.db" -o -name "*.sqlite*" 2>/dev/null >> "$OUTPUT_DIR/databases.txt" || true

# Docker data locations
echo "2. Docker Data Locations"
if command -v docker >/dev/null 2>&1; then
    docker system df > "$OUTPUT_DIR/docker_storage.txt" 2>/dev/null || echo "Docker system df failed"
    docker volume ls --format "table {{.Name}}\t{{.Driver}}\t{{.Mountpoint}}" > "$OUTPUT_DIR/docker_volumes.txt" 2>/dev/null || true
    
    # Get volume mount points
    echo "Docker volume details:" > "$OUTPUT_DIR/docker_volume_details.txt"
    docker volume ls --format "{{.Name}}" | while read volume; do
        echo "Volume: $volume" >> "$OUTPUT_DIR/docker_volume_details.txt"
        docker volume inspect "$volume" 2>/dev/null >> "$OUTPUT_DIR/docker_volume_details.txt" || true
        echo "---" >> "$OUTPUT_DIR/docker_volume_details.txt"
    done
fi

# Configuration files (targeted search)
echo "3. Critical Configuration Files"
echo "=== Application Configs ===" > "$OUTPUT_DIR/config_files.txt"
find /etc -maxdepth 2 -name "*.conf" -o -name "*.cfg" -o -name "*.ini" 2>/dev/null | head -30 >> "$OUTPUT_DIR/config_files.txt"
echo "=== Docker Compose Files ===" >> "$OUTPUT_DIR/config_files.txt"
find /opt /home -maxdepth 4 -name "docker-compose.yml" -o -name "docker-compose.yaml" -o -name "compose.yml" 2>/dev/null >> "$OUTPUT_DIR/config_files.txt" || true

# Storage and mount information
echo "4. Storage & Mount Points"
df -hT > "$OUTPUT_DIR/disk_usage.txt"
mount > "$OUTPUT_DIR/mount_points.txt"
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT > "$OUTPUT_DIR/block_devices.txt"

# NFS and network storage
echo "5. Network Storage"
if command -v showmount >/dev/null 2>&1; then
    showmount -e localhost > "$OUTPUT_DIR/nfs_exports.txt" 2>/dev/null || echo "No NFS exports"
fi
grep nfs /proc/mounts > "$OUTPUT_DIR/nfs_mounts.txt" 2>/dev/null || echo "No NFS mounts"

# Samba/SMB shares
echo "6. SMB/Samba Shares"
if command -v smbstatus >/dev/null 2>&1; then
    smbstatus -S > "$OUTPUT_DIR/smb_shares.txt" 2>/dev/null || echo "SMB not running"
fi
if [ -f /etc/samba/smb.conf ]; then
    cp /etc/samba/smb.conf "$OUTPUT_DIR/" 2>/dev/null || true
fi

# Application-specific data directories
echo "7. Application Data Directories"
echo "=== Common App Directories ===" > "$OUTPUT_DIR/app_directories.txt"
ls -la /var/lib/ 2>/dev/null | grep -E "(mysql|postgresql|redis|nginx|apache|docker)" >> "$OUTPUT_DIR/app_directories.txt" || true
echo "=== /opt Applications ===" >> "$OUTPUT_DIR/app_directories.txt"
ls -la /opt/ 2>/dev/null >> "$OUTPUT_DIR/app_directories.txt" || true
echo "=== /srv Data ===" >> "$OUTPUT_DIR/app_directories.txt"
ls -la /srv/ 2>/dev/null >> "$OUTPUT_DIR/app_directories.txt" || true

# Log directories (critical for troubleshooting)
echo "8. Log Locations"
echo "=== System Logs ===" > "$OUTPUT_DIR/log_locations.txt"
ls -la /var/log/ | head -20 >> "$OUTPUT_DIR/log_locations.txt"
echo "=== Application Logs ===" >> "$OUTPUT_DIR/log_locations.txt"
find /opt /var/log -maxdepth 3 -name "*.log" 2>/dev/null | head -20 >> "$OUTPUT_DIR/log_locations.txt" || true

# Home directory critical data
echo "9. User Data Locations"
ls -la /home/ > "$OUTPUT_DIR/user_directories.txt" 2>/dev/null || echo "No /home directory"
find /home -maxdepth 2 -type d -name ".*" 2>/dev/null | head -20 > "$OUTPUT_DIR/user_hidden_dirs.txt" || true

# System package data
echo "10. Package Manager Data"
if command -v dpkg >/dev/null 2>&1; then
    dpkg -l | wc -l > "$OUTPUT_DIR/package_count.txt"
    echo "dpkg packages: $(cat "$OUTPUT_DIR/package_count.txt")" >> "$OUTPUT_DIR/package_summary.txt"
fi
if command -v rpm >/dev/null 2>&1; then
    rpm -qa | wc -l > "$OUTPUT_DIR/rpm_package_count.txt"
    echo "rpm packages: $(cat "$OUTPUT_DIR/rpm_package_count.txt")" >> "$OUTPUT_DIR/package_summary.txt"
fi

# Backup locations
echo "11. Backup Locations"
echo "=== Common Backup Directories ===" > "$OUTPUT_DIR/backup_locations.txt"
find /backup /backups /mnt -maxdepth 2 -type d 2>/dev/null >> "$OUTPUT_DIR/backup_locations.txt" || echo "No backup directories found"

echo "Data discovery completed at $(date)"
echo "Results in: $OUTPUT_DIR"
ls -la "$OUTPUT_DIR"