##############################################################################
#
# Copyright (c) 2012 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
#
##############################################################################
from __future__ import print_function

from slapos.recipe.librecipe import GenericBaseRecipe
import zc.buildout
import sys
import zc.recipe.egg

class Recipe(GenericBaseRecipe):
  def install(self):
    """
    Taken out from the old "lamp" recipe. Allows do configure a LAMP instance.
    """
    self.path_list = []

    document_root = self.options['htdocs']
    url = self.options.get('url', '')
    mysql_conf = {'mysql_host': self.options['mysql-host'],
                  'mysql_port': self.options['mysql-port'],
                  'mysql_user': self.options['mysql-username'],
                  'mysql_password': self.options['mysql-password'],
                  'mysql_database': self.options['mysql-database'],
                 }
    self.configureInstallation(document_root, url, mysql_conf)

    return self.path_list

  def configureInstallation(self, document_root, url, mysql_conf):
    """Start process which can launch python scripts, move or remove files or
    directories when installing software.
    """
    if not self.options.has_key('delete') and not self.options.has_key('rename') and not\
        self.options.has_key('chmod') and not self.options.has_key('script') and not self.options.has_key('sql-script'):
      return ""
    delete = []
    chmod = []
    data = []
    rename = []
    rename_list = ""
    argument = [self.options['lampconfigure'], "-H", mysql_conf['mysql_host'], "-P", mysql_conf['mysql_port'],
                             "-p", mysql_conf['mysql_password'], "-u", mysql_conf['mysql_user']]
    if not self.options.has_key('file_token'):
      argument = argument + ["-d", mysql_conf['mysql_database'],
                             "--table", self.options['table_name'].strip(), "--cond",
                             self.options.get('constraint', '1').strip()]
    else:
      argument = argument + ["-f", self.options['file_token'].strip()]
    argument += ["-t", document_root]

    if self.options.has_key('delete'):
      delete = ["delete"]
      for fname in self.options['delete'].split(','):
        delete.append(fname.strip())
    if self.options.has_key('rename'):
      for fname in self.options['rename'].split(','):
        if fname.find("=>") < 0:
          old_name = fname
          fname = []
          fname.append(old_name)
          fname.append(old_name + '-' + mysql_conf['mysql_user'])
        else:
          fname = fname.split("=>")
        cmd = ["rename"]
        if self.options.has_key('rename_chmod'):
          cmd += ["--chmod", self.options['rename_chmod'].strip()]
        rename.append(cmd + [fname[0].strip(), fname[1].strip()])
        rename_list += fname[0] + " to " + fname[1] + " "
    if self.options.has_key('chmod'):
      chmod = ["chmod", self.options['mode'].strip()]
      for fname in self.options['chmod'].split(','):
        chmod.append(fname.strip())
    if self.options.has_key('script') and \
        self.options['script'].strip().endswith(".py"):
      data = ["run", self.options['script'].strip(), "-v", mysql_conf['mysql_database'], url, document_root]
    if self.options.has_key('sql-script'):
      data = ["sql", self.options['sql-script'].strip(), "-v", mysql_conf['mysql_database'], url, document_root]


    # TODO factor
    if delete != []:
      print("Creating lampconfigure with 'delete' arguments")
      command = argument + delete
    if rename != []:
      for parameters in rename:
        print("Creating lampconfigure with 'rename' arguments")
        command = argument + rename
    if chmod != []:
      print("Creating lampconfigure with 'chmod' arguments")
      command = argument + chmod
    if data != []:
      print("Creating lampconfigure with 'run' arguments")
      command = argument + data


    configureinstall_wrapper_path = self.createPythonScript(
        self.options['configureinstall-location'],
        __name__ + '.runner.executeRunner',
        (argument, delete, rename, chmod, data)
    )
    
    #TODO finish to port this and remove upper one
    #configureinstall_wrapper_path = self.createPythonScript(
    #    self.options['configureinstall-location'],
    #    'slapos.lamp.run',
    #    [command]
    #)
    self.path_list.append(configureinstall_wrapper_path)

    return rename_list
