-- needs lua5.1-socket

function savepos()
	local name = mp.get_property("filename")
	local path = mp.get_property("path")
	local fn = mp.get_property("stream-open-filename")
	local tpos = mp.get_property("time-pos")
	print(name)
	print(tpos)
	print(path)
	print(fn)
	local host, port = "127.0.0.1", 6789
	local socket = require("socket")
	local tcp = assert(socket.tcp())
	tcp:connect(host, port)
	local data = "save \"" .. name .. "\" " .. tpos .. "\n"
	print("sending: " .. data)
	tcp:send(data)
	tcp:close()
end

function getpos()
	local name = mp.get_property("filename")
	local tpos = mp.get_property("time-pos")
	print(name)
	print(tpos)
	local host, port = "127.0.0.1", 6789
	local socket = require("socket")
	local tcp = assert(socket.tcp())
	tcp:connect(host, port)
	local data = "get \"" .. name .. "\"\n"
	print("sending: " .. data)
	local newpos = ""
	tcp:send(data)
	local newpos = tcp:receive("*l")
	tcp:close()
	print(newpos)
	--print(s)
	--print(status)
	--print(partial)
	mp.set_property("time-pos", newpos)
end

mp.add_key_binding("X", "savepos", savepos)
mp.add_key_binding("C", "getpos", getpos)
