'notes on how to use this program
'gps2csv reads the gps report file from the nal server for trackers program
'and compiles each IMEI in to a single csv file.  An ouput file
'is created based upon the IMEI number with a .csv extension
'if it already exists it is appended
'
'gps2csv,path to file of file names, name of file of filenames,outpath
'
'nomainwin
dim info$(10, 10)
dim SBdata(30)

pathname$ = word$(CommandLine$,2, ",")
filename$ = word$(CommandLine$,3, ",")
outpath$ = word$(CommandLine$,4, ",")

'pathname$ = "s:\incoming\"
'filename$ = "list.gps"
'outpath$ = "s:\test\"

if fileExists(pathname$, filename$) then

    open pathname$+filename$ for input as #1
    numofreports=0
    while eof(#1) = 0
     line input #1, gpsfilename$
     numofreports=numofreports+1
     print "Processing file ";numofreports;": ";gpsfilename$
     errreport = readreport(pathname$,gpsfilename$, outpath$)
    wend
    close #1
    print "Processed ";numofreports
    input "Press enter to complete";done
end if
end

function readreport(pathname$, filename$, outpath$)

if fileExists(pathname$, filename$) then

    open pathname$+filename$ for input as #2

    if eof(#2) = 0 then
    line input #2, linestr$
    line input #2, linestr$
    line input #2, linestr$
    line input #2, linestr$
    line input #2, linestr$
    line input #2, linestr$
    line input #2, linestr$
    line input #2, linestr$
    'this should be the IMEI number
    line input #2, IMEI$
    line input #2, linestr$
    line input #2, gpsdata$
    close #2

    gpstime$=word$(gpsdata$,1,",")
    gpsdate$=word$(gpsdata$,2,",")
    lat$=word$(gpsdata$,3,",")
    latlen=len(lat$)
    latdeg$=word$(lat$,1,":")
    latdeglen=len(latdeg$)+2
    latmin$=mid$(lat$,latdeglen,(latlen-latdeglen))
    latdeg = val(latdeg$)
    latmin = val(latmin$)
    latdeg = latdeg + (latmin/60)
    lng$=word$(gpsdata$,4,",")
    lnglen=len(lng$)
    lngdeg$=word$(lng$,1,":")
    lngdeglen=len(lngdeg$)+2
    lngmin$=mid$(lng$,lngdeglen,(lnglen-lngdeglen))
    lngdeg = val(lngdeg$)
    lngmin = val(lngmin$)
    lngdeg = lngdeg + (lngmin/60)
    gpsalt$=word$(gpsdata$,5,",")
    gpspdir$=word$(gpsdata$,6,",")
    gpsvel$=word$(gpspdir$,1,":")
    gpsdir$=word$(gpspdir$,2,":")
    gpsvervel$=word$(gpsdata$,7,",")
    gpssats$=word$(gpsdata$,8,",")
    gpscsv$=gpsdate$+","+gpstime$+","+str$(latdeg)+","+str$(lngdeg)+","+gpsalt$+","+gpsvel$+","+gpsdir$+","+gpsvervel$+","+gpssats$

    print "   Writing to file: ";outpath$+IMEI$+".csv"
    open outpath$+IMEI$+".csv" for append as #3
    print #3,gpscsv$
    close #3
    readreport = 0
    else
        close #2
        readreport = -1
    end if

'    notice "completed conversion on ";pathname$;" ";filename$
else
'    notice "I could not find the file ";pathname$;" ";filename$
end if

end function

function fileExists(path$, filename$)

  'dimension the array info$( at the beginning of your program

  files path$, filename$, info$()

  fileExists = val(info$(0, 0))  'non zero is true

end function

