#!/bin/bash

# Define the source drives and destination folder
DRIVE_D="smb://Andy@192.168.1.10/D"
DRIVE_E="smb://Andy@192.168.1.10/E"
DESTINATION="smb://Andy@192.168.1.10/D/A-I/Music By Artists 0-A/A/"
Z_FOLDER="$DESTINATION/-z"

# Create the -z folder inside the destination if it doesn't exist
mkdir -p "$Z_FOLDER"

# Function to scan a drive and exclude the destination folder
scan_drive() {
    local DRIVE=$1
    echo "Scanning $DRIVE..."
    # Only move folders starting with "A" (case-sensitive)
    find "$DRIVE" -type d -mindepth 1 -iname "A*" ! -path "$DESTINATION/*" -exec mv {} "$Z_FOLDER" \; 2>/dev/null
}

# Scan the drives (D and E)
scan_drive "$DRIVE_D"
scan_drive "$DRIVE_E"

# Organize folders based on artist names
cd "$Z_FOLDER" || exit

for FOLDER in "$Z_FOLDER"/*; do
    if [ -d "$FOLDER" ]; then
        # Extract artist name from folder name (customize this logic as needed)
        ARTIST=$(basename "$FOLDER")
        ARTIST_FOLDER="$DESTINATION/$ARTIST"

        # Create artist folder if it doesn't exist
        mkdir -p "$ARTIST_FOLDER"

        # Move folder to the corresponding artist folder
        mv "$FOLDER" "$ARTIST_FOLDER/"
        echo "Moved $(basename "$FOLDER") to $ARTIST_FOLDER"
    fi
done

echo "Organizing complete!"
