#!/bin/bash

# Task: go through the files in $RPM_BUILD_ROOT and
# relink symbolic links so that all are relative
#
# NOTE: we're not doing this to fix a problem (as no matter how you
#   do it you fix one problem by creating another). We're doing it
#   so we can document this as policy - so you can rely on it

# Additional Task: check some usual errors arround symlinks e.g.
#   dangling symlinks or symlinks to init scripts in wrong location

# Author: Stephan Kulow <coolo@suse.de>
#
# major speedup: Tomas Cech <sleep_walker@suse.cz> and Michal Kubecek <mkubecek@suse.cz>
# migration to C++: Stephan Kulow <coolo@suse.de>

# If using normal root, avoid changing anything.
if [ -z "$RPM_BUILD_ROOT" ]; then
	exit 0
fi

LC_ALL=
LANG=
LC_TIME=POSIX

cd $RPM_BUILD_ROOT

had_errors=0

while IFS="|" read link link_orig link_dest link_absolut
do
    if test "$link" = "$link_dest"; then
        echo "ERROR: $link points to itself (as $orig_link_dest)"
        had_errors=1
        continue
    fi

    case "$link_absolut" in
        /opt/kde3/share/doc*/HTML/*/common) # white listed for not existant
            ;;
        /usr/share/doc/kde/HTML/*/common) # white listed for not existant
            ;;
        /etc/alternatives/*) # white listed (ghosts)
            ;;
        ${RPM_BUILD_ROOT}*)
            echo "ERROR: Link $link -> $link_orig points inside build root!"
            had_errors=1
            continue
            ;;
        *share/automake-*)
            echo "ERROR: link target $link points into automake directory"
            echo " You might want to add a -c to the automake call (or just"
            echo " skip the files from packaging)"
            had_errors=1
            continue
            ;;
        *)
            if test ! -L ./"$link_absolut" && test ! -e "$link_absolut" && test ! -e ./"$link_absolut"; then
                echo "ERROR: link target doesn't exist (neither in build root nor in installed system):"
                echo "  $link -> $link_orig"
                echo "Add the package providing the target to BuildRequires and Requires"
                if [ "$NO_BRP_STALE_LINK_ERROR" != "yes" ]; then
                    had_errors=1
                    continue
                fi
            fi
            ;;
    esac

    if [ "$link_orig" != "$link_dest" ]; then
        echo "INFO: relinking $link -> $link_dest (was $link_orig)"
        # we need to remove the link first because it may point to a directory
        # and then semantic changes
        rm ./"$link" && ln -s -- "$link_dest" ./"$link"
    fi
done < <(find . -type l -printf '%P|%l\n' | sort | brp-symlink.prg)

if test "$had_errors" = 1; then
    exit 1
fi
