#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Usage:
  bucket3 [options] init
  bucket3 [options] clear
  bucket3 [options] new <slug> [<ext>]
  bucket3 [options] render [--posts] [--static] [--rss] [--sitemap]
  bucket3 [options] update
  bucket3 [options] mentions [--twitter]
  bucket3 [options] getenv <variable>
  bucket3 -h, --help
  bucket3 --version

Options:
  -p, --path=<path>        top directory of your blog. [default: .]
  -v, --verbose=<level>,   show messages, level=0..2, 0=silent. [default: 1]
"""

from docopt import docopt

import sys
from bucket3 import __version__
from bucket3 import b3tools
from bucket3.fsmeta import fsmeta
from bucket3.bucket import Bucket3
import time


if __name__ == '__main__':

    args = docopt(__doc__, version=__version__)

    VERBOSE = int(args['--verbose'])

    if args['init']:
        b3tools.blog_new(args['--path'])

    elif args['getenv']:
        conf = b3tools.conf_get(args['--path'])
        parts = args['<variable>'].split('.')
        part = conf
        for p in parts:
          part = part[p]
        print part

    elif args['clear']:
        b3tools.blog_clean(args['--path'])

    elif args['new']:
        b3tools.post_new(slug=args['<slug>'], ext=args['<ext>'], cpath=args['--path'])

    elif args['update']:
        conf = b3tools.conf_get(args['--path'])
        fsdb = fsmeta( conf['root_dir'] )
        fsdb.create()

        t1 = int(time.time())
        t2 = int(fsdb.meta_get('last_obj_ts', 0))

        fsdb.fs_sync( 'posts' )

        b = Bucket3(conf = conf, verbose=VERBOSE)
        b.db_init()

        for f in fsdb.file_get_new( since_ts=t2 ):
            if VERBOSE:
                print "Located new/modified file", f['path']
            post = b.fs_post_get(f['path'])
            if post:
                b.db_post_put(post)
        for f in fsdb.file_get_deleted( before_ts=t1 ):
            if VERBOSE:
                print "Located deleted file    ", f['path']
            post_id = b.fs_post_get_id(f['path'])
            b.db_post_del(post_id)
            fsdb.file_del(f['id'])

        fsdb.close()

        # Render static pages, CSS files, etc.
        # Should be able to decide smarter if this is necessary each time
        print 'Rendering skeleton and static pages.'
        b.render_html_skel()

        b.rq_do()

    elif args['render']:
        conf = b3tools.conf_get(args['--path'])
        b = Bucket3(conf = conf, verbose=VERBOSE)

        if args['--static']:
            if VERBOSE:
                print 'Rendering skeleton and static pages.'
            b.render_html_skel()
        if args['--rss']:
            if VERBOSE:
                print 'Rendering RSS.'
            b.render_rss()
        if args['--sitemap']:
            if VERBOSE:
                print 'Rendering sitemap.xml.'
            b.render_xml_sitemap()
        if args['--posts']:
            if VERBOSE:
                print 'Rendering all posts in db.'
            b.db_init()
            for post in b.db_post_get_all(count=None):
                b.render_Q.update([('post', post['id']), ])
                b.rq_post_deps(post)
            b.rq_do()

    elif args['mentions']:
        from bucket3.mentions import Mentions
        conf = b3tools.conf_get(args['--path'])
        r = Mentions(conf=conf)
        r.getTwitterMentions()
        r.getWebMentions()
