#! /usr/bin/perl

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2016 - 2017 Raphaël Gertz <acmepl@rapsys.eu>

# Best practice
use strict;
use warnings;

# Load acme
use acme;

# Load POSIX
use POSIX qw(EXIT_SUCCESS EXIT_FAILURE);

# Init debug
my $debug = 0;

# Init prod
my $prod = 0;

# Strip and enable debug
@ARGV = map { if ($_ eq '-d') { $debug = 1; (); } else { $_; } } @ARGV;

# Strip and enable prod
@ARGV = map { if ($_ eq '-p') { $prod = 1; (); } else { $_; } } @ARGV;

# Show usage
if (scalar(@ARGV) < 2) {
	print "Usage: $0 user\@example.com www.example.com [example.com] [...]\n";
	exit EXIT_FAILURE;
}

# Create new object
my $acme = acme->new(shift @ARGV, $debug, $prod, @ARGV);

# Prepare environement
$acme->prepare();

# Generate required keys
$acme->genKeys();

# Generate csr
$acme->genCsr();

# Directory
$acme->directory();

# Register
$acme->register();

# Authorize
$acme->authorize();

# Issue
$acme->issue();

# Exit with success
exit EXIT_SUCCESS;