#!/usr/bin/env python3

import os.path
import subprocess
import sys

if os.environ.get('TRAVIS_PULL_REQUEST', 'false') == 'true':
    print('Not deploying images for pull requests')
    exit(0)

version = os.environ.get('CIRCLE_BRANCH', os.environ.get('TRAVIS_BRANCH'))

if version not in os.environ.get('HELM_RELEASE_NAMES', '').split(','):
    print('Not a release branch')
    exit(0)

print('Deploying images to Docker Hub for tag {}'.format(version))
sys.stdout.flush()
subprocess.check_call([
    'docker', 'login',
    '--username=' + os.environ['DOCKER_USERNAME'],
    '--password=' + os.environ['DOCKER_PASSWORD'],
])

images = sys.argv[1:]
for image in images:
    full_image = 'camptocamp/{}-{}'.format(os.environ['PROJECT'], image)
    src_image = full_image
    dest_image = '{}:{}'.format(full_image, version)
    subprocess.check_call(['docker', 'tag', src_image, dest_image])
    subprocess.check_call(['docker', 'push', dest_image])


openshift_version = '3.11.0'
openshift_hash = '0cbc58b'
openshift_version_name = 'openshift-origin-client-tools-v{}-{}-linux-64bit'.format(
    openshift_version, openshift_hash
)
openshift_file = openshift_version_name + '.tar.gz'
openshift_url = 'https://github.com/openshift/origin/releases/download/v{}/{}'.format(
    openshift_version, openshift_file
)
subprocess.check_call(['wget', '--quiet', openshift_url], cwd='/tmp')
subprocess.check_call(['tar', 'xfz', openshift_file], cwd='/tmp')
oc = '/tmp/{}/oc'.format(openshift_version_name)

subprocess.check_call([
    oc, 'login', os.environ['OPENSHIFT_URL'], '--token=' + os.environ['OPENSHIFT_TOKEN'],
])
for image in images:
    openshift_image_ref = '{}-c2cgeoportal-{}:{}'.format(version, image, version)
    subprocess.check_call([
        oc, 'import-image', openshift_image_ref, '--scheduled=true', '--reference-policy=local',
        '--namespace=' + os.environ['OPENSHIFT_PROJECT']
    ])
