#! /usr/bin/perl
################################################################################
## taskwarrior - a command line task list manager.
##
## Copyright 2006-2012, Paul Beckingham, Federico Hernandez.
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
##
## http://www.opensource.org/licenses/mit-license.php
##
################################################################################

use strict;
use warnings;
use Test::More tests => 19;
use File::Copy;

use constant false => 0;
use constant true => 1;

# Create data locations
mkdir("local", 0755);
ok(-e 'local', "Created directory local");
mkdir("remote", 0755);
ok(-e 'remote', "Created directory remote");

# Create the rc files.
if (open my $fh, '>', 'local.rc')
{
  print $fh "data.location=./local\n",
            "confirmation=no\n",
				"merge.autopush=no\n",
            "report.list.description=DESC\n",
				"report.list.columns=id,project,active,priority,description,tags\n",
				"report.list.labels=id,pro,a,pri,d,t\n",
				"report.list.sort=id+\n",
				"report.list.filter=status:pending depends.none:\n";
  close $fh;
  ok (-r 'local.rc', 'Created local.rc');
}

if (open my $fh, '>', 'remote.rc')
{
  print $fh "data.location=./remote\n",
            "confirmation=no\n",
				"merge.autopush=no\n",
            "report.list.description=DESC\n",
				"report.list.columns=id,project,active,priority,description,tags\n",
				"report.list.labels=id,pro,a,pri,d,t\n",
				"report.list.sort=id+\n",
				"report.list.filter=status:pending depends.none:\n";
  close $fh;
  ok (-r 'remote.rc', 'Created remote.rc');
}

# Create some basic tasks on both sides
qx{../src/task rc:local.rc add both};
qx{faketime -f '+1s' ../src/task rc:local.rc add blocked};
qx{faketime -f '+2s' ../src/task rc:local.rc add blocking};
qx{faketime -f '+3s' ../src/task rc:local.rc 2 dep:3};

# run gc
qx{../src/task rc:remote.rc};

# push to remote
qx{../src/task rc:local.rc push ./remote/};

# make remote modifications
#qx{faketime -f '+10s' ../src/task rc:remote.rc add right};
qx{faketime -f '+11s' ../src/task rc:remote.rc done 3};             #blocking

# run gc
qx{../src/task rc:remote.rc};

my $output_l = qx{../src/task rc:local.rc merge remote/};

#check output
unlike ($output_l,   qr/Missing/,              "merge: no missing entry");
unlike ($output_l,   qr/Not adding duplicate/, "merge: no duplicates");

# check reports
my $report_l = qx{../src/task rc:local.rc};

like   ($report_l,   qr/blocked/,          'merge: "blocked" pending');
unlike ($report_l,   qr/blocking/,         'merge: "blocking" not pending');

$report_l = qx{../src/task rc:local.rc blocked};
unlike   ($report_l,   qr/blocked/,        'merge: "blocked" unblocked');

#exit

# Cleanup.
unlink 'local/pending.data';
ok (!-r 'local/pending.data', 'Removed local/pending.data');

unlink 'local/completed.data';
ok (!-r 'local/completed.data', 'Removed local/completed.data');

unlink 'local/undo.data';
ok (!-r 'local/undo.data', 'Removed local/undo.data');

unlink 'local.rc';
ok (!-r 'local.rc', 'Removed local.rc');

unlink 'remote/pending.data';
ok (!-r 'remote/pending.data', 'Removed remote/pending.data');

unlink 'remote/completed.data';
ok (!-r 'remote/completed.data', 'Removed remote/completed.data');

unlink 'remote/undo.data';
ok (!-r 'remote/undo.data', 'Removed remote/undo.data');

unlink 'remote.rc';
ok (!-r 'remote.rc', 'Removed remote.rc');

rmdir("remote");
ok (!-e "remote", "Removed dir remote");
rmdir("local");
ok (!-e "local", "Removed dir local");

exit 0;

