#!/usr/bin/perl --
# 
# $Id: rndpassword 13 2008-06-14 10:28:53Z ryo $

use strict;
use Getopt::Std;
use Pod::Usage;
use Data::SimplePassword;

# getopts
Getopt::Std::getopts 'ht:n:' => my $opt = {};

# extra args
&HELP_MESSAGE if scalar @ARGV;

# help
&HELP_MESSAGE if defined $opt->{h};

# length
my $n = $opt->{n} || 8;
&usage unless $n =~ /^\d+$/o;

# type
my $chars = {
    default => [ 0..9, 'a'..'z', 'A'..'Z' ],
    base64 => [ 0..9, 'a'..'z', 'A'..'Z', qw(+ /) ],
    simple => [ 0..9, 'a'..'z' ],
    digit => [ 0..9 ],
    binary => [ 0, 1 ],
};

my $type = $opt->{t} || 'default';
&usage unless scalar grep { $type eq $_ } keys %{ $chars };

my $sp = Data::SimplePassword->new;
$sp->chars( @{ $chars->{ $type } } );

print $sp->make_password( $n ), "\n";

*usage = \&HELP_MESSAGE;

sub HELP_MESSAGE {
    pod2usage 0;
}

__END__

=head1 NAME

rndpassword - a command-line frontend to Data::SimplePassword

=head1 SYNOPSIS

rndpassword [-h] [-t (default|base64|simple|digit|binary)] [-n <length>]

 Options:
    -h  show usage
    -t  set charactor type
    -n  set length (default: 8 byte)

 Examples:
    shell> rndpassword
    shell> rndpassword -n 12
    shell> rndpassword -t digit -n 32
    shell> rndpassword -t binary -n 1024

=head1 DEPENDENCY

Data::SimplePassword

