#COMPILE EXE
#DIM NONE

FUNCTION PBMAIN () AS LONG

    PRINT "Simple echo instrument list parser v1.0.0.0.0.0.1a super plus deluxe"

    IF COMMAND$ = "" THEN
        PRINT "USAGE: <infile> <outdir>"
        PRINT ""
        PRINT "all files have to be in the instrument list file's dir."
        EXIT FUNCTION
    END IF

    outdir$ = COMMAND$(2)
    infile$ = COMMAND$(1)

    DIM flabel$(0 TO 255)
    DIM fnames$(0 TO 255)

    OPEN outdir$ + "\echolist.68k" FOR OUTPUT AS #2

    PRINT #2, "; Echo Pointer List for " + infile$ + " :) "

    OPEN infile$ FOR INPUT AS #1

    DIM p AS INTEGER

    MKDIR outdir$

    WHILE NOT EOF(1)

        LINE INPUT #1, a$
        ftyp$ = spleft(a$)
        fnam$ = MID$(a$, LEN(ftyp$)+2)

        lblnam$ = MID$(fnam$,1,LEN(fnam$)-4)
        REPLACE " " WITH "_" IN lblnam$
        lblnam$ = LCASE$(lblnam$)

        flabel$(p) = lblnam$


        SELECT CASE ftyp$

            CASE "vgi"
                fnames$(p) = CHR$(34) + outdir$ + "\" + MID$(fnam$,1,LEN(fnam$)-4) + ".eif" + CHR$(34)
                shl$ = "tool\vgi2eif " + CHR$(34) + fnam$ + CHR$(34) + " " + fnames$(p)
                PRINT shl$
                SHELL shl$
                PRINT #2, "    EchoListEntry " + lblnam$ + " ; $" + HEX$(p)

            CASE "nul"
                PRINT "NULL INSTRUMENT..."
                PRINT #2, "    EchoListEntry 0 ; $" + HEX$(p) + " Empty/unused"

            CASE "wav"
                KILL "tmp.raw"
                shl$ = "tool\sox " + CHR$(34) + fnam$ + CHR$(34) + " -e unsigned -b 8 tmp.raw channels 1 rate 10000"
                PRINT shl$
                SHELL shl$
                fnames$(p) = CHR$(34) + outdir$ + "\" + MID$(fnam$,1,LEN(fnam$)-4) + ".ewf" + CHR$(34)
                shl$ = "tool\pcm2ewf tmp.raw " + fnames$(p)
                PRINT SHL$
                SHELL SHL$
                PRINT #2, "    EchoListEntry " + lblnam$ + " ; $" + HEX$(p)
                KILL "tmp.raw"
            CASE "wat"
                KILL "tmp.raw"
                shl$ = "tool\sox " + CHR$(34) + fnam$ + CHR$(34) + " -e unsigned -b 8 tmp.raw channels 1 rate 8000"
                PRINT shl$
                SHELL shl$
                fnames$(p) = CHR$(34) + outdir$ + "\" + MID$(fnam$,1,LEN(fnam$)-4) + ".ewf" + CHR$(34)
                shl$ = "tool\pcm2ewf tmp.raw " + fnames$(p)
                PRINT SHL$
                SHELL SHL$
                PRINT #2, "    EchoListEntry " + lblnam$ + " ; $" + HEX$(p)
                KILL "tmp.raw"
            CASE "ewf"
                fnames$(p) = CHR$(34) + outdir$ + "\" + fnam$ + CHR$(34)
                FILECOPY fnam$, outdir$ + "\" + fnam$
                PRINT "EWF File " + fnames$(p)
                PRINT #2, "    EchoListEntry " + lblnam$ + " ; $" + HEX$(p)
            CASE "pcm"
                fnames$(p) = CHR$(34) + outdir$ + "\" + MID$(fnam$,1,LEN(fnam$)-4) + ".ewf" + CHR$(34)
                shl$ = "tool\pcm2ewf " + CHR$(34) + fnam$ + CHR$(34) + " " +fnames$(p)
                PRINT SHL$
                SHELL SHL$
                PRINT #2, "    EchoListEntry " + lblnam$ + " ; $" + HEX$(p)
            CASE "tfi"
                fnames$(p) = CHR$(34) + outdir$ + "\" + MID$(fnam$,1,LEN(fnam$)-4) + ".eif" + CHR$(34)
                shl$ = "tool\tfi2eif " + CHR$(34) + fnam$ + CHR$(34) + " " + fnames$(p)
                PRINT shl$
                SHELL shl$
                PRINT #2, "    EchoListEntry " + lblnam$ + " ; $" + HEX$(p)


            CASE "eef"
                fnames$(p) = CHR$(34) + outdir$ + "\" + fnam$ + CHR$(34)
                FILECOPY fnam$, outdir$ + "\" + fnam$
                PRINT "EEF File " + fnames$(p)
                PRINT #2, "    EchoListEntry " + lblnam$ + " ; $" + HEX$(p)
            CASE "eif"
                fnames$(p) = CHR$(34) + outdir$ + "\" + fnam$ + CHR$(34)
                FILECOPY fnam$, outdir$ + "\" + fnam$
                PRINT "EIF File " + fnames$(p)
                PRINT #2, "    EchoListEntry " + lblnam$ + " ; $" + HEX$(p)
        END SELECT
        p = p +1
    WEND

    DIM i AS INTEGER

    PRINT #2, ""
    PRINT #2, ""

    FOR i = 0 TO p-1

        IF fnames$(i) <> "" THEN
            PRINT #2, flabel$(i) + ":"
            PRINT #2, "   incbin " + fnames$(i)
            PRINT #2, "   even"
        END IF
    NEXT i

    CLOSE


END FUNCTION

FUNCTION SPLEFT(strn AS STRING) AS STRING
    DIM i AS LONG
    DIM car AS STRING
    DIM cars AS STRING
    DIM comp AS STRING
    DIM speft AS STRING

    i = 1
    car = MID$(strn, 1, 1)
    IF car = CHR$(34) THEN
        comp = CHR$(34)
        i = 2
        car = MID$(strn, 2, 1)
    ELSE
        comp = " "
    END IF
    WHILE car <> comp
          SPEFT = SPEFT + car
          i = i + 1
          car = MID$(strn, i, 1)
          IF i > LEN(strn) THEN
              SPLEFT = SPEFT + cars
              EXIT FUNCTION
          END IF
    WEND
              SPLEFT = SPEFT
END FUNCTION


FUNCTION param(strn AS STRING, b AS LONG) AS STRING
    DIM i AS LONG
    DIM c AS LONG
    DIM car AS STRING
    DIM cars AS STRING
    DIM comp AS STRING
    DIM speft AS STRING

    c = 0
    i = 1
    car = MID$(strn, 1, 1)
    IF car = CHR$(34) THEN
        comp = CHR$(34)
        i = 2
        car = MID$(strn, 2, 1)
    ELSE
        comp = " "
    END IF
    WHILE c <> b
    WHILE car <> comp
          SPEFT = SPEFT + car
          i = i + 1
          car = MID$(strn, i, 1)
          IF i > LEN(strn) THEN
              SPEFT = ""
              param = SPEFT
              EXIT FUNCTION
          END IF
    WEND
   i = i + 1
   c = c + 1
   ' SPEFT$ = MID(strn, i&, 1)
    car = SPEFT
    WEND


    param = RTRIM$(spleft(MID$(strn,i)))

END FUNCTION
