#!/usr/bin/env python
#
# Copyright (c) 2007 Jan Dittberner
# $Id$
#
import getopt, sys
from gnuviechadmin.dblayer import *

def usage():
  print """Usage information:
  =====================
  %(process)s -h|--help
  - prints this help text

  %(process)s --type=admin|reseller|client --clientid=<clientid> \
   [--name=<name>] [--home=<home>] [--shell=<shell>] [--password] \
   [--sysuid=<uid>]
  - adds a new system user
  """ % {'process': sys.argv[0]}

if __name__ == "__main__":
  try:
    (options, args) = getopt.getopt(sys.argv[1:], "h",
                                    ['help',
                                     'type=', 'clientid=', 'name=', 'home=',
                                     'shell=', 'password=', 'sysuid='])
  except getopt.GetoptError:
    usage()
    sys.exit(1)

  if (not options or
      dict(options).has_key('-h') or
      dict(options).has_key('--help') or
      not dict(options).has_key('--type') or
      not dict(options).has_key('--clientid') or
      not dict(options)['--type'].strip() or
      not dict(options)['--clientid'].strip() or
      not dict(options)['--type'].strip() in ('admin', 'reseller', 'client')):
    usage()
    sys.exit(1)

  query = session.query(Client)
  client = query.get_by(clientid = dict(options)['--clientid'].strip())
  if not client:
    print "Invalid client"
    allclients = query.select()
    if allclients:
        print "Valid clients are:\n- %s" % "\n- ".join([str(client) for client in allclients])
    else:
        print "No clients defined yet."
    sys.exit(1)

  print client.sysusers