'''
Created on Jan 6, 2013

__author__ = "Elizabeth 'pidge' Flanagan"
__copyright__ = "Copyright 2012-2013, Intel Corp."
__credits__ = ["Elizabeth Flanagan"]
__license__ = "GPL"
__version__ = "2.0"
__maintainer__ = "Elizabeth Flanagan"
__email__ = "elizabeth.flanagan@intel.com"
'''
from buildbot.steps.shell import ShellCommand
from autobuilder.config import *

class SendErrorReport(ShellCommand):
    haltOnFailure = False
    flunkOnFailure = True
    name = "SendErrorReport"
    def __init__(self, factory, argdict=None, **kwargs):
        self.factory = factory
        self.workerdir=os.path.join(os.path.join(YOCTO_ABBASE, "yocto-worker"))
        for k, v in argdict.iteritems():
            setattr(self, k, v)
        # Timeout needs to be passed to LoggingBuildStep as a kwarg
        self.timeout = 100000
        kwargs['timeout']=self.timeout
        ShellCommand.__init__(self, **kwargs)

    def start(self):
        self.workerdir=os.path.join(os.path.join(YOCTO_ABBASE, "yocto-worker"))
        self.buildername=self.getProperty("buildername")
        self.buildnumber=self.getProperty("buildnumber")
        self.layerversion_core = self.getProperty("layerversion_core")
        if os.environ.get('ERROR_REPORT_DIR') is None:
            self.errordir=os.path.join(os.path.join(
                            self.workerdir, self.buildername),
                            "build/build/tmp/log/error-report/")
        else:
            self.errordir=os.environ.get('ERROR_REPORT_DIR')
        if int(self.layerversion_core) < 4:
            self.command = "echo 'Skipping Step.'"
        else:
            if str(os.environ.get('ERROR_REPORT_COLLECT')) == "True":
                self.command = "if [ ! -f ~/.oe-send-error ]; then host=`hostname`; echo \"yocto-autobuilder-autogenerated-$host\" > ~/.oe-send-error; fi ; "
                self.command += ". ./oe-init-build-env; if [ -d " + self.errordir + " ]; then for x in `ls " + self.errordir + " | grep error_report_`;"
                if int(self.layerversion_core) == 4:
                    if os.environ.get('ERROR_REPORT_SERVER') is not None:
                        self.er_server = str(os.environ.get('ERROR_REPORT_SERVER'))
                    else:
                        self.er_server = ""
                    self.command += "do send-error-report " + self.errordir +"/$x"+ self.er_server + "; done; fi"
                elif int(self.layerversion_core) == 5:
                    master = self.build.builder.botmaster.parent
                    self.er_link = " -l " + master.status.getURLForBuild(self.buildername, self.buildnumber)
                    if os.environ.get('ERROR_REPORT_SERVER') is not None:
                        self.er_server = " -s " + str(os.environ.get('ERROR_REPORT_SERVER'))
                    else:
                        self.er_server = ""
                    if os.environ.get('ERROR_REPORT_EMAIL') is not None:
                        self.er_email = " -e " + str(os.environ.get('ERROR_REPORT_EMAIL'))
                    else:
                        self.er_email = ""
                    if os.environ.get('ERROR_REPORT_NAME') is not None:
                        self.er_name = " -n " + str(os.environ.get('ERROR_REPORT_NAME'))
                    else:
                        self.er_name = ""
                    self.command += "do send-error-report -y "
                    self.command += self.er_server + self.er_email
                    self.command += self.er_name +  self.er_link
                    self.command +=  " " + self.errordir +"/$x; done; fi"
            else:
                self.command = "echo environment var ERROR_REPORT_COLLECT not set - Step is noop"
            self.description = ["Sending error reports"]
        ShellCommand.start(self)


