The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/env perl
use 5.12.1;

use Yukki;

use Git::Repository v1.18;

my $repo_name   = shift;
my $remote_repo = shift;

die "usage: $0 repository-name [ remote-repository-url ]\n"
    unless $repo_name;

my $app = Yukki->new;

my $config = $app->settings->{repositories}{ $repo_name };

die qq[Could not find o repository named "$repo_name" in yukki.conf\n]
    unless $config;

my $repo_dir = ''.$app->locate('repository_path', $config->{repository});

if (defined $remote_repo) {
    Git::Repository->create('clone', '--mirror', 
        $remote_repo, $repo_dir);
}

else {
    my $title  = $config->{name} // ucfirst($repo_name);
    my $page   = $config->{default_page} // 'home.yukki';
    my $branch = $config->{site_branch} // 'refs/heads/master';

    my $stub_main = <<END_OF_STUB_MAIN;
# $title

Welcome to your new wiki repository. The first thing you will probably
want to do is edit this page.

Cheers.

END_OF_STUB_MAIN

    Git::Repository->run('init', '--bare', $repo_dir);
    my $git = Git::Repository->new(git_dir => $repo_dir);

    my $object_id = $git->run('hash-object', '-t', 'blob', '-w', '--stdin', "--path=$page", { input => $stub_main });

    my $stub_tree = "100655 blob $object_id\t$page\n";
    my $tree_id   = $git->run('mktree', { input => $stub_tree });
    my $commit_id = $git->run('commit-tree', $tree_id, { 
        input => 'Initializing empty Yukki repository.',
        env   => {
            GIT_AUTHOR_NAME  => 'yukki-git-init',
            GIT_AUTHOR_EMAIL => 'hanenkamp@cpan.org',
        },
    });

    $git->run('update-ref', $branch, $commit_id, '0' x 40);
}

# ABSTRACT: Initialize a git repository for use with Yukki
# PODNAME: yukki-git-init

__END__

=pod

=head1 NAME

yukki-git-init - Initialize a git repository for use with Yukki

=head1 VERSION

version 0.140290

=head1 SYNOPSIS

  yukki-git-init repository
  yukki-git-init repository remote-repository

=head1 DESCRIPTION

This will initialize a git repository for use with Yukki. The script must be
called with at least one argument and accepts an optional second argument.

=over

=item repository

The repository is the name of the repository to initalized. It should be one of
the repository keys in the C<repositories> section of the F<yukki.conf>.

=item remote-repository

This is a git path to a repository to clone as a mirror for use with Yukki.

=back

=head1 ENVIRONMENT

Normally, this script tries to find F<etc/yukki.conf> from the current working
directory. If no configuraiton file is found, it checks C<YUKKI_CONFIG> for the
path to this file.

=head1 AUTHOR

Andrew Sterling Hanenkamp <hanenkamp@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Qubling Software LLC.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut