#!/usr/bin/python
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import sys
import urllib
import httplib
import phacter
import argparse

def updateFacts(self):
    f = self.getFacts()
    url = f['dealeraddress']
    params = urllib.urlencode(self.getFacts())
    headers = { "Content-type" : "application/x-www-form-urlencoded",
                "Accept" : "text/plain" }
    conn = httplib.HTTPConnection(url)
    conn.request('POST','/index.php',params,headers)
    response = conn.getresponse()
    print response.status,response.reason
    data = response.read()
    conn.close
    return data
        
def parse_args():
    desc_txt = 'Phacter reports facts (phacts) about a system. If a ' + \
               'phact is not specified then all phacts will be returned. ' + \
               'Multiple phacts can be passed. Phacter is python.'

    parser = argparse.ArgumentParser(description=desc_txt)
    parser.add_argument('-l', '--list',
        action='store_true', default=False, required=False,
        help='Only list the phacts, don\'t evauate their values.')
    #parser.add_argument('-y', '--yaml',
    #    action='store_true', default=False, required=False,
    #    help='Return phact list in yaml. Requires PyYAML')
    #parser.add_argument('-j', '--json',
    #    action='store_true', default=False, required=False,
    #    help='Return phact list in json.')
    parser.add_argument('-v', '--version', dest='version',
        action='store_true', default=False, required=False,
        help='Print the version and exit.')
    parser.add_argument('phact', nargs='*',
        help='phact name, use --list for a list of phacts.')

    return parser.parse_args()


def main():
    args = parse_args()

    if args.version:
        print phacter.phacterversion
        sys.exit()

    if len(sys.argv) == 1:
        for phact in phacter.phacts:
            print '%s => %s' % (phact, phacter[phact])
    elif sys.argv[2:]:
        for phact in sys.argv[2:]:
            print '%s => %s' % (phact, phacter[phact])
    else:
        print phacter[sys.argv[1]]
    
if __name__ == '__main__':
    main()
