#!/usr/bin/env python

from string import Template
from optparse import OptionParser

cmake_str = """cmake_minimum_required (VERSION 2.8)
project (leechcraft_${plug_lower})
include (InitLCPlugin OPTIONAL)

include_directories (
	$${CMAKE_CURRENT_BINARY_DIR}
	$${Boost_INCLUDE_DIR}
	$${LEECHCRAFT_INCLUDE_DIR}
	)
set (SRCS
	$plug_lower.cpp
	)
#CreateTrs("$plug_lower" "en;ru_RU" COMPILED_TRANSLATIONS)
CreateTrsUpTarget("$plug_lower" "en;ru_RU" "$${SRCS}" "$${FORMS}" "")

add_library (leechcraft_${plug_lower} SHARED
	$${COMPILED_TRANSLATIONS}
	$${SRCS}
	)
target_link_libraries (leechcraft_${plug_lower}
	$${LEECHCRAFT_LIBRARIES}
	)
install (TARGETS leechcraft_${plug_lower} DESTINATION $${LC_PLUGINS_DEST})

FindQtLibs (leechcraft_${plug_lower} Core)
"""

header_str = """/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2014  $author
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#pragma once

#include <QObject>
#include <interfaces/$interfaces_includes.h>

namespace LeechCraft
{
namespace $plug
{
	class Plugin : public QObject
				 , public $interfaces_inherit
	{
		Q_OBJECT
		Q_INTERFACES ($interfaces)

		LC_PLUGIN_METADATA ("org.LeechCraft.$plug")
	public:
$interfaces_decls
	};
}
}
"""

source_str = """/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2014  $author
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "${plug_lower}.h"
#include <QIcon>

namespace LeechCraft
{
namespace $plug
{
$interfaces_defs
}
}

LC_EXPORT_PLUGIN (leechcraft_${plug_lower}, LeechCraft::$plug::Plugin);
"""

decls_all = {}
decls_all ['IInfo'] = """		void Init (ICoreProxy_ptr);
		void SecondInit ();
		QByteArray GetUniqueID () const;
		void Release ();
		QString GetName () const;
		QString GetInfo () const;
		QIcon GetIcon () const;"""
decls_all ['IToolBarEmbedder'] = """				QList<QAction*> GetActions () const;"""

defs_all = {}
defs_all ['IInfo'] = """	void Plugin::Init (ICoreProxy_ptr proxy)
	{
	}

	void Plugin::SecondInit ()
	{
	}

	QByteArray Plugin::GetUniqueID () const
	{
		return "org.LeechCraft.$plug";
	}

	void Plugin::Release ()
	{
	}

	QString Plugin::GetName () const
	{
		return "$plug";
	}

	QString Plugin::GetInfo () const
	{
		return tr ("");
	}

	QIcon Plugin::GetIcon () const
	{
		return QIcon ();
	}"""
defs_all ['IToolBarEmbedder'] = """			QList<QAction*> Plugin::GetActions () const
			{
				QList<QAction*> result;
				return result;
			}"""

parser = OptionParser ()
parser.add_option ('-a', '--author', dest='author')
parser.add_option ('-p', '--plugin', dest='plugin')
parser.add_option ('-i', '--interfaces', dest='interfaces', default=None)
(p, args) = parser.parse_args ()

interfaces_array = []

if p.interfaces != None:
	for iface in p.interfaces.split (','):
		interfaces_array.append (iface)

interfaces_array.insert (0, 'IInfo')

decls_array = ([decls_all [v] for v in interfaces_array])
defs_array = ([defs_all [v] for v in interfaces_array])

interfaces_includes = map (lambda s: s.lower (), interfaces_array)

d = {}
d ['author'] = p.author
d ['plug_upper'] = p.plugin.upper ()
d ['plug_lower'] = p.plugin.lower ()
d ['plug'] = p.plugin
d ['interfaces'] = ' '.join (interfaces_array)
d ['interfaces_inherit'] = '\n						 , public '.join (interfaces_array)
d ['interfaces_includes'] = '.h>\n#include <interfaces/'.join (interfaces_includes)
d ['interfaces_decls'] = '\n\n'.join (decls_array)
d ['interfaces_defs'] = '\n\n'.join (defs_array)

header = Template (header_str)
hfile = open ('%s.h' % p.plugin.lower (), 'w')
hfile.write (header.substitute (d))

source = Template (source_str)
sfile = open ('%s.cpp' % p.plugin.lower (), 'w')
sfile.write (Template (source.substitute (d)).substitute (d))

cmake = Template (cmake_str)
sfile = open ('CMakeLists.txt', 'w')
sfile.write (cmake.substitute (d))
