#!/usr/bin/python

# Copyright (C) 2008-2010 Oracle and/or its affiliates. All rights reserved.
# Author: Nelson Elhage
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, 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., 51 Franklin Street - Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# Not a Contribution for purposes of the Fedora Project Individual Contributor
# License Agreement

import sys
# Disable Launchpad's apport traceback hooks
sys.excepthook = sys.__excepthook__

import Uptrack
import pycurl
import urllib
import posixpath
import yaml
import datetime

cfg = Uptrack.UptrackConfig()
Uptrack.initCurl(cfg)
oops = sys.stdin.read()

curl = Uptrack.getCurl()

cfg.initWithLock()

url = posixpath.join(cfg.remote, urllib.quote('oops'))

data = {}
data['oopsdata'] = oops
data['Time'] = datetime.datetime.utcnow()
Uptrack.Status.addIdentity(cfg, data)

data = Uptrack.yaml_dump(data, version=(1, 1),
                         explicit_start = True, explicit_end = True)

curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.HTTPPOST, [("info", data)])
curl.setopt(pycurl.WRITEFUNCTION, lambda _: None)
curl.perform()
