'
' Using BaCon to create small and handy tools.
'
' Create an M3U file for MP3-players
'
' PvE, december 3 2009 - GPL.
'
'-----------------------------------------------------------------

target$ = CONCAT$(MID$(CURDIR$, INSTRREV(CURDIR$, "/") + 1), ".m3u")

OPEN "." FOR DIRECTORY AS curdir

REPEAT
    GETFILE item$ FROM curdir

    IF REGEX(item$, ".*\\.mp3") OR REGEX(item$, ".*\\.ogg") THEN
        list$ = CONCAT$(list$, NL$, item$)
    END IF
UNTIL ISFALSE(LEN(item$))

CLOSE DIRECTORY curdir

PRINT NL$, "Creating file: '", target$, "'"
PRINT NL$, "Contents:", NL$

SPLIT list$ BY NL$ TO nr$ SIZE dim

IF ISFALSE(dim) THEN
    PRINT TAB$(1), "---> No sound files found! <---"
ELSE
    SORT nr$

    OPEN CONCAT$(CURDIR$, "/", target$) FOR WRITING AS ind

    FOR i = 0 TO dim - 1
        WRITELN nr$[i] TO ind
        PRINT TAB$(1), nr$[i]
    NEXT

    CLOSE FILE ind
END IF

PRINT