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

warn()  { echo "$@" >&2;        }
die()   { warn "$@"; exit 1;    }
usage() { die "Usage: narada-new [-r base_repository] [-b branch] [directory]"; }


INITIAL_VERSION="0.0.0"
dir="."
repo="https://github.com/powerman/narada-base.git"
branch="master"

while test $# -ne 0; do
        if test "$1" = '-r' -a $# -ge 2; then
                repo="${2:-$repo}"
                shift 2
        elif test "$1" = "-b" -a $# -ge 2; then
                branch="${2:-master}"
                shift 2
        elif test "$1" = '-h' -o "$1" = '--help'; then
                usage
        elif test $# -eq 1; then
                dir="${1:-.}"
                shift
        else
                usage
        fi
done

if test -d "$dir"; then
        if test "$(ls -a "$dir" | wc -l)" -ne "2"; then
                die "Target directory '$dir' is not empty"
        fi
else
        mkdir "$dir" || exit 1
fi

set -ex
cd "$dir"
git init
test -n "$NONINTERACTIVE_TESTING" && git config commit.gpgsign false
git commit --allow-empty -m 'empty'
git tag -a -m empty "$INITIAL_VERSION"
git remote add narada-base -t "$branch" -m "$branch" -f "$repo"
git merge -h 2>&1 | grep -q allow-unrelated-histories && mergeopt="--allow-unrelated-histories"
git merge $mergeopt narada-base -m 'narada-new: merge narada-base'


: <<'=cut'

=encoding utf8

=head1 NAME

narada-new - creates a new project based on Narada framework


=head1 VERSION

This document describes narada-new version v2.3.8


=head1 USAGE

    narada-new [-r base_repository] [-b branch] [directory]


=head1 DESCRIPTION

This script help you create new project directory for using Narada framework.

Target C<directory> shouldn't exists or must be empty.

If target directory doesn't exists - it will be created (parent directory must
exists).

If executed without param will create new project in current directory, which
must be empty.

Target directory become source directory of Narada project. By default it will
also contain C<_live/> subdirectory for testing project while development, that
subdirectory become deploy directory of Narada project. All other C<narada-*>
commands should be executed in (one of) these directories.

It will merge C<base_repository>'s C<branch>. Default base repository is
L<https://github.com/powerman/narada-base.git>; default branch is "master".

After that you usually should run C<./release --minor> and C<./deploy> to
deploy it to C<_live/>.


=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- by Alex Efros E<lt>powerman@cpan.orgE<gt>.

This is free software, licensed under:

  The MIT (X11) License


=cut