#!/usr/bin/perl
#date -u +%Y%m%d-%H%M
use Getopt::Long;
use POSIX qw(strftime);
use strict;
use warnings;

my $annotate = 0;
my $signed = 0;
my $signkey = 0;
my $force = 0;
my $msg = 0;
my $file = 0;
my $delete = 0;
my $num = 0;
my $list = 0;
my $contains = 0;
my $verify = 0;
# custom options:
my $debug  = 0;

GetOptions(
	"annotate|a" => \$annotate,
	"signed|s" => \$signed,
	"u=s" => \$signkey,
	"force|f" => \$force,
	"msg|message|m=s" => \$msg,
	"file|F=s" => \$file,
	"delete|d" => \$delete,
	"num|n=i" => \$num,
	"list|l" => \$list,
	"contains=s" => \$contains,
	"verify|v" => \$verify,

	"debug" => \$debug,
) or die "Failed to parse options";


die "You MUST include a message with -m!" unless $msg or $file;

my @opts;
push @opts, "-a" if $annotate;
push @opts, "-u", $signkey if $signkey;
push @opts, "-f" if $force;
push @opts, "-m", $msg if $msg;
push @opts, "-F", $file if $file;

if($delete) {
	die "Cannot use -d and other options" if scalar(@opts) > 0;
	push @opts, "-d";
}
if($verify) {
	die "Cannot use -v and other options" if scalar(@opts) > 0;
	push @opts, "-v";
}
if($list) {
	die "Cannot use -l and other options" if scalar(@opts) > 0;
	push @opts, "-n", $num if $num;
	push @opts, "-l";
	push @opts, "--contains", $contains if $contains;
}

if(scalar(@ARGV) == 0) {
	push @ARGV, (strftime "production-%Y%m%d-%H%M", gmtime);
}

if($debug) {
	print "ARGV: ".join(' ',@ARGV)."\n";
	print "Command: ".join(" ", 'git', 'tag', map { sprintf("'%s'", $_) } @opts, @ARGV )."\n";
} else {
	system('git', 'tag', @opts, @ARGV )
}
