#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Dell Recovery
#
# Copyright (C) 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
import subprocess

target = os.path.expanduser("~/Downloads")
try:
    p = subprocess.Popen(["xdg-user-dir","DOWNLOAD"], stdout=subprocess.PIPE)
    downloadpath = p.communicate()[0].strip()
    if p.returncode == 0 and downloadpath:
        target = downloadpath
except OSError:
    pass


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)
        utility, recovery = find_partitions(options.up,options.rp)

        #If we don't find an RP, assume builder mode.
        if not recovery and not options.builder:
            options.builder = True
    
        args = (utility,
                recovery,
                options.version,
                options.media,
                options.target,
                options.overwrite)

        if options.builder:
            from Dell.recovery_advanced_gtk import AdvancedGeneratorGTK
            args += (options.xrev,
                     options.branch)
            tool = AdvancedGeneratorGTK(*args)
        else:
            from Dell.recovery_basic_gtk import BasicGeneratorGTK
            tool = BasicGeneratorGTK(*args)

        tool.run()
