#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL dir_count AS LONG
    LOCAL file_total AS LONG
    LOCAL total_size AS DOUBLE
    LOCAL avg_file_size AS DOUBLE
    LOCAL largest_file AS DOUBLE
    LOCAL largest_idx AS LONG
    LOCAL dir_sizes(20) AS DOUBLE
    LOCAL dir_files(20) AS LONG
    LOCAL dir_sub(20) AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL temp_str AS STRING
    LOCAL cumulative_size AS DOUBLE
    LOCAL temp_val AS LONG
    LOCAL temp_d AS DOUBLE

    ' Disk usage analyzer with simulated directory tree

    PRINT "Disk Usage Analyzer"
    PRINT "==================="
    PRINT ""

    dir_count = 8

    dir_sizes(0) = 51200
    dir_files(0) = 12
    dir_sub(0) = 3
    dir_sizes(1) = 25600
    dir_files(1) = 8
    dir_sub(1) = 0
    dir_sizes(2) = 15360
    dir_files(2) = 4
    dir_sub(2) = 0
    dir_sizes(3) = 10240
    dir_files(3) = 6
    dir_sub(3) = 1
    dir_sizes(4) = 4096
    dir_files(4) = 3
    dir_sub(4) = 0
    dir_sizes(5) = 6144
    dir_files(5) = 3
    dir_sub(5) = 0
    dir_sizes(6) = 81920
    dir_files(6) = 15
    dir_sub(6) = 0
    dir_sizes(7) = 32768
    dir_files(7) = 7
    dir_sub(7) = 0

    ' Calculate totals
    total_size = 0
    file_total = 0
    largest_file = 0
    largest_idx = 0

    i = 0
    WHILE i < dir_count
        total_size = total_size + dir_sizes(i)
        file_total = file_total + dir_files(i)
        IF dir_sizes(i) > largest_file THEN
            largest_file = dir_sizes(i)
            largest_idx = i
        END IF
        i = i + 1
    WEND

    IF file_total > 0 THEN
        avg_file_size = total_size / file_total
    END IF

    PRINT "Directory Tree:"
    PRINT "---------------"

    i = 0
    WHILE i < dir_count
        temp_str = STR$(dir_sizes(i) / 1024)
        PRINT "  "
        IF i = 0 THEN PRINT "\Project"
        IF i = 1 THEN PRINT "\Project\src"
        IF i = 2 THEN PRINT "\Project\docs"
        IF i = 3 THEN PRINT "\Project\data"
        IF i = 4 THEN PRINT "\Project\data\input"
        IF i = 5 THEN PRINT "\Project\data\output"
        IF i = 6 THEN PRINT "\Project\build"
        IF i = 7 THEN PRINT "\Project\lib"
        PRINT " (" + temp_str + " KB, " + STR$(dir_files(i)) + " files)"
        i = i + 1
    WEND

    PRINT ""
    PRINT "Summary:"
    PRINT "  Total directories: " + STR$(dir_count)
    PRINT "  Total files: " + STR$(file_total)

    temp_str = STR$(total_size)
    PRINT "  Total size: " + temp_str + " bytes"
    temp_str = STR$(total_size / 1024)
    PRINT "  Total size: " + temp_str + " KB"
    temp_str = STR$(total_size / 1048576)
    PRINT "  Total size: " + temp_str + " MB"

    temp_str = STR$(avg_file_size)
    PRINT "  Average file size: " + temp_str + " bytes"

    PRINT "  Largest directory: "
    IF largest_idx = 0 THEN PRINT "\Project"
    IF largest_idx = 1 THEN PRINT "\Project\src"
    IF largest_idx = 2 THEN PRINT "\Project\docs"
    IF largest_idx = 3 THEN PRINT "\Project\data"
    IF largest_idx = 4 THEN PRINT "\Project\data\input"
    IF largest_idx = 5 THEN PRINT "\Project\data\output"
    IF largest_idx = 6 THEN PRINT "\Project\build"
    IF largest_idx = 7 THEN PRINT "\Project\lib"
    PRINT " (" + STR$(largest_file / 1024) + " KB)"

    ' Sort by size for top consumers
    i = 0
    WHILE i < dir_count
        j = i + 1
        WHILE j < dir_count
            IF dir_sizes(j) > dir_sizes(i) THEN
                temp_d = dir_sizes(i)
                dir_sizes(i) = dir_sizes(j)
                dir_sizes(j) = temp_d
                temp_val = dir_files(i)
                dir_files(i) = dir_files(j)
                dir_files(j) = temp_val
            END IF
            j = j + 1
        WEND
        i = i + 1
    WEND

    PRINT ""
    PRINT "Top consumers:"
    i = 0
    WHILE i < 3 AND i < dir_count
        temp_str = STR$(dir_sizes(i) / 1024)
        PRINT "  " + STR$(i + 1) + ". "
        IF i = 0 THEN
            IF dir_sizes(0) >= 81920 THEN PRINT "\Project\build"
            IF dir_sizes(0) >= 51200 AND dir_sizes(0) < 81920 THEN PRINT "\Project"
            IF dir_sizes(0) >= 32768 AND dir_sizes(0) < 51200 THEN PRINT "\Project\lib"
            IF dir_sizes(0) >= 25600 AND dir_sizes(0) < 32768 THEN PRINT "\Project\src"
            IF dir_sizes(0) < 25600 THEN PRINT "\Project\docs"
        END IF
        IF i = 1 THEN
            IF dir_sizes(1) >= 51200 THEN PRINT "\Project"
            IF dir_sizes(1) >= 32768 AND dir_sizes(1) < 51200 THEN PRINT "\Project\lib"
            IF dir_sizes(1) >= 25600 AND dir_sizes(1) < 32768 THEN PRINT "\Project\src"
            IF dir_sizes(1) >= 15360 AND dir_sizes(1) < 25600 THEN PRINT "\Project\docs"
            IF dir_sizes(1) < 15360 THEN PRINT "\Project\data"
        END IF
        IF i = 2 THEN
            IF dir_sizes(2) >= 32768 THEN PRINT "\Project\lib"
            IF dir_sizes(2) >= 25600 AND dir_sizes(2) < 32768 THEN PRINT "\Project\src"
            IF dir_sizes(2) >= 15360 AND dir_sizes(2) < 25600 THEN PRINT "\Project\docs"
            IF dir_sizes(2) < 15360 THEN PRINT "\Project\data"
        END IF
        PRINT " (" + temp_str + " KB)"
        i = i + 1
    WEND

    ' Cumulative analysis
    PRINT ""
    PRINT "Cumulative analysis:"
    cumulative_size = 0
    i = 0
    WHILE i < dir_count
        cumulative_size = cumulative_size + dir_sizes(i)
        temp_str = STR$(cumulative_size * 100 / total_size)
        IF i = 0 THEN PRINT "\Project\build"
        IF i = 1 THEN PRINT "\Project"
        IF i = 2 THEN PRINT "\Project\lib"
        IF i = 3 THEN PRINT "\Project\src"
        IF i = 4 THEN PRINT "\Project\docs"
        IF i = 5 THEN PRINT "\Project\data"
        IF i = 6 THEN PRINT "\Project\data\output"
        IF i = 7 THEN PRINT "\Project\data\input"
        PRINT ": " + STR$(dir_sizes(i) / 1024) + " KB (cum: " + temp_str + "%)"
        i = i + 1
    WEND

    PRINT ""
    PRINT "Disk usage analysis complete."

    FUNCTION = 0
END FUNCTION
