'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,file,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$ = "NALServer1_20081014123912_1.txt"
'outpath$ = "s:\csv\"


if fileExists(pathname$, filename$) then

    open pathname$+filename$ for input as #1

    if eof(#1) = 0 then
    line input #1, linestr$
    line input #1, linestr$
    line input #1, linestr$
    line input #1, linestr$
    line input #1, linestr$
    line input #1, linestr$
    line input #1, linestr$
    line input #1, linestr$
    'this should be the IMEI number
    line input #1, IMEI$
    line input #1, linestr$
    line input #1, gpsdata$
    close #1

    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$

    open outpath$+IMEI$+".csv" for append as #2
    print #2,gpscsv$
    close #2

    else
        close #1
    end if

'    notice "completed conversion on ";pathname$;" ";filename$
else
'    notice "I could not find the file ";pathname$;" ";filename$
end if

end

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

