#!/usr/bin/python
"""Usage: kidc [OPTIONS] [file...]
Compile kid templates into Python byte-code (.pyc) files. 

OPTIONS:
  -f, --force                      Force compilation even if .pyc file already exists.
  -s, --source                     Generate .py source files along with .pyc files. This
                                   is sometimes useful for debugging.
  -d, --strip-dest-dir <destdir>   Strips the supplied path from the beginning of source
                                   filenames stored for error messages in the generated
                                   .pyc files

Files list may have files and/or directories. If a directory is specified,
all .kid files found in the directory and any sub-directories are compiled.
"""

# Copyright (c) 2004-2005 Ryan Tomayko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import sys
  
try:
  import kid
except ImportError:
  from os.path import join as joinpath, dirname, abspath
  # adjust sys.path if we're running directly within a source distribution
  bin_dir = dirname(__file__)
  lib_dir = abspath(joinpath(bin_dir, '..'))
  if os.access(joinpath(lib_dir, 'kid', '__init__.py'), os.F_OK):
    sys.path.insert(0, lib_dir)
  import kid

from kid.compiler import kidc, KidcUsageError
try:
  kidc(sys.argv)
except KidcUsageError, e:
  msg = str(e)
  if msg != 'help':
    print msg
  sys.stdout.write(__doc__)
except:
  if isinstance(sys.exc_type, str):
    sys.stderr.write(sys.exc_type + '\n')
  else:
    raise
