#!/usr/bin/env python
# vim: set filetype=python

# Copyright 2015 Lionheart Software LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import tempfile
from subprocess import call
import argparse

import git
from bigstore import push, pull, filter_clean, filter_smudge, init, log

class BigstoreInitAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        init()

class BigstorePushAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        push()

class BigstorePullAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        pull()

class BigstoreLogAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        log()

class BigstoreFilterCleanAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        filter_clean()

class BigstoreFilterSmudgeAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        filter_smudge()

class BigstoreShowImageAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        with tempfile.TemporaryFile(mode='w+r') as file:
            call("convert {} jpg:- | jp2a --width=80 -".format(sys.argv[2]), stdout=file, shell=True)

            file.seek(0)
            for line in file:
                sys.stdout.write(line)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(prog='git-bigstore')
    subparsers = parser.add_subparsers()

    parser_init = subparsers.add_parser("init", help="initialize a repository to use bigstore")
    parser_init.add_argument("directory", nargs="?", default=".", action=BigstoreInitAction)

    parser_push = subparsers.add_parser("push", help="upload bigstore files to your storage backend")
    parser_push.add_argument("pattern", nargs="*",
            help="only push filenames matching specified patterns", action=BigstorePushAction)

    parser_pull = subparsers.add_parser("pull", help="download bigstore files from the storage backend")
    parser_pull.add_argument("pattern", nargs="*", help="only pull filenames matching specified patterns",
            action=BigstorePullAction)

    parser_log = subparsers.add_parser("log", help="display a history of the specified file")
    parser_log.add_argument("filename", nargs=1, action=BigstoreLogAction)

    parser_filter_clean = subparsers.add_parser("filter-clean", help="clean the specified file from stdin")
    parser_filter_clean.add_argument("input", nargs='?', type=argparse.FileType('r'), action=BigstoreFilterCleanAction, default=sys.stdin)

    parser_filter_smudge = subparsers.add_parser("filter-smudge", help="smudge the specified file from stdin")
    parser_filter_smudge.add_argument("input", nargs='?', type=argparse.FileType('r'), action=BigstoreFilterSmudgeAction, default=sys.stdin)

    parser_show_image = subparsers.add_parser("show-image", help="display the specified file from stdin in ascii format")
    parser_show_image.add_argument("input", type=argparse.FileType('r'), action=BigstoreShowImageAction)

    args = parser.parse_args()

