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

# usbmanager.py
#
# Copyright (c) 2008 Magnun Leno da Silva
#
# Author: Magnun Leno da Silva <magnun.leno@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USAA.

__version__ = "1.0"
__author__ = "Magnun Leno da Silva <magnun.leno@gmail.com>"

from dbus.mainloop.glib import DBusGMainLoop
from sys import argv
from shutil import rmtree
from shutil import copy
import os

if os.path.exists("src") and os.path.exists("data") and os.path.exists("locale"):
    locales = os.walk('./locale').next()[2]
    locales.remove('template.pot')
    locales.remove('template.mo')
    locales = [locale.split('.')[0] for locale in locales if locale.split('.')[1] == 'mo']

    for locale in locales:
        dst = './locale/'+locale    
        if os.path.exists(dst):
            rmtree(dst)
        os.mkdir(dst)
        dst += '/LC_MESSAGES/'
        os.mkdir(dst)
        src = './locale/'+locale+'.mo'
        dst += 'usbmanager.mo'
        copy(src, dst)
        import src.main as usbmanager
else:
    import usbmanager.main as usbmanager

if __name__ == "__main__":
    '''
        Main routine.
        Responsible for starting the usbmanager in the following modes:
         - Main app;
         - Single device properties;
         - On system tray.
    '''
    # Set the DBusMainLoop as default: Necessary to use DBus signals
    DBusGMainLoop(set_as_default=True)
    if len(argv) is 1:
        # No arg. Main app Mode
        m = usbmanager.Main(mode=1)
        m.run()
        exit(0)

    elif len(argv) is 2 and argv[1] == '--tray':
        # System tray mode
        m = usbmanager.Main(mode=2)
        m.run()
        exit(0)

    elif len(argv) is 2:
        m = usbmanager.Main(mode=3, arg=argv[1])
        m.run()
        exit(0)


    print "Usage:"
    print "   usbmanager [mount_point | --tray]"
    print "ex:"
    print "   $ usbmanager"
    print "or "
    print "   $ usbmanager --tray"
    print "or "
    print "   $ usbmanager /media/my_flash_device"
    exit(1)
