#!/usr/bin/env bash
VERSION='v2.0.0'

warn()  { echo "$@" >&2;        }
die()   { warn "$@"; exit 1;    }

test -z "$(find . -mindepth 1 -maxdepth 1 -not -name .release -not -name .backup -not -name '.lock*')" ||
	perl -MNarada -E 'Narada::detect(@ARGV)' narada || exit 1

[ $# -ne 0 ] || die "Usage: narada-restore path/to/backup.full.tar [files]"

backup="$1"
shift

# make sure every file name to extract (if any) begins with ./
files=("$@")
files=("${files[@]#/}")
files=("${files[@]#./}")
files=("${files[@]/#/./}")

excl=()
test -d '.release' && excl+=( --exclude './.release' )
test -d '.backup'  && excl+=( --exclude './.backup'  )
excl+=( --exclude './.lock*' )

TAR=`which gtar tar 2>/dev/null | head -n 1`
LANG= exec $TAR -x -p -f "$backup" -g /dev/null --anchored "${excl[@]}" "${files[@]}"


: <<'=cut'

=encoding utf8

=head1 NAME

narada-restore - restore full project or some files from backup


=head1 VERSION

This document describes narada-restore version v2.0.0


=head1 USAGE

    narada-restore path/to/backup.full.tar [files]


=head1 DESCRIPTION

Should be executed in project deploy directory.

Will extract full project or just given files from backup created with
C<narada-backup>.

When given, file names must be the full path from project deploy directory,
without wildcards. Values C<a/b>, C</a/b> and C<./a/b> are all handled in
same way (as C<./a/b>).


=head1 CONFIGURATION AND ENVIRONMENT

None.


=head1 SUPPORT

=head2 Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker
at L<https://github.com/powerman/Narada/issues>.
You will be notified automatically of any progress on your issue.

=head2 Source Code

This is open source software. The code repository is available for
public review and contribution under the terms of the license.
Feel free to fork the repository and submit pull requests.

L<https://github.com/powerman/Narada>

    git clone https://github.com/powerman/Narada.git

=head2 Resources

=over

=item * MetaCPAN Search

L<https://metacpan.org/search?q=Narada>

=item * CPAN Ratings

L<http://cpanratings.perl.org/dist/Narada>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Narada>

=item * CPAN Testers Matrix

L<http://matrix.cpantesters.org/?dist=Narada>

=item * CPANTS: A CPAN Testing Service (Kwalitee)

L<http://cpants.cpanauthors.org/dist/Narada>

=back


=head1 AUTHOR

Alex Efros E<lt>powerman@cpan.orgE<gt>


=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2008-2015 by Alex Efros E<lt>powerman@cpan.orgE<gt>.

This is free software, licensed under:

  The MIT (X11) License


=cut