# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import os.path
import logging
    
_log = logging.getLogger('varnish')

DEFAULTS = {
    'VARNISH_HOST': 'raw.githubusercontent.com',
    'VARNISH_VERSION': '3.0.6',
    'VARNISH_PACKAGE': 'varnish-{VARNISH_VERSION}.tar.gz',
    'VARNISH_DOWNLOAD_URL': 'https://gitlab.liip.ch/chregu/cf-varnish-binary/raw/master/vendor/varnish-{VARNISH_VERSION}.tar.gz',
}


class VarnishInstaller(object):

    def __init__(self, ctx):
        self._log = _log
        self._ctx = ctx
        self._merge_defaults()


    def _merge_defaults(self):
        for key, val in DEFAULTS.iteritems():
            if key not in self._ctx:
                self._ctx[key] = val
                
    def should_install(self):
        return self._ctx['CACHE_SERVER'] == 'varnish'
    
    def install(self):
        _log.info("Installing Varnish METHOD")
        self._builder.install()._installer._install_binary_from_manifest(
                self._ctx['VARNISH_DOWNLOAD_URL'],
                os.path.join('app'),
                extract=True)

        

def preprocess_commands(ctx):
    return ((
        '$HOME/.bp/bin/rewrite',
        '"$HOME/varnish/etc/varnish"'),)


def service_commands(ctx):
    return {
            'varnish': (
                '$HOME/varnish/sbin/varnishd',
                '-n $TMPDIR/varnish/',
                '-F',
                '-f $HOME/varnish/etc/varnish/default.vcl',
                '-a 0.0.0.0:$VCAP_APP_PORT',
                '-t 120',
                '-w 50,1000,120',
                '-s malloc,128M',
                '-T 127.0.0.1:6082',
                '-p http_resp_hdr_len=32768'
                '2>&1'
                )
        }

def service_environment(ctx):
    env = {
            'LD_LIBRARY_PATH': '$LD_LIBRARY_PATH:$HOME/varnish/lib/varnish',
    }
    return env


def compile(install):
    varnish = VarnishInstaller(install.builder._ctx)
    if varnish.should_install():
        _log.info("Installing Varnish")
        (install
            .package('VARNISH')
            .config()
                .from_application('.bp-config/varnish')  # noqa
                .or_from_build_pack('defaults/config/varnish/{VARNISH_VERSION}')
                .to('varnish/etc/varnish')
                .rewrite()
                .done())
        _log.info("Varnish Installed.")
    return 0

@@ -1957,32 +1957,37 @@
 _commands(ctx):%0A
+    %0A
     return %7B%0A   
@@ -1983,16 +1983,16 @@
 eturn %7B%0A
-
         
@@ -2312,12 +2312,29 @@
 loc,
-128M
+$VARNISH_MEMORY_LIMIT
 ',%0A 
@@ -2492,24 +2492,252 @@
 nment(ctx):%0A
+    if 'VARNISH_MEMORY_LIMIT' in ctx:%0A        varnish_memory_limit = ctx%5B'VARNISH_MEMORY_LIMIT'%5D%0A    else:%0A        varnish_memory_limit = ctx%5B'MEMORY_LIMIT'%5D;%0A%0A    _log.info('Varnish memory limit is %5B%25s%5D', varnish_memory_limit)%0A
     env = %7B%0A
@@ -2813,16 +2813,74 @@
 rnish',%0A
+            'VARNISH_MEMORY_LIMIT': varnish_memory_limit,%0A
     %7D%0A  
@@ -3393,16 +3393,16 @@
 lled.%22)%0A
-
     retu
@@ -3406,8 +3406,13 @@
 eturn 0%0A
+    %0A

implement the possibility to add a VARNISH_MEMORY_LIMIT