#!/bin/bash
## kola:
##   # This is a read-only test that can be run with other tests.
##   exclusive: false
##   # This test should pass everywhere if it passes anywhere
##   platforms: qemu-unpriv
#
# Check if there are broken symlinks in /etc/ and /usr/.
# See: https://github.com/coreos/fedora-coreos-config/issues/1782

set -xeuo pipefail

. $KOLA_EXT_DATA/commonlib.sh

# List of known broken symlinks that require further investigation:
list_broken_symlinks_skip=(
    '/etc/mtab'
    '/etc/ssl/'
    '/etc/swid/swidtags.d/fedoraproject.org'
    '/etc/xdg/systemd/user'
    '/usr/lib/.build-id/'
    '/usr/lib/firmware/qcom/LENOVO/21BX.xz'
    '/usr/lib/modules/'
    '/usr/share/rhel/secrets/etc-pki-entitlement'
    '/usr/share/rhel/secrets/redhat.repo'
    '/usr/share/rhel/secrets/rhsm'
)

# If RHCOS, update the array of ignored symlinks
if is_scos || is_rhcos; then
    rhcos_list=(
        '/etc/grub2-efi.cfg'
        '/etc/rhsm-host'
        '/etc/pki/entitlement-host'
        '/etc/sysconfig/grub'
    )
    list_broken_symlinks_skip+=("${rhcos_list[@]}")
fi

find /usr/ /etc/ -type l -not -path "/usr/etc*" | while read -r file_name; do
    real_path=$(realpath -m "${file_name}")
    if [[ -e "${real_path}" ]]; then
        continue
    fi
    found="false"
    for search_element in "${list_broken_symlinks_skip[@]}"; do
        if [[ "${file_name}" == "${search_element}"* || "${file_name}" == "${search_element}" ]]; then
            found="true"
            break
        fi
    done
    if [[ "${found}" == "false" ]]; then
        fatal "Error: ${file_name} symlink to ${real_path} which does not exist"
    fi
done
