# coding:utf-8

import os
import sys
import platform
from setuptools import setup, find_packages

config_sample = '''
qy_access_key_id: 'QINGCLOUDACCESSKEYID'
qy_secret_access_key: 'QINGCLOUDSECRETACCESSKEYEXAMPLE'
zone: 'pek1'
'''

def is_windows():
    return platform.system().lower() == 'windows'

def prepare_config_file():
    config_file = os.path.expanduser('~/.qingcloud/config.yaml')
    if os.path.exists(config_file):
        return

    d = os.path.dirname(config_file)
    if not os.path.exists(d):
        os.makedirs(d)

    with open(config_file, 'w') as fd:
        fd.write(config_sample)

def setup_qingcloud_completer():
    # only support linux
    if is_windows():
        return

    cmd = 'complete -C qingcloud_completer qingcloud'
    complete_file = '/etc/bash_completion.d/qingcloud-cli'
    if not os.path.exists(os.path.dirname(complete_file)):
        with open(os.path.expanduser('~/.bash_profile'), 'a') as fd:
            fd.write('\n\n# QingCloud CLI\n%s\n' % cmd)
    else:
        with open((complete_file), 'w') as fd:
            fd.write(cmd)


if len(sys.argv) < 2 or sys.argv[1] != 'install':
    bin_scripts = ['bin/qingcloud', 'bin/qingcloud.cmd', 'bin/qingcloud_completer']
elif is_windows():
    bin_scripts = ['bin/qingcloud.cmd']
else:
    bin_scripts = ['bin/qingcloud', 'bin/qingcloud_completer']

setup(
    name = 'qingcloud-cli',
    version = '0.9.3',
    description = 'Command Line Interface for QingCloud.',
    long_description = open('README.rst', 'rb').read().decode('utf-8'),
    keywords = 'qingcloud iaas cli',
    author = 'Yunify Team',
    author_email = 'simon@yunify.com',
    url = 'https://docs.qingcloud.com/cli/',
    scripts=bin_scripts,
    packages = find_packages('.'),
    package_dir = {'qingcloud-cli': 'qingcloud'},
    include_package_data = True,
    install_requires = [
        'argparse>=1.1',
        'PyYAML>=3.1',
        'qingcloud-sdk>=0.6',
    ]
)

prepare_config_file()

setup_qingcloud_completer()

@@ -1935,16 +1935,72 @@
    %5D%0A)%0A%0A
+if len(sys.argv) %3E= 2 and sys.argv%5B1%5D == 'install':%0A    
 prepare_
@@ -2013,17 +2013,20 @@
 _file()%0A
-%0A
+    
 setup_qi

Prepare and setup only when install.