#!/bin/bash
set -x

mkdir -p /opt
pushd /opt
tar xzf /tmp/gating_repo.tar.gz

cat > /etc/yum.repos.d/gating.repo << EOF
[gating-repo]
name=Gating repository
baseurl=file:///opt/gating_repo
enabled={{ ib_gating_repo_enabled | bool | ternary('1', '0') }}
gpgcheck=0
priority=1
EOF

{% if ib_create_web_repo|bool %}
ps -ef | grep -i python | grep SimpleHTTPServer | awk '{print $2}' | xargs --no-run-if-empty kill -9 || true
pushd /opt/gating_repo
iptables -I INPUT -p tcp --dport 8766 -j ACCEPT
nohup python -m SimpleHTTPServer 8766 1>{{ working_dir }}/pkg_mgr_mirror.log 2>{{ working_dir }}/pkg_mgr_mirror_error.log &
popd
cat > {{ working_dir }}//web-gating.repo << EOF
[gating-repo]
name=Gating repository
baseurl=http://localhost:8766/
enabled={{ ib_gating_repo_enabled | bool | ternary('1', '0') }}
gpgcheck=0
priority=1
EOF
{% endif %}

{% if ib_gating_repo_enabled|bool and ib_gating_repo_update|bool %}
{{ ansible_pkg_mgr }} --enablerepo="gating-repo" update -y
{% endif %}
