' File Move — rename and move files with error handling, hardcoded file numbers
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL srcFile AS STRING
    LOCAL dstFile AS STRING
    LOCAL tempFile AS STRING
    LOCAL i AS LONG
    LOCAL success AS LONG
    LOCAL errorCode AS LONG
    LOCAL lineStr AS STRING
    LOCAL lineContent AS STRING
    LOCAL fileA AS STRING
    LOCAL fileB AS STRING
    LOCAL fileC AS STRING
    PRINT "============================================"
    PRINT "          FILE MOVE / RENAME UTILITY"
    PRINT "============================================"
    PRINT ""
    srcFile = "test_move_source.txt"
    dstFile = "test_move_dest.txt"
    tempFile = "test_move_temp.txt"
    PRINT "Creating source file..."
    PRINT ""
    OPEN srcFile FOR OUTPUT AS #1
    FOR i = 0 TO 49
        lineStr = "This is line " + STR$(i) + " of the source file for testing."
        PRINT #1, lineStr
    NEXT i
    CLOSE #1
    PRINT "Created: " + srcFile
    OPEN srcFile FOR INPUT AS #1
    PRINT "Source file size: " + STR$(LOF(1))
    CLOSE #1
    PRINT ""
    PRINT "============================================"
    PRINT "      MOVE (RENAME) OPERATION"
    PRINT "============================================"
    PRINT ""
    PRINT "Moving " + srcFile + " -> " + dstFile
    PRINT ""
    NAME srcFile AS dstFile
    errorCode = ERR
    IF errorCode = 0 THEN
        PRINT "Move successful"
        success = 1
        OPEN dstFile FOR INPUT AS #1
        PRINT "Destination file exists: " + STR$(LOF(1)) + " bytes"
        CLOSE #1
    ELSE
        PRINT "Move failed with error code: " + STR$(errorCode)
        success = 0
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      VERIFY SOURCE IS GONE"
    PRINT "============================================"
    PRINT ""
    OPEN srcFile FOR INPUT AS #1
    PRINT "Source file still exists!"
    CLOSE #1
    PRINT ""
    PRINT "============================================"
    PRINT "      MOVE BACK TO ORIGINAL NAME"
    PRINT "============================================"
    PRINT ""
    PRINT "Moving back: " + dstFile + " -> " + srcFile
    NAME dstFile AS srcFile
    errorCode = ERR
    IF errorCode = 0 THEN
        PRINT "Move back successful"
        OPEN srcFile FOR INPUT AS #1
        PRINT "Source file restored: " + STR$(LOF(1)) + " bytes"
        CLOSE #1
    ELSE
        PRINT "Move back failed with error code: " + STR$(errorCode)
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      MOVE TO TEMPORARY NAME"
    PRINT "============================================"
    PRINT ""
    PRINT "Moving: " + srcFile + " -> " + tempFile
    NAME srcFile AS tempFile
    errorCode = ERR
    IF errorCode = 0 THEN
        PRINT "Move to temp successful"
        OPEN tempFile FOR INPUT AS #1
        PRINT "Temp file exists: " + STR$(LOF(1)) + " bytes"
        CLOSE #1
    ELSE
        PRINT "Move to temp failed: " + STR$(errorCode)
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      VERIFY CONTENT INTEGRITY"
    PRINT "============================================"
    PRINT ""
    OPEN tempFile FOR INPUT AS #1
    PRINT "Reading first 5 lines from moved file:"
    PRINT ""
    FOR i = 1 TO 5
        LINE INPUT #1, lineContent
        PRINT "  " + lineContent
    NEXT i
    CLOSE #1
    PRINT ""
    PRINT "============================================"
    PRINT "      OVERWRITE PROTECTION TEST"
    PRINT "============================================"
    PRINT ""
    OPEN srcFile FOR OUTPUT AS #1
    PRINT #1, "This file should not be overwritten by move"
    CLOSE #1
    PRINT "Trying to move temp file to existing source file..."
    NAME tempFile AS srcFile
    errorCode = ERR
    IF errorCode = 0 THEN
        PRINT "Overwrite allowed (some systems allow this)"
    ELSE
        PRINT "Overwrite prevented (error " + STR$(errorCode) + ")"
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      MULTIPLE MOVE OPERATIONS"
    PRINT "============================================"
    PRINT ""
    fileA = "test_a.txt"
    fileB = "test_b.txt"
    fileC = "test_c.txt"
    PRINT "Creating multiple files for rename test..."
    OPEN fileA FOR OUTPUT AS #1
    PRINT #1, "File A content"
    CLOSE #1
    OPEN fileB FOR OUTPUT AS #1
    PRINT #1, "File B content"
    CLOSE #1
    PRINT "Renaming " + fileA + " -> " + fileC
    NAME fileA AS fileC
    PRINT "Renaming " + fileB + " -> " + fileA
    NAME fileB AS fileA
    PRINT "Renaming " + fileC + " -> " + fileB
    NAME fileC AS fileB
    PRINT ""
    PRINT "Verifying cyclic rename..."
    OPEN fileA FOR INPUT AS #1
    LINE INPUT #1, lineContent
    PRINT "File A now contains: " + lineContent
    CLOSE #1
    OPEN fileB FOR INPUT AS #1
    LINE INPUT #1, lineContent
    PRINT "File B now contains: " + lineContent
    CLOSE #1
    PRINT ""
    PRINT "Cleaning up all test files..."
    KILL srcFile
    KILL tempFile
    KILL fileA
    KILL fileB
    KILL fileC
    PRINT "Cleanup complete"
    PRINT ""
    PRINT "File move operations complete"
    FUNCTION = 0
END FUNCTION
