#!/usr/bin/env bash
# Copyright 2020 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed 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.

set -ev

{% set verb_matrix = {
    'label': 'label',
    'cmd': 'cmd',
    'entrypoint': 'entrypoint',
    'env': 'env',
    'expose': 'port',
    'healthcheck': 'healthcheck',
    'onbuild': 'onbuild',
    'shell': 'shell',
    'stopsignal': 'stop-signal',
    'user': 'user',
    'volume': 'volume',
    'workdir': 'workingdir'
   }
%}
{% for key, value in tcib_args.items() %}
export {{ key }}={{ value | to_json }}
{% endfor %}
CONTAINER=$(buildah from {{ tcib_from }})
MOUNTPOINT=$(buildah mount ${CONTAINER})
{% for key, value in tcib_labels.items() %}
buildah config --label {{ key }}={{ value | to_json }} ${CONTAINER}
{% endfor %}
{% for key, value in tcib_envs.items() %}
buildah config --env {{ key }}={{ value | to_json }} ${CONTAINER}
{% endfor %}
{% if tcib_workdir | length > 0 %}
buildah config --workingdir {{ tcib_workdir | to_json }} ${CONTAINER}
{% endif %}
{% for item in tcib_onbuilds %}
buildah config --onbuild {{ item }} ${CONTAINER}
{% endfor %}
{% for item in tcib_volumes %}
buildah config --volume {{ item }} ${CONTAINER}
{% endfor %}
{% for item in tcib_exposes %}
buildah config --port {{ item }} ${CONTAINER}
{% endfor %}
{% if tcib_shell | length > 0 %}
buildah config --shell {{ tcib_shell | to_json }} ${CONTAINER}
{% endif %}
{% if tcib_healthcheck | length > 0 %}
buildah config --healthcheck {{ tcib_healthcheck | to_json }} ${CONTAINER}
{% endif %}
{% if tcib_stopsignal | length > 0 %}
buildah config --stop-signal {{ tcib_stopsignal }} ${CONTAINER}
{% endif %}
{% if tcib_entrypoint | length > 0 %}
buildah config --entrypoint {{ tcib_entrypoint | to_json }} ${CONTAINER}
{% endif %}
{% if tcib_cmd | length > 0 %}
buildah config --cmd {{ tcib_cmd | to_json }} ${CONTAINER}
{% endif %}
{% for item in tcib_adds %}
buildah add ${CONTAINER} {{ item }}
{% endfor %}
{% for item in tcib_copies %}
buildah copy ${CONTAINER} {{ item }}
{% endfor %}
{% for item in tcib_runs %}
{%   if item is iterable and item is not string %}
buildah run ${CONTAINER} {{ item | join(' ') }}
{%   else %}
buildah run ${CONTAINER} {{ item }}
{%   endif %}
{% endfor %}
{% for item in tcib_actions %}
{%   for key, value in item.items() %}
{%     if key.lower() in verb_matrix.keys() %}
buildah config --{{ verb_matrix[key.lower()] | to_json }} ${CONTAINER}
{%     else %}
{%       if value is iterable and value is not string %}
buildah {{ key.lower() }} ${CONTAINER} {{ value | join(' ') }}
{%       else %}
buildah {{ key.lower() }} ${CONTAINER} {{ value }}
{%       endif %}
{%     endif %}
{%   endfor %}
{% endfor %}
{% if tcib_user | length > 0 %}
buildah config --user {{ tcib_user }} ${CONTAINER}
{% endif %}
buildah commit ${CONTAINER} {{ tcib_path | basename }}
buildah unmount ${CONTAINER}
