##############################################################################
#
# Copyright (c) 2010 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.
#
##############################################################################
import subprocess
from six.moves import http_client as httplib
import base64
import os
import shutil

from slapos.recipe.librecipe import GenericBaseRecipe

class Recipe(GenericBaseRecipe):

  def _options(self, options):
    options['password'] = self.generatePassword()

  def install(self):
    path_list = []

    htpasswd_file = self.options['htpasswd-file']
    # Create or empty the file
    open(htpasswd_file, 'w').close()
    path_list.append(htpasswd_file)

    user = self.options['user']
    password = self.options['password']
    subprocess.check_call([self.options['apache-htpasswd'],
                           '-bc', htpasswd_file,
                           user, password
                          ])

    htdocs_location = self.options['htdocs']
    if not (os.path.exists(htdocs_location) and os.listdir(htdocs_location)):
      try:
        os.rmdir(htdocs_location)
      except:
        pass
      shutil.copytree(self.options['source'], htdocs_location)

    # Install php.ini
    php_ini = self.createFile(os.path.join(self.options['php-ini-dir'],
                                             'php.ini'),
                                self.substituteTemplate(self.getTemplateFilename('php.ini.in'),
                                dict(tmp_directory=self.options['tmp-dir']))
                )
    path_list.append(php_ini)
    
    apache_config = dict(
      pid_file=self.options['pid-file'],
      lock_file=self.options['lock-file'],
      davlock_db=self.options['davdb-lock'],
      ip=self.options['ip'],
      port_webdav=self.options['port_webdav'],
      port_ajax=self.options['port_ajax'],
      error_log=self.options['error-log'],
      access_log=self.options['access-log'],
      document_root=self.options['htdocs'],
      modules_dir=self.options['apache-modules-dir'],
      mime_types=self.options['apache-mime-file'],
      server_root=self.options['root'],
      email_address=self.options['email-address'],
      htpasswd_file=htpasswd_file,
      ssl_certificate=self.options['cert-file'],
      ssl_key=self.options['key-file'],
      php_ini_dir=self.options['php-ini-dir']
    )

    # Create logfiles
    for log in [self.options['error-log'], self.options['access-log']]:
      open(log, 'a').close()

    config_file = self.createFile(self.options['conf-file'],
       self.substituteTemplate(self.getTemplateFilename('httpd.conf.in'),
                               apache_config)
    )
    path_list.append(config_file)

    wrapper = self.createWrapper(self.options['wrapper'],
      (self.options['apache-binary'], '-f', config_file, '-DFOREGROUND'))
    path_list.append(wrapper)

    promise = self.createPythonScript(self.options['promise'],
      __name__ + '.promise',
      (self.options['ip'], int(self.options['port_webdav']),
       self.options['user'], self.options['password']))
    path_list.append(promise)

    return path_list

def promise(host, port, user, password):
  connection = httplib.HTTPSConnection(host, port)
  auth = base64.b64encode('%s:%s' % (user, password))
  connection.request('OPTIONS', '/',
                     headers=dict(
                       Authorization='Basic %s' % auth,
                     )
                    )
  connection.getresponse()

  return 0
