' File Copy — copies file contents using OPEN/BGET/PUT with hardcoded file numbers
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL srcFile AS STRING
    LOCAL dstFile AS STRING
    LOCAL buffer(10000) AS LONG
    LOCAL bytesRead AS LONG
    LOCAL totalBytes AS LONG
    LOCAL fileSize AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL success AS LONG
    LOCAL bufSize AS LONG
    LOCAL chunkCount AS LONG
    LOCAL lineStr AS STRING
    srcFile = "test_source.dat"
    dstFile = "test_copy.dat"
    PRINT "============================================"
    PRINT "          FILE COPY UTILITY"
    PRINT "============================================"
    PRINT ""
    PRINT "Creating test source file..."
    PRINT ""
    OPEN srcFile FOR OUTPUT AS #1
    FOR i = 0 TO 99
        PRINT #1, "Line " + STR$(i) + ": The quick brown fox jumps over the lazy dog."
    NEXT i
    CLOSE #1
    PRINT "Source file created: " + srcFile
    PRINT ""
    OPEN srcFile FOR BINARY AS #1
    fileSize = LOF(1)
    PRINT "Source file size: " + STR$(fileSize) + " bytes"
    CLOSE #1
    PRINT ""
    PRINT "Copying file..."
    PRINT ""
    OPEN srcFile FOR BINARY AS #1
    OPEN dstFile FOR OUTPUT AS #2
    IF LOF(1) > 0 THEN
        totalBytes = 0
        DO WHILE totalBytes < LOF(1)
            GET #1, totalBytes + 1, buffer(0)
            PUT #2, totalBytes + 1, buffer(0)
            totalBytes = totalBytes + 1
        LOOP
    END IF
    CLOSE #1
    CLOSE #2
    PRINT "Copy complete: " + STR$(totalBytes) + " bytes copied"
    PRINT ""
    OPEN dstFile FOR BINARY AS #1
    fileSize = LOF(1)
    IF fileSize = totalBytes THEN
        PRINT "File sizes match: " + STR$(fileSize) + " bytes"
        success = 1
    ELSE
        PRINT "File size mismatch!"
        success = 0
    END IF
    CLOSE #1
    PRINT ""
    PRINT "============================================"
    PRINT "      VERIFYING FILE CONTENT"
    PRINT "============================================"
    PRINT ""
    OPEN srcFile FOR INPUT AS #1
    OPEN dstFile FOR INPUT AS #2
    FOR i = 1 TO 5
        LINE INPUT #1, lineStr
        PRINT "Source line " + STR$(i) + ": " + MID$(lineStr, 1, 50)
    NEXT i
    CLOSE #1
    PRINT ""
    FOR i = 1 TO 5
        LINE INPUT #2, lineStr
        PRINT "Dest line " + STR$(i) + ": " + MID$(lineStr, 1, 50)
    NEXT i
    CLOSE #2
    PRINT ""
    PRINT "============================================"
    PRINT "      BINARY FILE COPY (with buffer)"
    PRINT "============================================"
    PRINT ""
    OPEN srcFile FOR BINARY AS #1
    OPEN dstFile FOR OUTPUT AS #2
    fileSize = LOF(1)
    totalBytes = 0
    bufSize = 100
    DO WHILE totalBytes < fileSize
        chunkCount = bufSize
        IF totalBytes + chunkCount > fileSize THEN
            chunkCount = fileSize - totalBytes
        END IF
        FOR i = 0 TO chunkCount - 1
            GET #1, totalBytes + i + 1, buffer(i)
        NEXT i
        FOR i = 0 TO chunkCount - 1
            PUT #2, totalBytes + i + 1, buffer(i)
        NEXT i
        totalBytes = totalBytes + chunkCount
    LOOP
    CLOSE #1
    CLOSE #2
    PRINT "Chunk copy complete: " + STR$(totalBytes) + " bytes"
    PRINT ""
    PRINT "============================================"
    PRINT "      FILE SIZE COMPARISON"
    PRINT "============================================"
    PRINT ""
    OPEN srcFile FOR BINARY AS #1
    OPEN dstFile FOR BINARY AS #2
    IF LOF(1) = LOF(2) THEN
        PRINT "PASS: Both files are " + STR$(LOF(1)) + " bytes"
    ELSE
        PRINT "FAIL: Source=" + STR$(LOF(1)) + " Dest=" + STR$(LOF(2))
    END IF
    CLOSE #1
    CLOSE #2
    PRINT ""
    KILL srcFile
    KILL dstFile
    PRINT "Cleanup complete"
    PRINT ""
    PRINT "File copy operations complete"
    FUNCTION = 0
END FUNCTION
