# Copyright 2017 Russell Anderson, Philip Starkey
#
# This file is part of autoscrub.
#
# autoscrub 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 3 of the License, or
# (at your option) any later version.
#
# autoscrub 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 autoscrub.  If not, see <http://www.gnu.org/licenses/>.

import os
from setuptools import setup, find_packages


# base version
VERSION = '0.7.2'
# version information generated by our GitHub Action (see .github/workflows/release.yml)
CURRENT_TAG = os.environ.get('GHA_AUTOSCRUB_VERSION')
CURRENT_GIT_COMMIT_HASH = os.environ.get('GHA_AUTOSCRUB_GIT_COMMIT')
if CURRENT_TAG is not None:
    print('GitHub Action debug: tag=', CURRENT_TAG)
if CURRENT_GIT_COMMIT_HASH is not None:
    print('GitHub Action debug: git hash=', CURRENT_GIT_COMMIT_HASH)
# Modify the current version if we are building inside our GitHub action
if CURRENT_TAG is not None and CURRENT_TAG and CURRENT_TAG != 'refs/heads/master':
    VERSION = CURRENT_TAG
elif CURRENT_GIT_COMMIT_HASH:
    index = 0
    try:
        import requests
        response = requests.get('https://test.pypi.org/pypi/autoscrub/json', timeout=2)
        data = response.json()
        if response.status_code == 200:
            found = True
            while found:
                # PyPI converts '-' to '.' in version
                if '{version}.dev{index}'.format(version=VERSION, index=index) not in data['releases']:
                    break
                else:
                    index += 1
        else:
            print('GitHub Action debug: Error while getting JSON data from test PyPI. Status code was:', response.status_code)
    except BaseException:
        print('GitHub Action debug: Error while querying PyPI release information. Will guess -dev0 is the correct postfix.')
        pass

    # Despite PyPI converting '-' to '.', we want to use '-' in __version__.py as it matches semver specs.
    VERSION += '-dev{}'.format(index)


# Auto generate a __version__ package for the package to import
with open(os.path.join('autoscrub', '__version__.py'), 'w') as f:
    f.write("__version__ = '%s'\n" % VERSION)

setup(
    name='autoscrub',
    version=VERSION.replace('+', '-'), # PyPI can't handle local versions, but we want something like this for TestPyPI
    description='Hastens silent intervals of videos using FFmpeg',
    url='https://github.com/philipstarkey/autoscrub',
    license='GPLv3',
    author='Russell Anderson, Philip Starkey',
    classifiers=['Development Status :: 4 - Beta',
                 'Programming Language :: Python :: 2.7',
                 'Programming Language :: Python :: 3.4',
                 'Programming Language :: Python :: 3.5',
                 'Programming Language :: Python :: 3.6',
                 'Environment :: Console',
                 'Intended Audience :: Education',
                 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
                 'Natural Language :: English',
                ],
    python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'click',
        'six',
        'requests',
        'subprocess32;python_version<"3.2"',
    ],
    entry_points='''
        [console_scripts]
        autoscrub=autoscrub.scripts.cli:cli
    ''',
)