#!/usr/bin/env python
# -*- coding: utf-8 -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

"""%(name)s: Gérer un fichier html tiddlywiki
USAGE
    %(name)s [-f tiddlywiki.html] action
"""

try: True, False
except: True, False = 1, 0

import os, sys
from os import path
from pyutools.base import get_args, die
from pyutools.TiddlyWiki import *

######################################################################
# Programme principal

def run():
    twfile = None

    opts, args = get_args(None, [
        ("h", "help", "Afficher l'aide"),
        ("f:", None, "Spécifier le fichier tiddlywiki.html"),
        ])

    for opt, value in opts:
        if opt in ("-h", "--help"):
            print __doc__ % {"name": path.split(sys.argv[0])}
            sys.exit(0)
        elif opt in ("-f", ):
            twfile = value

    if twfile is None: twfile = "TODO.html"
    if not path.exists(twfile): die("Fichier inexistant: %s" % twfile)

    action = args[0:1] and args[0] or "list"
    if action in ("l", "list"):
        list_tiddlers(twfile, args[1:])
    elif action in ('a', 'add', 'r', 'replace'):
        replace_tiddler(twfile, args=args[1:])
    elif action in ('d', 'delete'):
        delete_tiddler(twfile, args=args[1:])
    else:
        die("Action invalide: %s" % action)

if __name__ == "__main__":
    run()
