#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Dell Recovery Media Launcher Script
#
# Copyright (C) 2008-2010 Dell Inc,
#   Author: Mario Limonciello
#
# This is free software; you can redistribute it and/or modify it under
# the terms of the GNU 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 General Public License along
# with this application; if not, write to the Free Software Foundation, Inc., 51
# Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
##################################################################################

from Dell.recovery_common import find_partitions, check_version
import optparse
import os

def check_builder(builder,rp):
    """Determines if we should run in builder mode"""
    if not rp and not builder:
        import gtk
        dialog=gtk.MessageDialog(type=gtk.MESSAGE_QUESTION,
                                 buttons=gtk.BUTTONS_YES_NO,
                                 message_format="This tool requires that a Linux Recovery partition is present to function normally. Would you like to run in BTO Image Builder Mode?")
        answer=dialog.run()
        dialog.hide()
        if answer == gtk.RESPONSE_YES:
            builder = True
    return builder

target=os.path.join(os.getenv('HOME'),'Download')
if not os.path.isdir(target):
    target=os.path.join(os.getenv('HOME'),'Downloads')

usage = '%prog [options]'
parser = optparse.OptionParser(usage=usage)
parser.set_defaults(
    up='',
    rp='',
    version='',
    media="dvd",
    builder=False,
    target=target,
    overwrite=False,
    xrev=False,
    branch=False
    )
parser.add_option('-c', '--check-version', dest='checkversion', action='store_true',
                  help='Show the version information.')
parser.add_option('-u', '--up', dest='up',
                  help='Override detected utility partition with this file.')
parser.add_option('-r', '--rp', dest='rp',
                  help='Override detected recovery partition with this file.')
parser.add_option('-v', '--override-version', dest='version',
                  help='Override the automatic version number generation of this ISO.')
parser.add_option('-m', '--media', dest='media',
                  help='Set type of recovery media to create [dvd, usb, iso].')
parser.add_option('-t', '--target', dest='target',
                  help='Set target directory to store ISO in when completed.')
parser.add_option('-o', '--overwrite', dest='overwrite', action='store_true',
                  help='Force overwrite of any existing file')
parser.add_option('--builder', dest='builder', action='store_true',
                  help='Enable OEM Builder mode for assembling an image from multiple sources.')
parser.add_option('--show-development-tags', dest='xrev', action='store_true',
                  help='Show development git tags when operating in builder mode.  By default, these are hidden if stable tags are present.')
parser.add_option('--override-branch-mode', dest='branch', action='store_true',
                  help='Show branches instead of tags in builder mode.  This is most useful when the tip of the branch is known stable.')
(options, args) = parser.parse_args()

if __name__ == '__main__':
    if options.checkversion:
        print "Version: %s" % check_version()
    else:
        import dbus.mainloop.glib
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        up,rp=find_partitions(options.up,options.rp)
        builder=check_builder(options.builder,rp)
    
        args = (up,
                rp,
                options.version,
                options.media,
                options.target,
                options.overwrite,
                options.xrev,
                options.branch)

        if builder:
            from Dell.recovery_builder import GTKBuilderFrontend
            frontend = GTKBuilderFrontend(*args)
        else:
            from Dell.recovery_frontend import GTKFrontend
            frontend = GTKFrontend(*args)

        frontend.run()
