# cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os

from cerbero.commands import Command, register_command
from cerbero.errors import FatalError
from cerbero.utils import _, N_, ArgparseArgument, shell


SCRIPT_TPL = '''\
#!/usr/bin/env bash

%s

%s
'''


class GenSdkShell(Command):
    doc = N_('Create a script with the shell environment for the SDK')
    name = 'gensdkshell'

    DEFAULT_CMD = '$SHELL "$@"'

    def __init__(self):
        Command.__init__(self,
            [ArgparseArgument('name', nargs=1, default='sdk-shell',
                             help=_('name of the scrips')),
            ArgparseArgument('-o', '--output-dir', default='.',
                             help=_('output directory')),
            ArgparseArgument('-p', '--prefix',
                             help=_('prefix of the SDK')),
            ArgparseArgument('--cmd', default=self.DEFAULT_CMD,
                             help=_('command to run in the script')),
            ])

    def run(self, config, args):
        name = args.name[0]
        prefix = args.prefix and args.prefix or config.prefix
        libdir = os.path.join(prefix, 'lib')
        py_prefix = config.py_prefix
        output_dir = args.output_dir
        cmd = args.cmd
        self.runargs(config, name, output_dir, prefix, libdir, py_prefix, cmd)

    def runargs(self, config, name, output_dir, prefix, libdir,
                py_prefix, cmd=None):
        cmd = cmd or self.DEFAULT_CMD
        env = config.get_env(prefix, libdir, py_prefix)
        env['PATH'] = '%s/bin${PATH:+:$PATH}' % prefix
        env['LDFLAGS'] = '-L%s' % libdir
        envstr = ''
        for e, v in env.iteritems():
            v = v.replace(config.prefix, prefix)
            envstr += 'export %s="%s"\n' % (e, v)
        try:
            filepath = os.path.join(output_dir, name)
            with open(filepath, 'w+') as f:
                f.write(SCRIPT_TPL % (envstr, cmd))
            shell.call("chmod +x %s" % filepath)
        except IOError, ex:
            raise FatalError(_("Error creating script: %s", ex))


register_command(GenSdkShell)

@@ -1056,16 +1056,19 @@
 env bash
+ -i
 %0A%0A%25s%0A%0A%25s
@@ -2296,49 +2296,757 @@
 v = 
-config.get_env(prefix, libdir, py_
+%7B%7D%0A        env%5B'PATH'%5D = '%25s/bin$%7BPATH:+:$PATH%7D:/usr/local/bin:/usr/bin:/bin' %25 prefix%0A        env%5B'LD_LIBRARY_PATH'%5D = '%25s/lib$%7BLD_LIBRARY_PATH:+:$LD_LIBRARY_PATH%7D' %25 libdir%0A        env%5B'PKG_CONFIG_PATH'%5D = '%25s/lib/pkgconfig:%25s/share/pkgconfig$%7BPKG_CONFIG_PATH:+:$PKG_CONFIG_PATH%7D' %25 (prefix, prefix)%0A        env%5B'XDG_DATA_DIRS'%5D = '%25s/share$%7BXDG_DATA_DIRS:+:$XDG_DATA_DIRS%7D:/usr/local/share:/usr/share' %25 prefix%0A        env%5B'XDG_CONFIG_DIRS'%5D = '%25s/etc/xdg$%7BXDG_CONFIG_DIRS:+:$XDG_CONFIG_DIRS%7D:/etc/xdg' %25 prefix%0A        env%5B'GST_PLUGIN_SYSTEM_PATH'%5D = '%25s/gstreamer-0.10' %25 libdir%0A        env%5B'GST_REGISTRY'%5D = '$%7BHOME%7D/.gstreamer-0.10/.cerbero-registry'%0A        env%5B'GST_PLUGIN_SCANNER'%5D = '%25s/libexec/gstreamer-0.10/gst-plugin-scanner' %25 
 prefix
-)
 %0A   
@@ -3047,32 +3047,38 @@
 ix%0A        env%5B'
+PYTHON
 PATH'%5D = '%25s/bin
@@ -3070,37 +3070,56 @@
 ATH'%5D = '%25s/
-bin$%7B
+%25s/site-packages$%7BPYTHON
 PATH:+:$
 PATH%7D' %25 pre
@@ -3106,24 +3106,30 @@
 PATH:+:$
+PYTHON
 PATH%7D' %25
  prefix%0A
@@ -3120,16 +3120,149 @@
 PATH%7D' %25
+ (prefix, py_prefix)%0A        env%5B'CFLAGS'%5D = '-I%25s/include $%7BCFLAGS%7D' %25 prefix%0A        env%5B'CXXFLAGS'%5D = '-I%25s/include $%7BCXXFLAGS%7D' %25
  prefix%0A
@@ -3291,16 +3291,27 @@
  = '-L%25s
+ $%7BLDFLAGS%7D
 ' %25 libd
@@ -3374,57 +3374,8 @@
 ():%0A
-            v = v.replace(config.prefix, prefix)%0A
     

Fix generated gst-sdk-shell scripts