#!/usr/bin/env python

# Author: Stefan Schwarzer, 2001

'''userdata - fetch data of users by their userid, name, lastname, or
mailname and display it on standard output. The command uses Suns
NIS to fetch the passwd user data (key: passwd.byname).

Usage:

userdata -h

userdata [filter] [-d userdb] [-s order]
  where filter is one of -u userid, -n name, -l lastname, or -m mailname

The first form prints this help and exits.

The second form uses one of the -u, -n, -l or -m options to specify a
filter rule. See above for the allowed options. Only user data items which
match the rule will be displayed. Matching means identity for userid, and
containment of the filter value in name, lastname, or mailname. You may
use -x (without a value after it) to look for users without mailname. In
this case, the use of -d is obligatory.

The option -s with one of the values userid, name, lastname, or mailname
can be used to sort the output by the specified field.

If a sendmail userdb file is given with the option -d, the output will
contain also the mailadresse(s) of the respective users. Of course,
filtering or sorting by mailnames won't make sense without using -d to
read the mail addresses. :-)

Examples:

userdata
  display all user data without email addresses (i. e. only userid and
  realname)

userdata  -l 'Schwarzer'  -s name  -d /path/to/userdb
  show user data where the lastname contains 'Schwarzer'. The email
  adresses are supplied by the file /path/to/userdb. The output is
  sorted by name (effectively by firstname).'''

import sys, getopt, user_data_list

def error( msg ):
    print 'userdata:', msg
    sys.exit()

def main():
    # get options
    try:
        options, args = getopt.getopt( sys.argv[1:], 'hu:n:l:m:xs:d:' )
    except getopt.error, msg:
        print __doc__
        print '\nuserdata:', str(msg)
        sys.exit()
    # defaults
    filter = None               # 1 if a filter was defined
    filter_fieldname = None     # userid, name, lastname, mailname, or nomail
    userdb = None               # path of the userdb file
    sort_order = None           # userid, name, lastname, or mailname
    # process options
    for opt, val in options:
        if opt == '-h':
            print __doc__
            sys.exit()
        if opt[1] in 'unlm':    # check for options -u, -n, -l, and -m
            if filter:
                error( "only a single filter is allowed (one of u, n, l, m, x)" )
            filter = 1
            filter_dict = { 'u': 'userid', 'n': 'name', 'l': 'lastname',
              'm': 'mailname' }
            filter_fieldname, filter_value = filter_dict[ opt[1] ], val
        if opt == '-x':
            if filter:
                error( "only a single filter is allowed (one of u, n, l, m, x)" )
            filter = 1
            filter_fieldname, filter_value = 'nomail', 'ignored'
        if opt == '-d':
            userdb = val
        if opt == '-s':
            sort_order = val
    # more checks for valid input
    if not userdb:
        if filter_fieldname == 'nomail':
            error( "you can't specify -x without -d userdb" )
        elif filter_fieldname == 'mailname':
            error( "you can't specify -m without -d userdb" )
        elif sort_order == 'mailname':
            error( "you can't specify -s mailname without -d userdb" )
    # perform desired program function
    try:
        user_list = user_data_list.UserDataList( source='NIS' )
    except user_data_list.nis.error, msg:
        error( 'NIS error ' + str(msg) )
    if userdb:
        try:
            user_list.update_mailnames( userdb )
        except IOError, msg:
            error( msg )
    if filter:
        user_list.filter_by( filter_fieldname, filter_value )
    if sort_order:
        try:
            user_list.sort_by( sort_order )
        except ValueError, msg:
            error( msg )
    print user_list
    
    
if __name__ == '__main__':
    main()