#!/usr/bin/python2 -OO

# Copyright (C) 2013 W. Trevor King <wking@tremily.us>
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

"""Catalyst is a release building tool used by Gentoo Linux"""

# py2.6 compatibility
from __future__ import print_function

import codecs as _codecs
from distutils.core import setup as _setup, Command as _Command
import os as _os

from catalyst import __version__
from catalyst.version import set_release_version as _set_release_version
from catalyst.version import get_version as _get_version


_this_dir = _os.path.dirname(__file__)
package_name = 'catalyst'
tag = '{0}-{1}'.format(package_name, __version__)


if _os.path.sep != '/':
	raise NotImplementedError('Non-POSIX paths are not supported')

def files(root, target):
	"""Iterate through all the file paths under `root`

	Distutils wants all paths to be written in the Unix convention
	(i.e. slash-separated) [1], so that's what we'll do here.

	[1]: http://docs.python.org/2/distutils/setupscript.html#writing-the-setup-script
	"""
	for dirpath, dirnames, filenames in _os.walk(root):
		key = _os.path.join(target, dirpath)
		filepaths = [_os.path.join(dirpath, filename)
		             for filename in filenames]
		yield (key, filepaths)


_data_files = [('/etc/catalyst', ['etc/catalyst.conf','etc/catalystrc']),
	('/usr/share/man/man1', ['files/catalyst.1']),
	('/usr/share/man/man5', ['files/catalyst-config.5', 'files/catalyst-spec.5'])
	]
_data_files.extend(files('livecd', 'lib/catalyst/'))
_data_files.extend(files('targets', 'lib/catalyst/'))


class set_version(_Command):
	'''Saves the specified release version information
	'''
	global __version__
	description = "hardcode script's version using VERSION from environment"
	user_options = []  # [(long_name, short_name, desc),]

	def initialize_options (self):
		pass

	def finalize_options (self):
		pass

	def run(self):
		try:
			version = _os.environ['VERSION']
		except KeyError:
			print("Try setting 'VERSION=x.y.z' on the command line... Aborting")
			return
		_set_release_version(version)
		__version__ = _get_version()
		print("Version set to:\n", __version__)


_setup(
	name=package_name,
	version=__version__,
	maintainer='Gentoo Release Engineering',
	maintainer_email='releng@gentoo.org',
	url='http://www.gentoo.org/proj/en/releng/{0}/'.format(package_name),
	download_url='http://git.overlays.gentoo.org/gitweb/?p=proj/{0}.git;a=snapshot;h={1};sf=tgz'.format(package_name, tag),
	license='GNU General Public License (GPL)',
	platforms=['all'],
	description=__doc__,
	long_description=_codecs.open(
		_os.path.join(_this_dir, 'README'), 'r', 'utf-8').read(),
	classifiers=[
		'Development Status :: 5 - Production/Stable',
		'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
		'Intended Audience :: System Administrators',
		'Operating System :: POSIX',
		'Topic :: System :: Archiving :: Packaging',
		'Topic :: System :: Installation/Setup',
		'Topic :: System :: Software Distribution',
		'Programming Language :: Python :: 2',
		'Programming Language :: Python :: 2.6',
		'Programming Language :: Python :: 2.7',
		],
	scripts=['bin/{0}'.format(package_name)],
	packages=[
		package_name,
		'{0}.arch'.format(package_name),
		'{0}.base'.format(package_name),
		'{0}.targets'.format(package_name),
		],
	data_files=_data_files,
	provides=[package_name],
	cmdclass={
		'set_version': set_version
		},
	)
