#!/usr/bin/python -tt

import re

DOCUMENTATION = '''
---
module: pear
short_description: Install PHP libs with I(pear)
description:
     - Installs PHP libs with I(pear)
     - Only installs.  Does not uninstall or offer configuration.  If you need to configure pear, do so before using this module.
options:
  name:
    description:
      - "Name of the pear lib as expected by pear command line tool, like C(Archive_Tar). 
    required: true
    default: null
author: Dylan Martin
'''
EXAMPLES = '''
- pear: name=Archive_Tar
'''

def main():
  module = AnsibleModule(
    argument_spec = dict(
      name=dict(aliases=['pkg']),
    ),
  )
  pkg = module.params['name']
  rc,out,err = module.run_command('pear install "%s"' % pkg)
  if rc == 0:
      module.exit_json(rc=rc, out=out, err=err, changed=True, failed=False)
  if err == '' and rc == 1:
    if re.search('is already installed and is the same as the released version', out, re.M):
      module.exit_json(out=out, err=err, failed=False, changed=False)
    module.fail_json(rc=rc, out=out, msg=err, failed=True)

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

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

