--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
-- ftp Transfer Test
title = "ftp Test"
-----------------------------------------------------------------------------
-- Little program to download files from URLs
-- LuaSocket sample files
-- Author: Diego Nehab
-----------------------------------------------------------------------------
local socket = require("socket")
local http = require("socket.http")
local ftp = require("socket.ftp")
local url = require("socket.url")
local ltn12 = require("ltn12")

-- formats a number of seconds into human readable form
function nicetime(s)
    local l = "s"
    if s > 60 then
        s = s / 60
        l = "m"
        if s > 60 then
            s = s / 60
            l = "h"
            if s > 24 then
                s = s / 24
                l = "d" -- hmmm
            end
        end
    end
    if l == "s" then return string.format("%5.0f%s", s, l)
    else return string.format("%5.2f%s", s, l) end
end

-- formats a number of bytes into human readable form
function nicesize(b)
    local l = "B"
    if b > 1024 then
        b = b / 1024
        l = "KB"
        if b > 1024 then
            b = b / 1024
            l = "MB"
            if b > 1024 then
                b = b / 1024
                l = "GB" -- hmmm
            end
        end
    end
    return string.format("%7.2f%2s", b, l)
end

-- returns a string with the current state of the download
local remaining_s = "%s received, %s/s throughput, %2.0f%% done, %s remaining"
local elapsed_s =   "%s received, %s/s throughput, %s elapsed                "
function gauge(got, delta, size)
    local rate = got / delta
    if size and size >= 1 then
        return string.format(remaining_s, nicesize(got),  nicesize(rate),
            100*got/size, nicetime((size-got)/rate))
    else
        return string.format(elapsed_s, nicesize(got),
            nicesize(rate), nicetime(delta))
    end
end

-- creates a new instance of a receive_cb that saves to disk
-- kind of copied from luasocket's manual callback examples
function stats(size)
    local start = socket.gettime()
    local last = start
    local got = 0
    return function(chunk)
        -- elapsed time since start
        local current = socket.gettime()
        if chunk then
            -- total bytes received
            got = got + string.len(chunk)   
            -- not enough time for estimate
            if current - last > 1 then
                io.stderr:write("\r", gauge(got, current - start, size))
                io.stderr:flush()
                last = current
            end
        else
            -- close up
            io.stderr:write("\r", gauge(got, current - start), "\n")
        end
        return chunk
    end
end

-- determines the size of a http file
function gethttpsize(u)
    local r, c, h = http.request {method = "HEAD", url = u}
    if c == 200 then
        return tonumber(h["content-length"])
    end
end

-- downloads a file using the http protocol
function getbyhttp(u, file)
	print("getbyhttp");
    local save = ltn12.sink.file(file or io.stdout)
    -- only print feedback if output is not stdout
    if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
    local r, c, h, s = http.request {url = u, sink = save }
    if c ~= 200 then io.stderr:write(s or c, "\n") end
	print(s or c);
end

-- downloads a file using the ftp protocol
function getbyftp(u, file)
	print("getbyftp");
    local save = ltn12.sink.file(file or io.stdout)
    -- only print feedback if output is not stdout
    -- and we don't know how big the file is
    if file then save = ltn12.sink.chain(stats(), save) end
    local gett = url.parse(u)
    gett.sink = save
    gett.type = "i"
	gett.user = "anonymous"
	gett.password = "anonymous"
    local ret, err = ftp.get(gett)
    if err then print(err) end
end

-- determines the scheme
function getscheme(u)
    -- this is an heuristic to solve a common invalid url poblem
    if not string.find(u, "//") then u = "//" .. u end
    local parsed = url.parse(u, {scheme = "http"})
    return parsed.scheme
end

-- gets a file either by http or ftp, saving as <name>
function get(u, name)
    local fout = name and io.open(name, "wb")
	if (name ~= nil) then
		print("getbyhttp - name : " .. name);
		end	
    local scheme = getscheme(u)
    if scheme == "ftp" then getbyftp(u, fout)
    elseif scheme == "http" then getbyhttp(u, fout)
    else print("unknown scheme" .. scheme) end
end

-- main program
function setup() 
-- seems to put the files into the Scripts folder
print("ftp transfer test");
get( "http://www.silogic.com/index.html" , "c:/temp/deleteme.html") 
-- note that the next test file is 5 MB but the ZIP is empty
get( "http://ipv4.download.thinkbroadband.com/5MB.zip" , "c:/temp/deleteme.zip")
print("Done");
return Stop();
end
