#!/usr/bin/python
# -*- coding: UTF-8 -*-
# checksysreport, a tool to spot unsupported issues.
# Copyright 2005 Pierre Amadio 
# pamadio@redhat.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
import sys
sys.path.append('/usr/share/checksysreport')
sys.path.append('/usr/share/checksysreport/plugins')
#print sys.path
#Would the following url be a way to worakround the https/proxy problem ?
# http://gregscomputerservice.com/cgi-bin/dwww?type=file&location=/usr/share/doc/python2.3-pycurl/examples/xmlrpc_curl.py
#
# This is usefull when using svn on 108: 
# svn propset  svn:mime-type "image/jpeg" www/yadarh-thumb.jpg
# svn propset svn:eol-style "native" drafts/threadcache/fillRhnCache.py
#
import fnmatch
import re
import os
import shutil
import string
import sysreportChecker
import globals
#import getpass
#As optparse does not seems to exist on RHEL3, lets use optik.
try:
    from optparse import OptionParser, Option
except ImportError:
    from optik import OptionParser


plugin_dic={}

def plugin_opt_callback(option, opt_str, value, parser):
    plugname=value[0]
    plugargkey=value[1]
    plugargval=value[2]
    if plugname not in plugin_dic:
        plugin_dic[plugname]=[]
    plugin_dic[plugname].append({'key':plugargkey,'value':plugargval})



parser = OptionParser()
parser.add_option("-a","--arch",action="store",type="string",dest="arch",\
                  metavar='ARCH',\
                  help="force architecture: (ia64 i386 x86_64 ppc s390 s390x)")
parser.add_option("-c","--list-channels",action="store_true",\
                  dest="listChannels",help="list existing channels")
parser.add_option("-d","--diff-changelog",action="append",dest="pidToDiff",\
                  help="one of 2 package_id to compare changelog from")
parser.add_option("-f","--full-changelog",action="store_true",\
                  dest="fullChangelog", \
                  help="display changelogs diff for all packages")
parser.add_option("-n","--no-changelog",action="store_true",dest="noChangelog",
                  help="dont bother dealing with changelogs at all.")
parser.add_option("-p","--plugin",action="callback",dest="pluginOpt",\
                 type="string",callback=plugin_opt_callback,nargs=3,\
                  help="pluginname pluginargname pluginargvalue\n\
                  can be used several times." )
parser.add_option("-r","--list-packages",action="store",type="string",\
                  dest="listPackages",metavar='CHANNEL',\
                  help="list up2date packages for a given channel")
parser.add_option("-s", "--sysreport",
                  action="store", type="string", dest="sysreportDir",
                  help="path to the sysreport directory", metavar="SYSREPORT")                 
parser.add_option("-t","--list-all-packages",action="store",type="string",\
                  dest="listAllPackages",metavar='CHANNEL',\
                  help="list all packages existing for a given channel")
parser.add_option("-z","--z-stream",action="store",metavar="ZSTREAM",\
                  dest="zstream")

(options, args) = parser.parse_args()

def init_stuff():
    """
    behave according to options used. If -s is used, it return a correct
    subclasse of sysreportChecker
    """
    #print "avant=",options
    if options.pidToDiff:
        checker=sysreportChecker.sysreportChecker(sysreportPath='',action=2,options=options)

    if options.listChannels:
        globals.displayListChannels()
        globals.die('')

    if options.listPackages:
        chanLabel=options.listPackages
        globals.displayListPackages(chanLabel)
        globals.die('')

    if options.listAllPackages:
        chanLabel=options.listAllPackages
        globals.displayListAllPackages(chanLabel)
        globals.die('')
        
    if options.sysreportDir==None:
        globals.die("No sysreport specified, use the -s option")
    else:
        release=globals.detect_release(options.sysreportDir)

        globals.sosReportCompat(options.sysreportDir)
        releaseDetected=0
        if release=='2.1':
            checker=sysreportChecker.sysreportChecker_2(sysreportPath=options.sysreportDir,options=options)
            releaseDetected=1
        if release=='3':
            checker=sysreportChecker.sysreportChecker_3(sysreportPath=options.sysreportDir,options=options)
            releaseDetected=1
        if release=='4':
            checker=sysreportChecker.sysreportChecker_4(sysreportPath=options.sysreportDir,options=options)
            releaseDetected=1

        if release=='5':
            checker=sysreportChecker.sysreportChecker_5(sysreportPath=options.sysreportDir,options=options)
            releaseDetected=1

        if release=='6':
            checker=sysreportChecker.sysreportChecker_6(sysreportPath=options.sysreportDir,options=options)
            releaseDetected=1

        if releaseDetected==0:
            globals.die("Unable to detect Red Hat Release Version")

        return checker


###########################
#  Real stuff starts here #
###########################

checker=init_stuff()

for cur_plug in globals.config["mandatory_plugin"]:
    if cur_plug not in plugin_dic.keys():
        plugin_dic[cur_plug]=[]
    for cur_dic in globals.config['mandatory_plugin'][cur_plug]:
        plugin_dic[cur_plug].append(cur_dic)




data=globals.generate_report(checker,options,plugin_dic)
try:
    print data
except:
    print data.encode('utf8')


