#!/bin/bash

# Detect the USB mount point (assumes it's mounted at /media/ or a similar location)
USB_MOUNT=$(ls /media | grep -v "pre-hook.sh")
if [ -z "$USB_MOUNT" ]; then
    # Fallback log (only if you really want a message when USB is missing):
    echo "$(date +'%Y-%m-%d %H:%M:%S') - No USB drive found." >&2
    exit 1
fi
USB_MOUNT="/media/$USB_MOUNT"

# Set base dir early and ensure it exists before any logging
BASE_DIR="$USB_MOUNT/Driver Diagnostic File"
mkdir -p "$BASE_DIR"
# Ensure the log file exists
: > "$BASE_DIR/logdiagnostic"

# Function to log messages (now safe to write to USB)
logMessage() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$BASE_DIR/logdiagnostic"
}

logMessage "USB drive detected at $USB_MOUNT."
logMessage "Using BASE_DIR: $BASE_DIR."

# Copy subdirectories for all dbus-serialbattery.ttyUSB* found in /data/etc/dbus-sfkdriver/logs
for dir in /data/etc/dbus-sfkdriver/logs/dbus-serialbattery.ttyUSB*; do
    if [ -d "$dir" ]; then
        TTYUSB_DIR=$(basename "$dir")
        mkdir -p "$BASE_DIR/$TTYUSB_DIR"
        logMessage "Created directory: $BASE_DIR/$TTYUSB_DIR"
        
        # Copy corresponding logs
        if cp -r "$dir" "$BASE_DIR/"; then
            logMessage "Successfully copied $dir to $BASE_DIR/."
        else
            logMessage "Failed to copy $dir."
        fi
    fi
done

# Copy other specified directories from /data/log
for dir in  "serial-starter" "start-gui" "dbus-systemcalc-py" "localsettings" "vrmlogger" "gui"; do
    SRC_DIR="/data/log/$dir"
    if [ -d "$SRC_DIR" ]; then
        DEST_DIR="$BASE_DIR/$dir"
        mkdir -p "$DEST_DIR"
        logMessage "Created directory: $DEST_DIR"
        
        # Copy corresponding files from the source to the destination
        if cp -r "$SRC_DIR" "$DEST_DIR"; then
            logMessage "Successfully copied $SRC_DIR to $DEST_DIR."
        else
            logMessage "Failed to copy $SRC_DIR."
        fi
    else
        logMessage "Source directory $SRC_DIR does not exist."
    fi
done

# Copy specific dbus-sfkdriver logs if they exist
for dir in "SFKsettinginstallation" "vedbusservice_SFKvb"; do
    SRC_DIR="/data/etc/dbus-sfkdriver/logs/$dir"
    if [ -d "$SRC_DIR" ]; then
        DEST_DIR="$BASE_DIR/$dir"
        mkdir -p "$DEST_DIR"
        logMessage "Created directory: $DEST_DIR"

        if cp -r "$SRC_DIR" "$DEST_DIR"; then
            logMessage "Successfully copied $SRC_DIR to $DEST_DIR."
        else
            logMessage "Failed to copy $SRC_DIR."
        fi
    else
        logMessage "Source directory $SRC_DIR does not exist."
    fi
done


# Copy everything from /data/etc/dbus-sfkdriver except the 'ext' folder
SRC_DIR="/data/etc/dbus-sfkdriver"
DEST_DIR="$BASE_DIR/dbus-sfkdriver"

if [ -d "$SRC_DIR" ]; then
    mkdir -p "$DEST_DIR"
    logMessage "Created directory: $DEST_DIR"

    rsync -av --exclude 'ext' "$SRC_DIR/" "$DEST_DIR/" >> "$BASE_DIR/logdiagnostic" 2>&1

    if [ $? -eq 0 ]; then
        logMessage "Successfully copied $SRC_DIR to $DEST_DIR (excluding ext)."
    else
        logMessage "Failed to copy $SRC_DIR."
    fi
else
    logMessage "Source directory $SRC_DIR does not exist."
fi


# Copy /opt/victronenergy/dbus-sfkdriver if it exists
SRC_DIR="/opt/victronenergy/dbus-sfkdriver"
DEST_DIR="$BASE_DIR/opt"

if [ -d "$SRC_DIR" ]; then
    mkdir -p "$DEST_DIR"
    logMessage "Created directory: $DEST_DIR"

    if cp -r "$SRC_DIR/"* "$DEST_DIR/"; then
        logMessage "Successfully copied $SRC_DIR contents to $DEST_DIR."
    else
        logMessage "Failed to copy $SRC_DIR contents."
    fi
else
    logMessage "Source directory $SRC_DIR does not exist."
fi



# Create a ZIP archive of the Driver Diagnostic File directory without including the full path
cd "$USB_MOUNT"
if zip -r "Driver_Diagnostic_File.zip" "Driver Diagnostic File"/*; then
    logMessage "Successfully created ZIP archive: Driver_Diagnostic_File.zip."
    
    # Delete the original Driver Diagnostic File directory after zipping
    rm -rf "$BASE_DIR"
    logMessage "Successfully deleted the original Driver Diagnostic File directory."
else
    logMessage "Failed to create ZIP archive."
fi
