#!/usr/bin/env python
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-
#
# This file is part of Déjà Dup.
# For copyright information, see AUTHORS.
#
# 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/>.

# This mock duplicity reads from a given file describing:
# 1) What arguments to expect
# 2) What output to give
#
# The file location is specified by DEJA_DUP_TEST_MOCKSCRIPT.
# An example format of the file is:

# ARGS: full --include --exclude --etc --dry-run
# RETURN: 0
#
# First sample output message
#
# Second and final sample output message
#
# === deja-dup ===
# ARGS: full --include --exclude --etc
# RETURN: 0
#
# First sample output message
#
# Second and final sample output message

# Every time if things go as expected, we will wipe the first stanza from the
# file.  If it's the last stanza left, we'll delete the file.  That way,
# any caller can know if we got passed unexpected arguments by testing for the
# existence of the file.

import sys, os, shlex, getpass, time

if not os.path.exists(os.environ['DEJA_DUP_TEST_MOCKSCRIPT']):
  print >> logfd, "TESTFAIL: no mockscript"
  sys.exit(-1)

lines = []
with open(os.environ['DEJA_DUP_TEST_MOCKSCRIPT']) as f:
  lines = f.readlines()

# In general, don't bother trying to avoid exceptions. If we don't get expected
# input, that's a test failure too.

def skip_whitespace(lineno):
  while len(lines) > lineno and not lines[lineno].strip():
    lineno += 1
  return lineno

curline = skip_whitespace(0)

rv = 0
expected_args = []
delay = 0

while len(lines) > curline and lines[curline].strip():
  tokens = lines[curline].split()
  if tokens[0] == 'ARGS:':
    expected_args = shlex.split(lines[curline])[1:]
  elif tokens[0] == 'RETURN:':
    rv = int(tokens[1])
  elif tokens[0] == 'DELAY:':
    delay = int(tokens[1])
  curline += 1

# Where should we spit our messages to?
logfd = None
for i in xrange(len(sys.argv)):
  split = sys.argv[i].split('=', 1)
  if len(split) > 1 and split[0] == "--log-fd":
    logfd = os.fdopen(int(split[1]), "w")
    sys.argv[i] = "--log-fd=?"

if expected_args != sys.argv[1:]:
  print >> logfd, "TESTFAIL: expected\n%s\nvs\n%s" % (expected_args, sys.argv[1:])
  sys.exit(-1)

curline = skip_whitespace(curline)

while len(lines) > curline and lines[curline] != "=== deja-dup ===\n":
  print >> logfd, lines[curline],
  curline += 1

# Write back mockscript
if len(lines) <= curline:
  os.unlink(os.environ['DEJA_DUP_TEST_MOCKSCRIPT'])
else:
  lines = lines[curline+1:]
  with open(os.environ['DEJA_DUP_TEST_MOCKSCRIPT'], 'w') as f:
    f.writelines(lines)

time.sleep(delay)

sys.exit(rv)
