#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- python -*-
# Copyright (C) 2006 by Sebastien Estienne
#
# This file may be distributed and/or modified under the terms of
# the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
# See "COPYING" in the source distribution for more information.
#
# $Id: exec_wrapper.in 141 2007-09-22 14:50:25Z sebest $
#

import sys
import os
import pygtk
import gtk


from subprocess import *
from string import join, replace

pygtk.require('2.0')

def error_msg(msg):
    d = gtk.MessageDialog(parent=None, flags=gtk.DIALOG_MODAL,
                          type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
    d.set_markup(msg)
    d.show_all()
    d.run()
    d.destroy()

def remove_markup(msg):
    msg = replace(msg, "&" , "&amp;")
    msg = replace(msg, "<" , "&lt;")
    msg = replace(msg, ">" , "&gt;")
    return msg

def main(argv):
    if len(argv) < 2:
        print "Not enought arguments."
        sys.exit(1)

    argv.pop(0)

    try:
        p = Popen(argv, close_fds=True, stderr = PIPE)
    except OSError, e:
        error_msg("<b>An error has occured!</b>\n\nCommand: %s\nError: %s" % (join(argv, " "), e))
    else:
        status = os.waitpid(p.pid, 0)
        if status[1] != 0:
            error_msg(remove_markup(p.stderr.read()))

if __name__ == "__main__":
    main(sys.argv)
