#!/usr/bin/env python
#vim: et:ts=4:sw=4:sts=4
import json
import os
import re
import shutil

def copy_file(source, target):
    print 'copying', source, 'to', target
    write_file(target, read_file(source))

def read_file(file):
    print 'reading', file
    f = open(file)
    data = f.read()
    f.close()
    return data

def write_file(file, data):
    print 'writing', file
    write_path(file)
    f = open(file, 'w')
    f.write(data)
    f.close()
    return len(data)

def write_link(source, target):
    print 'linking', source, 'to', target
    write_path(target)
    if os.path.exists(target):
        os.unlink(target)
    os.symlink(source, target)

def write_path(file):
    path = os.path.split(file)[0]
    if path and not os.path.exists(path):
        os.makedirs(path)

base_path = os.path.dirname(__file__)
if base_path:
    os.chdir(base_path)

source_path = '../../source/'
build_path = '../../build/'
files = ['Ox.UI/css/Ox.UI.css']

# SVGs
path = source_path + 'Ox.UI/themes/classic/svg/'
for filename in os.listdir(path):
    svg = read_file(path + filename)
    svg = svg.replace('#404040', '#FFFFFF').replace('#000000', '#FFFFFF')
    write_file(path.replace('/classic/', '/modern/') + filename, svg)

# symlinks
for path, dirnames, filenames in os.walk(source_path):
    for filename in filenames:
        if not '_' in path and not filename[0] in '._':
            parts = os.path.join(path.replace(source_path, ''), filename).split('/')
            for i, part in enumerate(parts):
                if i < len(parts) - 1:
                    parts[i] = '..'
            source = '/'.join(parts).replace(filename, os.path.join(path, filename))[3:]
            is_jquery = re.compile('^jquery-[\d\.]+\.js$').findall(filename)
            is_jquery_min = re.compile('^jquery-[\d\.]+\.min\.js$').findall(filename)
            is_jquery_plugin = re.compile('^jquery\..*?\.js$').findall(filename)
            if is_jquery:
                target = os.path.join(path.replace(source_path, build_path), 'jquery.js')
            elif is_jquery_min:
                target = os.path.join(path.replace(source_path, build_path), 'jquery.min.js')
            else:
                target = os.path.join(path.replace(source_path, build_path), filename)
                if is_jquery_plugin:
                    files.append(target.replace(build_path, ''))
            write_link(source, target)

# Ox.UI
js = ''
section = ''
root = source_path + 'Ox.UI/'
for path, dirnames, filenames in os.walk(root):
    for filename in filenames:
        # theme css files get included by main css
        if path != root and not '_' in path and not filename[0] in '._' and filename[-4:] != '.css':
            files.append(os.path.join(path.replace(source_path, ''), filename))
            if filename[-3:] == '.js':
                folder = os.path.split(path)[-1]
                if section != folder:
                    section = folder
                    js += '/*\n' + '=' * 80 + '\n' + section + '\n' + '=' * 80 + '\n*/\n\n'
                js += '/*\n' + '-' * 80 + '\n' + filename[:-3] + '\n' + '-' * 80 + '\n*/\n\n'
                js += read_file(os.path.join(path, filename)) + '\n\n'

write_file(build_path + 'Ox.UI/js/Ox.UI.js', js)
files = json.dumps(sorted(files), indent=4, sort_keys=True)
write_file(build_path + 'Ox.UI/json/Ox.UI.json', files)