#!/usr/bin/env python
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-
#
# This file is part of Déjà Dup.
# © 2008,2009 Michael Terry <mike@mterry.name>
#
# Déjà Dup 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 3 of the License, or
# (at your option) any later version.
#
# Déjà Dup 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 Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.
#
# Setup a local backend, but just show the UI.  The user will interact with
# it and test manually.  Good for playing with the UI as you make changes,
# without futzing up your own gconf settings.

import base
import ldtp
import sys
import os
import tempfile

def test():
  version = None
  mtype = None
  tmpid = None
  sources = []
  dest = None
  size = None
  back = 'file'
  prog = None
  hasrun = False
  encrypt = True
  cmd = ''
  for arg in sys.argv[1:]:
    if prog == 'cmd':
      cmd += arg + ' '
      continue
    if arg == 'vfat':
      mtype = arg
    elif arg == 'ext3':
      mtype = arg
    elif arg in ('applet', 'shell', 'cmd'):
      prog = arg
    elif arg == 'hasrun':
      hasrun = True
    elif arg == 'nocrypto':
      encrypt = False
    elif arg.startswith('id='):
      tmpid = arg.split('=', 1)[-1]
    elif arg.startswith('dup='):
      version = arg.split('=', 1)[-1]
    elif arg.startswith('source='):
      source = arg.split('=', 1)[-1]
      sources += [source]
    elif arg.startswith('size='):
      size = int(arg.split('=', 1)[-1])
    elif arg.startswith('back='):
      back = arg.split('=', 1)[-1]
    elif arg.startswith('dest='):
      dest = arg.split('=', 1)[-1]
    else:
      print "Unrecognized argument '%s'" % arg
      return 1
  
  if version is not None:
    os.environ['DEJA_DUP_TEST_VERSION'] = version
  if tmpid is not None:
    tmpdir = tempfile.gettempdir() + '/' + tmpid
    os.environ['DEJA_DUP_TEST_TMP'] = tmpdir
  if mtype is not None or size is not None:
    dest = base.create_mount(mtype=mtype, size=size)
  
  base.setup(backend=back, dest=dest, sources=sources, start=False, encrypt=encrypt)
  if hasrun:
    base.set_gconf_value("last-run", "fake")
  
  if prog is None:
    base.start_deja_dup(waitfor=None)
    base.wait_for_quit()
  elif prog == 'applet':
    base.start_deja_dup_applet()
    base.wait_for_quit()
  elif prog == 'shell':
    os.system('bash')
  elif prog == 'cmd':
    os.system(cmd)
  return True

base.run(test)
