#!/bin/sh
# Copyright Broadcom, Inc. All Rights Reserved.
# SPDX-License-Identifier: APACHE-2.0
set -eu

n=0
max=2

# Enable photon and photon-updates repos
if [ -f /etc/yum.repos.d/photon.repo ]; then
    sed -i '/^\[photon\]/,/^\[/{s/^enabled=0/enabled=1/}' /etc/yum.repos.d/photon.repo;
fi

# Toybox includes a set of binaries which are conflicting with other
# packages' binaries such as 'findutils' or 'sed'
if [ -n "$(tdnf list installed | grep -oE 'toybox')" ]; then
    tdnf remove -y toybox
    # Install sed as a key dependency for postin scripts
    tdnf install -y sed
fi

until [ $n -gt $max ]; do
    set +e
    tdnf install -y "$@"
    CODE=$?
    set -e
    if [ $CODE -eq 0 ]; then
        break
    fi
    if [ $n -eq $max ]; then
        exit $CODE
    fi
    echo "tdnf failed, retrying"
    n=$(($n + 1))
done
tdnf clean all && rm -rf /var/cache/tdnf
