#!/usr/bin/perl

# Created on: 2011-06-21 09:46:35
# Create by:  Ivan Wills
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$

use strict;
use warnings;
use Getopt::Long;
use English qw/ -no_match_vars /;
use App::PS1;

eval { require Term::Size::Any };
my $tsa = !$EVAL_ERROR;
if ($tsa) {
    Term::Size::Any->import(qw/chars/);
}

our $VERSION = 0.002;
my ($name)   = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;

my %option = (
    ps1     => $ENV{APP_PS1} || 'face;branch;date;directory;uptime',
    theme   => $ENV{APP_PS1_THEME} || 'default',
    verbose => 0,
    man     => 0,
    help    => 0,
    VERSION => 0,
);

main();
exit 0;

sub main {

    Getopt::Long::Configure('bundling');
    GetOptions(
        \%option,
        'ps1|p=s',
        'exit|e=s',
        '256',
        '16|low',
        'bw|black-white|2|b!',
        'theme|t=s',
        'quick_start|quick-start|q',
        'verbose|v+',
        'man',
        'help',
        'VERSION!',
    ) or pod2usage(2);

    if ( $option{'VERSION'} ) {
        print "$name Version = $VERSION\n";
        exit 1;
    }
    elsif ( $option{'man'} ) {
        pod2usage( -verbose => 2 );
    }
    elsif ( $option{'help'} ) {
        pod2usage( -verbose => 1 );
    }

    # do stuff here
    return quick_start() if $option{quick_start};

    my @chars = term_chars();
    my $ps1 = App::PS1->new({
        ps1   => $option{ps1},
        low   => $option{16},
        bw    => $option{bw},
        theme => $option{theme},
        exit  => $option{exit},
        cols  => $chars[0],
    });

    print $ps1->cmd_prompt;

    return;
}

sub quick_start {
    print <<'QS';
export PS1='\[\033[0m\]\[`ps1 -e $?`\]\n\[\033[1;32m\]\u\[\033[0m\]\[\033[0;33m\]\[\033[01;22m\]@\[\033[0m\]\[\033[1;35m\]\h \[\033[0m\]\$ '; export PATH=~/bin:$PATH; export TM_OUT=0; alias l='ls --ignore=.*.sw? -lAF -h'; alias gfind='find . | /bin/grep -v ".svn" | grep -r -P --color'
alias ghistory='history | /bin/grep -r -P --color'; alias gps='ps aux | head -1; ps aux | grep -v " /bin/grep " | grep -r -P --color'; alias grep='/bin/grep -r -P --color'; alias less='less -R'; shopt -s checkwinsize; shopt -s cdspell
QS
    return;
}

sub term_chars {
    my @chars;
    @chars = chars() if $tsa;
    @chars = (
        $ENV{COLUMNS} || $ENV{PS1_COLS} || 90,
        $ENV{LINES}   || $ENV{PS1_ROWS} || 40
    ) if !@chars;
    return @chars;
}

sub pod2usage {
    require Pod::Usage;
    Pod::Usage::pod2usage(@_);
}

__DATA__

=head1 NAME

app-ps1 - Command prompt generator

=head1 VERSION

This documentation refers to app-ps1 version 0.002.

=head1 SYNOPSIS

   app-ps1 [option]

 OPTIONS:
  -p --ps1[=]str    The prompt string (Default $ENV{APP_PS1} || 'face;branch;date;directory;uptime')
  -e --exit[=]str   Last program's exit code
  -q --quick_start  A quick start helper
     --256          Show colours for 256 colour terminal (Default)
     --16           Terminal only supports 16 colours
     --2            Terminal does no support colour

  -v --verbose       Show more detailed option
     --version       Prints the version information
     --help          Prints this help information
     --man           Prints the full documentation for app-ps1

=head1 DESCRIPTION

=head1 SUBROUTINES/METHODS

=head1 DIAGNOSTICS

=head1 CONFIGURATION AND ENVIRONMENT

=head1 DEPENDENCIES

=head1 INCOMPATIBILITIES

=head1 BUGS AND LIMITATIONS

There are no known bugs in this module.

Please report problems to Ivan Wills (ivan.wills@gmail.com).

Patches are welcome.

=head1 AUTHOR

Ivan Wills - (ivan.wills@gmail.com)

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2011 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
All rights reserved.

This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. See L<perlartistic>.  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.

=cut
