#!${buildout:executable} -S

# Initialize our paths.
${python-relative-path-setup}
import sys
sys.path.insert(0, ${scripts:parts-directory|path-repr})
import site

import os

from lp.scripts.utilities.js.jsbuild import ComboFile
from lp.scripts.utilities.js.combo import combine_files

# This constant helps us meet maximum line-length goals.
GALLERY_ACCORDION = 'yui3-gallery/gallery-accordion/assets/'


root = os.path.abspath('.')
root = os.path.normpath(${buildout:directory|path-repr})
icing = os.path.join(root, 'lib/canonical/launchpad/icing')
target = os.path.join(icing, 'combo.css')
# It'd probably be nice to have this script find all the CSS files we might
# need and combine them together, but if we do that we'd certainly end up
# including lots of styles that we don't need/want, so keeping this hard-coded
# list seems like the best option for now.
names = [
    'ubuntu-webfonts.css',
    'style.css',
    'yui/cssreset/reset.css',
    # Use the old cssgrids instead of the new cssgrids.
    #'lazr/build/yui/cssgrids/grids.css',
    'cssgrids/grids.css',
    'build/lazr/assets/skins/sam/lazr.css',
    'build/inlineedit/assets/skins/sam/editor.css',
    'build/autocomplete/assets/skins/sam/autocomplete.css',
    'build/overlay/assets/skins/sam/pretty-overlay.css',
    'build/formoverlay/assets/formoverlay-core.css',
    'build/indicator/assets/indicator-core.css',
    'build/confirmationoverlay/assets/confirmationoverlay-core.css',
    'build/picker/assets/skins/sam/picker.css',
    'build/activator/assets/skins/sam/activator.css',
    'build/choiceedit/assets/choiceedit-core.css',
    'build/ordering/assets/ordering-core.css',
    GALLERY_ACCORDION + 'gallery-accordion-core.css',
    GALLERY_ACCORDION + 'skins/sam/gallery-accordion-skin.css',
    'build/sprite.css',
    # Include our main stylesheets at the end so they
    # take precedence over the others.
    'css/base.css',
    'css/typography.css',
    'css/colours.css',
    'css/forms.css',
    'css/layout.css',
    'css/modifiers.css']

# Get all the component css files so we don't have to edit this file every
# time a new component is added
component_dir = 'css/components'
component_path = os.path.abspath(os.path.join(icing, component_dir))
for root, dirs, files in os.walk(component_path):
    for file in files:
        if file.endswith('.css'):
            names.append('%s/%s' % (component_dir, file))

absolute_names = []
for name in names:
    full_path_name = os.path.abspath(os.path.join(icing, name))
    absolute_names.append(full_path_name)

combo = ComboFile(absolute_names, target)
if combo.needs_update():
    result = ''
    for content in combine_files(names, icing):
        result += content

    f = open(target, 'w')
    f.write(result)
    f.close()
