#!/usr/bin/env python
# -*- coding: utf-8 -*-

Import('env')

from util import match_patterns, minifiables, filter_files
from fnmatch import fnmatch
from os.path import basename
from filesystem import walk_dir

SRC_DIR = './'
DEST_DIR = '../' + env['MEDIA_DEST_DIR'] + '/'

SCRIPT_FILES = Split("""
    script/lib/chickoojs/src/jquery/jquery-1.3.2.js
    script/lib/chickoojs/src/jquery/jquery-1.4a1.min.js
    script/lib/chickoojs/src/jquery/tools//1.1.2/jquery.tools.min.js
    script/base.js
    script/index.js
    script/blog.js
""")

IMAGE_FILES = Split("""
    image/bg/button_overlay.png
    image/bg/textfield_normal.png
    image/bg/textfield_readonly.png
    image/bg/bg.gif
    image/bg/splash_hover.jpg
    image/bg/footer_bg.png
    image/bg/trunk.jpg
    image/bg/bg_header.jpg
    image/bg/bkg_footer1.jpg
    image/bg/trunk-content.jpg
    image/bg/footer.jpg
    image/bg/splash.jpg
    image/bg/top-shadow.png
    image/bg/bottom-shadow.png
    image/bg/op-screenshot.png
    image/icon/folder.png
    image/logo/money_bee.png
    image/logo/money_bee_small.png
""")

CSS_FILES = Split("""
    css/screen.css
""")

# Do not include IMAGE files in here.
# Please include them in IMAGE_FILES above.
# If you would like images to be copied "as is" without
# any optimizations, then you may include them here.
PLAIN_COPIES = SCRIPT_FILES + CSS_FILES + Split("""
    robots.txt
""")

PLAIN_COPY_DIRECTORIES = Split("""

""")

# Recursively add all the files in the directories to files to copy.
for directory in set(PLAIN_COPY_DIRECTORIES):
    files = [f[len(SRC_DIR):] for f in walk_dir(SRC_DIR + directory)]
    PLAIN_COPIES.extend(files)

# Plain copies.
for f in set(PLAIN_COPIES):
    env.Command(DEST_DIR + f, SRC_DIR + f, Copy("$TARGET", "$SOURCE"))

# Optimize graphics
for f in IMAGE_FILES:
    filename = str(f)
    if match_patterns(filename, patterns=['*.png']):
        env.OptimizePNG(DEST_DIR + f, SRC_DIR + f)
    elif match_patterns(filename, patterns=['*.jpg', '*.jpeg']):
        env.OptimizeJPEG(DEST_DIR + f, SRC_DIR + f)
    else:
        env.Command(DEST_DIR + f, SRC_DIR + f, Copy("$TARGET", "$SOURCE"))


# Stitching graphics together

stitched_images = []
'''
    dict(
        target='target.png',
        sources=Split("""
        """),
        orientation='horizontal',
        ),
]
'''

for stitch in stitched_images:
    func = env.HStitchImages if stitch.get('orientation', 'horizontal') == 'horizontal' else env.VStitchImages
    func(DEST_DIR + stitch['target'], [SRC_DIR + f for f in stitch['sources']])

# Minification of the generated CSS and JS files.
MINIFIABLE_FILES = SCRIPT_FILES + CSS_FILES + Split("""

""")

# Exclude these files specifically from minification.
NON_MINIFIABLE_FILES = Split("""

""")


# Don't minify all files in one command.  Minify them separately.
# It keeps build times small.
for f in minifiables(MINIFIABLE_FILES, NON_MINIFIABLE_FILES):
    filepath = DEST_DIR + str(f)
    env.YuiCompressorMinify(filepath)
    #if fnmatch(basename(filepath), '*.js'):
    #    env.JsminMinify(filepath)

# Combine these JavaScript files and create a common js.
#
# Warning:  THE ORDER MATTERS.
#
env.YuiCompressorMinify(env.Concatenate([DEST_DIR + 'script/page/base.js'], [SRC_DIR + f for f in Split("""
    script/base.js
""")]))

env.YuiCompressorMinify(env.Concatenate([DEST_DIR + 'script/page/index.js'], [SRC_DIR + f for f in Split("""
    script/index.js
""")]))

env.YuiCompressorMinify(env.Concatenate([DEST_DIR + 'css/screen-all.css'], [SRC_DIR + f for f in Split("""
    css/screen.css
""")]))
