#!/usr/bin/perl

use warnings;
use strict;

use Getopt::Long;
use Topbloke;

Getopt::Long::Configure(qw(bundling));

die "bad usage\n" unless @ARGV==1;

our $spec = parse_branch_spec($ARGV[0]);
our $current = current_tb_branch();

die "cannot make branch starting at base of another;".
    " check out a real branch\n" if $current->{Kind} eq 'base';

die "strange branch ref $current->{Kind} $current->{Ref},\n".
    " making new branch with this as dep is unwise\n"
    unless ($current->{Kind} eq 'foreign' ||
	    $current->{Kind} eq 'tip');

sub fillin ($$$) {
    my ($key, $newval, $what) = @_;
    return if defined $spec->{$key};
    $spec->{$key} = $newval;
}

if (!defined $spec->{Email} || !defined $spec->{Domain}) {
    my $eaddr = run_git_1line(qw(config user.email));
    $eaddr =~ m/^(.*)\@/ or die "$eaddr ?";
    fillin('Email',$1,'email domain');
    fillin('Domain',$','email domain'); #');
}

if (!defined $spec->{Date}) {
    $spec->{Date} = `LC_TIME=C date -u +%Y-%m-%dT%H%M%SZ`;
    chomp $spec->{Date} or die $!;
}

length($spec->{Date})==18 or die "partial date specified, not supported\n";

my $newbranch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";

$newbranch = run_git_1line(qw(check-ref-format --print), $newbranch);

printf "creating %s\n", $newbranch;
