#!/usr/bin/python -tt

import re

DOCUMENTATION = '''
---
module: cpan
short_description: Install perl libs with I(cpan)
description:
     - Installs perl libs with I(cpan)
     - Only installs.  Does not uninstall or offer configuration.  If you need to configure cpan, do so before using this module.
     - Will hang indefinitely if the module wants input (not uncommon!)
     - Make sure you know how the module's install behaves before trying to use this module with it!
options:
  name:
    description:
      - "Name of the perl lib as expected by cpan command line tool, like C(Archive::Tar). 
    required: true
    default: null
author: Dylan Martin
'''
EXAMPLES = '''
- cpan: name=Archive::Tar
'''

def main():
  module = AnsibleModule(
    argument_spec = dict(
      name=dict(aliases=['pkg']),
    ),
  )
  pkg = module.params['name']
  rc,out,err = module.run_command('cpan "%s"' % pkg)
  if rc == 0:
    if re.search('up to date',out,re.M):
      module.exit_json(out=out, err=err, failed=False, changed=False, rc=rc)
    if re.search('Cannot install',out,re.M):
      module.fail_json( rc=rc, out=out, msg='Cannot install %s' % pkg )
    if re.search('OK$',out):
      module.exit_json(out=out, err=err, failed=False, changed=True, rc=rc)

    module.exit_fail(rc=rc, out=out, err=err, msg="I don't know what just happened")

  module.fail_json( rc=rc, out=out, msg=err )

# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()

