#!/usr/bin/env python3

import os
import shutil

# Mostly copypasta from https://github.com/LongHairedHacker/verdandi/blob/master/mixins/fileassetsmixin.py

def copy_file(source_path, dest_path):
    print("Copying %s to %s" % (source_path, dest_path))

    dest_dir = os.path.dirname(dest_path)

    if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)
    shutil.copy(source_path, dest_path)


def copy_dir(source_path, dest_path):
	# /foo/bar /rofl -> contents of bar go to rofl/bar
	# /foo/bar/ /rofl -> contests of bar got to rofl/
	# Trailing slash on destination should have no effect

	# Will be '' in case of a trailing slash: /foo/bar/ else bar
	source_base = os.path.basename(source_path)
	# /rofl will become /rofl/ if base is '' otherwise it will become /rofl/bar
	dest_path = os.path.join(dest_path, source_base)

	if not os.path.exists(dest_path):
		os.makedirs(dest_path)

	# Discover the whole tree and copy each file individually
	for source_dir, _, files in os.walk(source_path):
		rel_path = os.path.relpath(source_dir, source_path)
		# Purely cosmetical for debug output
		if rel_path == '.':
			dest_dir = dest_path
		else:
			dest_dir = os.path.join(dest_path, rel_path)

		for source_file in files:
			file_source_path = os.path.join(source_dir, source_file)
			file_dest_path = os.path.join(dest_dir, source_file)
			copy_file(file_source_path, file_dest_path)
