#!/usr/bin/env python

# tile-generator
#
# Copyright (c) 2015-Present Pivotal Software, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import, division, print_function
import os
import sys
import errno
import requests
import shutil
import subprocess
import tarfile
from . import template
try:
	# Python 3
	from urllib.request import urlretrieve
except ImportError:
	# Python 2
	from urllib import urlretrieve
import zipfile
import yaml
import datetime

from .bosh import *
from .util import *

LIB_PATH = os.path.dirname(os.path.realpath(__file__))
REPO_PATH = os.path.realpath(os.path.join(LIB_PATH, '..'))
DOCKER_BOSHRELEASE_VERSION = '23'

def build(config):
	build_bosh_releases(config)
	build_tile(config)

def build_bosh_releases(config):
	mkdir_p('release', clobber=True)
	for release in config.get('releases', []):
		release_name = release['name']
		bosh_release = BoshRelease(release, config)
		tarball = bosh_release.get_tarball()
		metadata = bosh_release.get_metadata()
		release.update(metadata)
	print()

def build_tile(context):
	mkdir_p('product', clobber=True)
	mkdir_p('product/releases')
	mkdir_p('product/tile-generator')
	tile_name = context['name']
	tile_version = context['version']
	print('tile generate metadata')
	template.render('product/metadata/' + tile_name + '.yml', 'tile/metadata.yml', context)
	print('tile generate content-migrations')
	template.render('product/content_migrations/' + tile_name + '.yml', 'tile/content-migrations.yml', context)
	print('tile generate migrations')
	migrations = 'product/migrations/v1/' + datetime.datetime.now().strftime('%Y%m%d%H%M') + '_noop.js'
	template.render(migrations, 'tile/migration.js', context)
	print('tile generate package')
	pivotal_file = os.path.join('product', tile_name + '-' + tile_version + '.pivotal')
	output = subprocess.check_output(["pip", "show", "tile-generator"], stderr=subprocess.STDOUT, cwd=".")
	with zipfile.ZipFile(pivotal_file, 'w') as f:
		for release in context.get('releases', []):
			print('tile include release', release['release_name'] + '-' + release['version'])
			shutil.copy(release['tarball'], os.path.join('product/releases', release['file']))
			f.write(
				os.path.join('product/releases', release['file']),
				os.path.join('releases', release['file']))
		f.write(
			os.path.join('product/metadata', tile_name + '.yml'),
			os.path.join('metadata', tile_name + '.yml'))
		f.write(
			os.path.join('product/content_migrations', tile_name + '.yml'),
			os.path.join('content_migrations', tile_name + '.yml'))
		f.write(migrations, migrations.replace('product/', '', 1))
		print('including tile-generator and verion information')
		f.write(
			'tile.yml',os.path.join('tile-generator','tile.yml')
		)
		f.writestr(
			os.path.join('tile-generator','version'),output
		)

	print('created tile', pivotal_file)

@@ -2348,17 +2348,138 @@
 ')%0A%09
-output = 
+print('include tile generator version and inputs')%0A%09with open(os.path.join('product', 'tile-generator', 'version'), 'wb') as f:%0A%09%09
 subp
@@ -2495,15 +2495,17 @@
 eck_
-output(
+call(%0A%09%09%09
 %5B%22pi
@@ -2534,17 +2534,20 @@
 rator%22%5D,
- 
+%0A%09%09%09
 stderr=s
@@ -2567,16 +2567,112 @@
 OUT,
- cwd=%22.%22
+%0A%09%09%09stdout=f,%0A%09%09%09cwd=%22.%22)%0A%09shutil.copy('tile.yml', os.path.join('product', 'tile-generator', 'tile.yml')
 )%0A%09w
@@ -3368,79 +3368,59 @@
 )%0A%09%09
-print('including tile-generator and verion information')%0A%09%09f.write(%0A%09%09%09
+f.write(%0A%09%09%09os.path.join('product/tile-generator', 
 'til
@@ -3425,17 +3425,22 @@
 ile.yml'
-,
+),%0A%09%09%09
 os.path.
@@ -3461,16 +3461,17 @@
 erator',
+ 
 'tile.ym
@@ -3473,19 +3473,16 @@
 le.yml')
-%0A%09%09
 )%0A%09%09f.wr
@@ -3484,19 +3484,16 @@
 %09f.write
-str
 (%0A%09%09%09os.
@@ -3495,32 +3495,40 @@
 %09%09os.path.join('
+product/
 tile-generator',
@@ -3527,16 +3527,17 @@
 erator',
+ 
 'version
@@ -3543,17 +3543,53 @@
 n'),
-output%0A%09%09
+%0A%09%09%09os.path.join('tile-generator', 'version')
 )%0A%0A%09

Make sure product directory reflects contents of zip file