GCC_MAJOR="${__MAJOR}"
GCC_DIR="$(gcc-${GCC_MAJOR} --print-file-name=.)"
GCC_DIR="$(realpath "$GCC_DIR")"
abinfo "GCC directory: $GCC_DIR"

abinfo "Collecting library names ..."
IFS=$'\n'
LIBS=($(find "$GCC_DIR" -maxdepth 1 -mindepth 1 -type f -name 'lib*.so.*' -executable -printf "%P\n"))
unset IFS

abinfo "Installing symlinks ..."
install -dv "$PKGDIR"/usr/lib
for lib in "${LIBS[@]}" ; do
	ln -svf "${GCC_DIR##/usr/lib/}"/"$lib" "$PKGDIR"/usr/lib/"$lib"
	ln -svf "${GCC_DIR##/usr/lib/}"/"$lib" "$PKGDIR"/usr/lib/"${lib%%.so*}.so"
	sover="${lib##*.so.}"
	somajor="${sover%%.*}"
	ln -svf "${GCC_DIR##/usr/lib/}"/"$lib" "$PKGDIR"/usr/lib/"${lib%%.so*}".so."$somajor"
done

abinfo "Removing unneeded symlinks ..."
for omit_libname in libcc1 ; do
	find "$PKGDIR"/usr/lib -type l -name "$omit_libname"'*' -exec rm -v {} \;
done

# WARNING: libgcc_s.so is a linker script which automatically appends
# -lgcc (libgcc.a) and the correct shared library name.
abinfo "Removing invalid libgcc_s.so symlink ..."
unlink "$PKGDIR"/usr/lib/libgcc_s.so
