#!/usr/bin/env moon
arg_spec = {
	{"target", "REMOTE_MACHINE", "remote"}
	{"fwd", "FORWARD_PORTS", "1313,3000,4800:8000,8080,1234,4400:4000,40080:80,40443:443"}
	{"sargs", "SSH_ARGS", "-XY"}
}
args = {v[1], os.getenv(v[2]) or arg[i] or v[3] for i, v in ipairs(arg_spec)}
ports = {a, (if b == "" then a else b) for a, b in args.fwd\gmatch "(%d+):?(%d*),?"}
pargs = table.concat ["-L #{k}:localhost:#{v}" for k,v in pairs ports], " "
for k,v in pairs ports do print "Forwarding localhost:#{k} to #{args.target}:#{v}"
os.execute "ssh -N #{args.target} #{pargs} #{args.sargs}"