#! /usr/bin/env python

#
# This script packs the given sensor into an installer script. The user
# only has to start the installer script in order to install the sensor into
# her home directory.
#
# Usage: pack_sensor.bin <path>
#   where path is the package directory of the sensor to pack.
#
# (c) 2003 Martin Grimme
#

# This script was modified to pack gnumeric python plugins by Yaacov Zamir 2004

import sys, os, commands, base64

NAME = "Pack Sensor"
VERSION = "0.33"

# the installer
INSTALLER = """#! /usr/bin/env python

#
# Autogenerated installer for gnumeric python pluins.
#
# Usage: Install_Xyz_Sensor.bin [--nomsg] [--list] [<destination>]
#
# The default destination is user specific sensor path. <destination> overrides
# the destination path.
# The option --nomsg can be used to suppress message dialogs.
# The option --list only shows the contents of the archive without installing.
#

import sys, os, commands, base64, tempfile


ERROR_B64 = "Could not extract the archive.\\n" \\
            "The installer seems to be corrupted."
ERROR_TAR = "Could not extract the archive.\\n" \\
            "tar reported an error:\\n"
SUCCESS = "The plugin has been installed successfully.\\n" \\
          "gnumeric is now able to use it."

# guess gnumeric version
fail, version = commands.getstatusoutput(
    "LANG=C gnumeric --version | grep version | cut -f 2 -d \\\\\\\'")
if (fail):
    print "Could not get gnumeric version number", version
    sys.exit(1)

# default settings
no_message = 0
list_only = 0
destination = "~/.gnumeric/" + version + "/plugins"

# parse arguments
args = sys.argv[1:]
for p in args:
    if (p == "--nomsg"): no_message = 1
    elif (p == "--list"): list_only = 1
    else: destination = p
#end for

try:
    import pygtk; pygtk.require("2.0")
    import gtk
except:
    no_message = 1

def message(msg, mtype):
    d = gtk.MessageDialog(None, 0,
                          (mtype == "error") and gtk.MESSAGE_ERROR or
                          gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
                          unicode(msg, "iso-8859-1"))
    d.run()
    d.destroy()

def install(data, destination, no_message, list_only):
    try:
        archive = base64.decodestring(data)
    except:
        if (no_message): print ERROR_B64
        else: message(ERROR_B64, "error")
        sys.exit(1)
    tmp = tempfile.mktemp()
    fd = open(tmp, "w")
    fd.write(archive)
    fd.close()
	
    if (list_only):
        cmd = "mkdir -p %%(destination)s; cd %%(destination)s; " \\
              "LANG=C tar -tzf %%(tmp)s 2>&1"
    else:
        cmd = "mkdir -p %%(destination)s; cd %%(destination)s; " \\
              "LANG=C tar -xzf %%(tmp)s 2>&1"
    fail, out = commands.getstatusoutput(cmd %% vars())
    #os.unlink(tmp)
       
    if (fail):
        if (no_message): print ERROR_TAR + out
        else: message(ERROR_TAR + out, "error")
        sys.exit(1)
    
    if (not list_only):
        if (no_message): print SUCCESS
        else: message(SUCCESS, "info")

# the tarball in base64 encoding
BASE64DATA = \"\"\"%(data)s\"\"\"

install(BASE64DATA, destination, no_message, list_only)
"""

if (len(sys.argv) != 2):
    print "Usage: pack_plugin <directory>"
    sys.exit(1)
else:
    print NAME + " " + VERSION + ", (c) 2003 Martin Grimme\n(modified for gnumeric python plugins by Yaacov Zamir)"
    print ""

package = sys.argv[1]
if (package[-1] == "/"): package = package[:-1]
name = os.path.basename(package)
dir = os.path.dirname(package) or "."
oldcwd = os.getcwd()
os.chdir(dir)

print "packing " + name + "..."

# create the tarball
fail, data = commands.getstatusoutput(
    "tar -c --exclude=CVS --exclude=Makefile --exclude=*.pyc "
    "--exclude .cvsignore --exclude '*~' --exclude .xvpics "
    "--exclude mkinstalldirs --exclude ChangeLog  "
    "--exclude po -f - '%(name)s' | gzip" % vars())
if (fail):
    print "Could not create installer:", data
    sys.exit(1)
	
os.chdir(oldcwd)

# encode tarball
data = base64.encodestring(data)

# write the installer
filename = "Install_" + name + "_Plugin.bin"
fd = open(filename, "w")
fd.write(INSTALLER % vars())
fd.close()
os.system("chmod 0755 %(filename)s" % vars())
print "created " + filename
