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

HELP = """
Transifex client application

usage: tx [--version] [-h|--help] [-d|--debug] [-p|--path_to_tx] COMMAND ARGS

COMMAND can be one of the following:

get_source_file   Fetch only the source file of a specific resource.
init              Create a local instance of a transifex project.
push              Update remote resources with the local file content.
pull              Fetch all files (source, translation) from server.
send_source_file  Update the source strings of a specific remote resource.
set_source_file   Assign a source file to a project resource locally.
set_translation   Assign a translation file to a project resource locally.
status            Show the current configuration status.


See 'tx help COMMAND' for more information on a specific command.
"""

DEBUG = True
#AUTHENTICATION = 'HTTPAUTHBASIC'


#import copy
#import ConfigParser
#import getpass
import getopt
import os
#import re
#import shutil
import sys
#import urllib2

#from json import loads as parse_json, dumps as compile_json
#from poster.encode import multipart_encode, MultipartParam
#from poster.streaminghttp import register_openers

from txclib.commands import *
from txclib.utils import *
from txclib.project import *

reload(sys) # WTF? Otherwise setdefaultencoding doesn't work

# This block ensures that ^C interrupts are handled quietly.
try:
    import signal

    def exithandler(signum,frame):
        signal.signal(signal.SIGINT, signal.SIG_IGN)
        signal.signal(signal.SIGTERM, signal.SIG_IGN)
        sys.exit(1)

    signal.signal(signal.SIGINT, exithandler)
    signal.signal(signal.SIGTERM, exithandler)
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)

except KeyboardInterrupt:
    sys.exit(1)

# When we open file with f = codecs.open we specifi FROM what encoding to read
# This sets the encoding for the strings which are created with f.read()
sys.setdefaultencoding('utf-8')


def usage(cmd=None):
    """
    Explain the usage of the tx client commands.
    """
    # TODO: Implement all the command specific documentation.
    MSG(HELP)


def main(argv):
    """
    Here we parse the flags (short, long) and we instantiate the classes.
    """
    DEBUG = False
    path_to_tx = None
    extra_opts = []
    try:
        opts, args = getopt.getopt(argv, "vhd",
            ["version", "help", "debug", "path_to_tx="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-v", "--version"):
            MSG("Transifex Client Version 0.1")
            sys.exit()
        elif opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-d", "--debug"):
            DEBUG = True
        elif opt in ("--path_to_tx"):
            path_to_tx = arg

    # If no commands provided show the usage and exit
    if len(args) == 0:
        usage()
        sys.exit(2)

    cmd = args[0]
    MSG("Command : %s" % cmd)
    try:
        if cmd == 'get_source_file':
            cmd_get_source_file(args[1:], path_to_tx)
        elif cmd == 'init':
            cmd_init(args[1:], path_to_tx)
        elif cmd == 'push':
            cmd_push(args[1:], path_to_tx)
        elif cmd == 'pull':
            cmd_pull(args[1:], path_to_tx)
        elif cmd == 'send_source_file':
            cmd_send_source_file(args[1:], path_to_tx)
        elif cmd == 'set_source_file':
            cmd_set_source_file(args[1:], path_to_tx)
        elif cmd == 'set_translation':
            cmd_set_translation(args[1:], path_to_tx)
        elif cmd == 'status':
            cmd_status(args[1:], path_to_tx)
        else:
            ERRMSG("\'%s\' is not a tx-command. See \'tx --help\'." % cmd)
            sys.exit(2)
    except:
        raise
        sys.exit()


# Run baby :) ... run
if __name__ == "__main__":
    # sys.argv[0] is the name of the script that we’re running.
    main(sys.argv[1:])
