#!{{ ansible_python['executable'] }}
# -*- coding: utf-8 -*-

# Copyright (C) 2016-2018 Reto Gantenbein <reto.gantenbein@linuxmonk.ch>
# Copyright (C) 2016-2018 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only

# {{ ansible_managed }}

from __future__ import print_function
from json import loads, dumps
import os

roundcube_deploy_path = loads('''{{ roundcube__git_dest
                                    | to_nice_json }}''')

roundcube_version_file = (roundcube_deploy_path +
                          '/program/include/iniset.php')

output = {'installed': False}

try:
    with open(roundcube_version_file, 'r') as f:
        for line in f:
            if "define('RCMAIL_VERSION'," in line.strip().split():
                line = line.strip().split()[1]
                line = line.lstrip("'").rstrip("');")

                output.update(
                        {'installed': True,
                         'version': line})

except Exception:
    pass

print(dumps(output, sort_keys=True, indent=4))
