#!perl

#PODNAME: tinyid
use strict;
use warnings;

use Getopt::Long::Descriptive;
use Integer::Tiny;
use v5.10;

my $key = 'Wel0v3you';
my ( $opt, $usage ) = describe_options(
    "tinyid %o",
    [ 'text|t=s', 'Text to be encrypted.', { required => 1 } ],
    [
        'key|k=s',
'Overrides default key value "WEl0v3you" with your key of choice. Note that you cannot include duplicated characters, but only single unique character per text.',
        { default => 'WEl0v3you' }
    ],
    [
        'encrypt|e',
        'This encrypts numerical value. You must include --text or -t too.'
    ],
    [
        'decrypt|d',
        'This decrypts the text. You must include --text or -t too.'
    ],
    [ 'help|h', 'Print this message and exit.', { shortcircuit => 1 } ]
);

say( $usage->text ), exit 0 if $opt->help;

my $intiny = Integer::Tiny->new( $opt->key );

if ( not $opt->text ) {
    warn '--text/-t cannot be empty. Try again.';
}

if ( $opt->encrypt and ( $opt->text =~ /^[0-9]+$/ ) ) {
    say $intiny->encrypt( $opt->text );
}

elsif ( $opt->encrypt and ( $opt->text !~ /^[0-9]+$/ ) ) {
    warn 'You can only encrypt integer values!. Please try again.';
    exit 1;
}

elsif ( $opt->decrypt ) {
    say $intiny->decrypt( $opt->text );
}

else {
    warn
'Error occured. The program is not supplied with --encrypt/-e or --decrypt/-d argument.';
    exit 1;
}
