<% def title = 'Edit configuration';
def saved = request.getParameter("config");
def error = null;
if (saved) {
    try {
        def initFile = new File("etc/config.tdsl");
        initFile.renameTo(new File("etc/config.tdsl.bak"));
        initFile.setText(saved);
        title = "Configuration updated";
    } catch (Throwable ex) {
        error = ex;
        title = "Error";
    }
} %>
${ util.include('header', [title:'Setup: ' + title]) }
<% if ((!saved) || (error != null)) { %>
    <form method='POST' style="margin: 1em 2em; " >
        <% if (error) { %>
        <div class="panel panel-danger">
            <div class="panel-heading panel-xs">
                <h4>Update of configuration file failed</h4>
            </div>
            <div class="panel-body">
                <h4>${ex.getMessage()}</h4>
            </div>
        </div>
        <% } %>

        <div class="panel panel-default">
            <div class="panel-heading panel-xs">
                <% if (error) { %>
                <h4>Unsaved configuration:</h4>
                <% } else { %>
                <h4>Current Tigase XMPP Server configuration:</h4>
                <% } %>
            </div>
            <div class="panel-body">
<textarea class="form-control" name='config' rows=40 cols=140 style="width: 100%">
<%= saved ?: new File("etc/config.tdsl").getText() %>
</textarea>
                <h4>Do you wish to save this configuration to <i>etc/config.tdsl</i> file?</h4>
            </div>
        </div>
        <button class="btn btn-default" type="submit" name="save" style="margin-top: 1em; margin-left: 100px;">Save</button>
    </form>

<% } else { %>
<div class="panel panel-success">
    <div class="panel-heading panel-xs">
        <h4>Configuration file was successfully updated with new configuration.</h4>
    </div>
    <div class="panel-body">
        <h4>File with old configuration was backed up as <i>config.tdsl.bak</i>.<br/><br/>Please restart server to apply new configuration.</h4>
    </div>
</div>
<% } %>
${ util.include('footer') }