#!/usr/bin/env lua5.1
--
-- Released under the terms of GPLv3 or at your option any later version.
-- No warranties.
-- Copyright Enrico Tassi <gares@fettunta.org>

-- utils
function normalize(x)
	return x:gsub("^['\"]",''):gsub("[/'\"]$",'')
end

function escape(x)
	return x:gsub('[]%%%.-]',function(x) return '%'..x end)
end

-- argument parsing
local mode, dir, endpoint
mode = 'default'
local i = 1
while i <= select('#',...) do
	local cur = select(i,...)
	if cur == '-m' then
		i = i + 1
		mode = select(i,...)
	elseif cur == '-d' then
		i = i + 1
		dir = select(i,...)
	else
		if endpoint ~= nil then
			print 'ERROR'
			print 'smd-translate: too many arguments'
			os.exit(1)
		end
		endpoint = cur
	end
	i = i + 1
end

-- argument checking
if endpoint == nil then
	print 'ERROR'
	print('smd-translate: No endpoint specified')
	os.exit(1)
end
if dir ~= 'LR' and dir ~= 'RL' then
	print 'ERROR'
	print('smd-translate: Direction '..(dir or 'nil')..
		' passed with -d is not LR nor R')
	os.exit(1)
end

-- config file parsing
conf=os.getenv'HOME'..'/.smd/config.'..endpoint
conf_f = io.open(conf)
if conf_f == nil then
	print 'ERROR'
	print('smd-translate: No configuration file for endpoint ' ..
		(endpoint or 'nil'))
	os.exit(1)
end
local conf_tbl = {}
for k, v in conf_f:read('*a'):gmatch('%s*(%S+)%s*=%s*(%S+)') do
	conf_tbl[k]=v
end
conf_f:close()

mailbox_local = conf_tbl['MAILBOX_LOCAL'] or conf_tbl['MAILBOX']
mailbox_local = normalize(mailbox_local)
mailbox_remote = conf_tbl['MAILBOX_REMOTE'] or conf_tbl['MAILBOX']
mailbox_remote = normalize(mailbox_remote)

-- modes
modes = {}
modes['oimap-dovecot'] = function(dir)
	if dir == 'LR' then
		local l_ml = '^'..escape(mailbox_local)..'/'
		local r_ml_dot = mailbox_remote..'/.'
		local r_ml = mailbox_remote..'/'
		local mc = mailbox_local..'/cur'
		local mn = mailbox_local..'/new'
		local mt = mailbox_local..'/tmp'
		for l in io.stdin:lines() do
			if l == mc or l == mn or l == mt then
				print((l:gsub(l_ml,r_ml)))
			else
				print((l:gsub(l_ml,r_ml_dot)))
			end
		end
	else
		local e_mr = '^'..escape(mailbox_remote)
		local m_ml = '^'..escape(mailbox_local)..'/%.'
		local dot_ml = mailbox_local..'/'
		for l in io.stdin:lines() do
			print((l:gsub(e_mr,mailbox_local):gsub(m_ml,dot_ml)))
		end
	end
end
modes['nodots'] = function(dir)
	if dir == 'LR' then
		local l_ml = '^'..escape(mailbox_local)..'/'
		local l_mlc = '^'..escape(mailbox_local)..'/(.*)/(cur)'
		local l_mln = '^'..escape(mailbox_local)..'/(.*)/(new)'
		local l_mlt = '^'..escape(mailbox_local)..'/(.*)/(tmp)'
		local r_ml_dot = mailbox_remote..'/.'
		local r_ml = mailbox_remote..'/'
		local mc = mailbox_local..'/cur'
		local mn = mailbox_local..'/new'
		local mt = mailbox_local..'/tmp'
		for l in io.stdin:lines() do
			if l == mc or l == mn or l == mt then
				print((l:gsub(l_ml,r_ml)))
			else
				local f = function(path, last)
					return r_ml_dot..(path:gsub('/','.'))..'/'..last end
				print((l:gsub(l_mlc,f):gsub(l_mln,f):gsub(l_mlt,f)))
			end
		end
	else
		local e_mr = '^'..escape(mailbox_remote)..'(/.*)'
		local dot_ml = mailbox_local
		for l in io.stdin:lines() do
			print((l:gsub(e_mr,function(cap) 
				return dot_ml..(cap:gsub('%.','/'):gsub('^//','/')) end)))
		end
	end

end
modes['cat'] = function(_) for l in io.stdin:lines() do print(l) end end
modes['default'] = modes['oimap-dovecot']

-- run
modes[mode](dir, f,s,init)

-- vim:set ts=4:
