#!/usr/bin/perl -w
# $Id: pwdInfo,v 1.4 2013/07/17 19:34:07 tom Exp $

#
# This reads the passwd file and creates an interface to browse the
# information.
#

use strict;
use Cdk;
Cdk::init();

#
# This function loads up the passwd file and returns a reference to the
# data structure.
#
sub loadPasswd {
    my %passwd = ();
    my @logins = ();
    my ( $name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell );

    # Start reading through the passwd file.
    while (
        ( $name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell ) =
        getpwent )
    {

        # Store the information.
        $passwd{$name} = "$uid$gid$comment$gcos$dir$shell";
        push( @logins, $name );
    }

    return ( \@logins, \%passwd );
}

# Load up the passwd file.
my ( $logins, $passwdObject ) = loadPasswd();

# Create a scrolling list of all the names.
my $mainScroll = new Cdk::Scroll(
    'Title'   => '<C>Pick An Account',
    'Numbers' => "TRUE",
    'List'    => $logins,
    'Height'  => 13,
    'Width'   => 45
);

# Do this forever.
for ( ; ; ) {

    # Let the user select the login to view.
    my $selected = $mainScroll->activate();

    # Did the user just hit escape?
    if ( !defined $selected ) {
        Cdk::end();
        exit 0;
    }

    # Get the name and display the info.
    my $name = $logins->[$selected];

    # Display the info.
    my ( $uid, $gid, $comment, $gcos, $dir, $shell ) =
      split( //, $passwdObject->{$name} );
    my $info = [
        "Account Name   : </R>$name",
        "UID/GID        : ($uid/$gid)",
        "Comment        : </R>$comment",
        "GCOS Value     : </R>$gcos",
        "Home Directory : </R>$dir",
        "Shell          : </R>$shell"
    ];

    my $title = new Cdk::Label( 'Message' => $info );

    # Draw the label.
    $title->draw( 'Box' => "TRUE" );
    $title->wait();
    undef $title;
}
