#!/usr/bin/env python3
# Copyright (c) 2011, 2012, 2013 Marek Sapota
#
# 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 readline
import signal
import threading
import time

from gi.repository import GObject
import dbus.mainloop.glib
dbus.mainloop.glib.threads_init()
dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
import dbus

from Shade.storage.Storage import Storage
from Shade.storage.Completer import Completer
from Shade.storage import Config

Config.init('storage.conf')

stop = False
reading = False

class DBusLoop(threading.Thread):
    def run(self):
        loop = GObject.MainLoop()
        context = loop.get_context()
        while not stop:
            context.iteration(False)
            time.sleep(0.5)

dbus_loop = DBusLoop()
dbus_loop.start()

# Register a SIGINT handler.
def handler(signum, frame):
    global stop
    stop = True
    if reading:
        raise EOFError()
signal.signal(signal.SIGINT, handler)
# Don't interrupt system cals on SIGINT.
signal.siginterrupt(signal.SIGINT, False)

storage = Storage()

readline.parse_and_bind("tab: complete")
readline.set_completer(Completer(storage).complete)

while not stop:
    try:
        reading = True
        cmd = input('>>> ')
        reading = False
    except EOFError:
        cmd = 'exit'

    cmd = cmd.split()
    # Set list as the default command.
    if len(cmd) == 0:
        cmd = ['list']
    args = cmd[1:]
    cmd = cmd[0]

    if cmd == 'update':
        storage.update()
    elif cmd == 'list':
        storage.list()
    elif cmd == 'mount':
        if len(args) == 0:
            print('Not enough arguments')
            continue
        storage.mount(args[0])
    elif cmd == 'unmount' or cmd == 'umount':
        if len(args) == 0:
            print('Not enough arguments')
            continue
        storage.unmount(args[0])
    elif cmd == 'lock':
        if len(args) == 0:
            print('Not enough arguments')
            continue
        storage.lock(args[0])
    elif cmd == 'unlock':
        if len(args) == 0:
            print('Not enough arguments')
            continue
        storage.unlock(args[0])
    elif cmd == 'detach':
        if len(args) == 0:
            print('Not enough arguments')
            continue
        storage.detach(args[0])
    elif cmd == 'exit':
        stop = True
    else:
        print('Unknown command')

dbus_loop.join()
