GCC_LIBDIR="$LIBDIR"/gcc/"${ARCH_TARGET[$ABHOST]}"/"${PKGVER%%.*}"
GCC_INCDIR="$GCC_LIBDIR"/include
MAJOR="${PKGVER%%.*}"

# FIXME: temporary copy of GCC is installed as TRIPLE-gcc-tmp.
if [ -e "$PKGDIR"/usr/bin/"${ARCH_TARGET[$ABHOST]}"-gcc-tmp ] ; then
	abinfo "Removing gcc-tmp ..."
	rm -v "$PKGDIR"/usr/bin/"${ARCH_TARGET[$ABHOST]}"-gcc-tmp
fi

abinfo "Creating symlink - FHS expects 'cpp' to be in /usr/lib..."
ln -sv ../bin/cpp-"$MAJOR" \
    "$PKGDIR"/usr/lib/cpp-"$MAJOR"

abinfo "Moving a misplaced file that confuses ldconfig(8) ..."
mkdir -pv "$PKGDIR"/usr/share/gdb/auto-load/usr/lib
mv -v "$PKGDIR"/"$GCC_LIBDIR"/*gdb.py \
    "$PKGDIR"/usr/share/gdb/auto-load/usr/lib/

abinfo "Moving rogue libraries to the correct location ..."
for d in $(find "$PKGDIR"/"$GCC_LIBDIR"/.. -maxdepth 1 -type d -name 'lib*') ; do
    for f in $(find "$d" -maxdepth 1 -type f,l -name 'lib*') ; do
        mv -v "$f" "$PKGDIR"/"$GCC_LIBDIR"/
    done
    rm -rv "$d"
done

abinfo "Moving rogue headers to the correct location ..."
mv -v "$PKGDIR"/usr/include/*.h \
    "$PKGDIR"/"$GCC_INCDIR"/
rm -rv "$PKGDIR"/usr/include

# Debian: libbacktrace is not installed by default
if [ -e "$BLDDIR"/libbacktrace/backtrace-supported.h ] ; then
    abinfo "Installing libbacktrace..."
    install -Dvm644 "$BLDDIR"/libbacktrace/backtrace-supported.h \
        "$PKGDIR"/"$GCC_INCDIR"/backtrace-supported.h
    install -Dvm644 "$SRCDIR"/libbacktrace/backtrace.h \
        "$PKGDIR"/"$GCC_INCDIR"/backtrace.h
    install -Dvm644 "$BLDDIR"/libbacktrace/.libs/libbacktrace.a \
        "$PKGDIR"/"$GCC_LIBDIR"/libbacktrace.a
fi

abinfo "Installing position-independent libiberty.a ..."
install -Dvm644 "$BLDDIR"/libiberty/pic/libiberty.a \
    "$PKGDIR"/"$GCC_LIBDIR"/libiberty.a

abinfo "Installing runtime files to GCC version-specific libdir ..."
find "$PKGDIR"/"$LIBDIR" -maxdepth 1 -name '*.so' \
    -exec cp -av {} "$PKGDIR"/"$GCC_LIBDIR"/ \;
find "$PKGDIR"/"$LIBDIR" -maxdepth 1 -name '*.so.*' \
    -exec cp -av {} "$PKGDIR"/"$GCC_LIBDIR"/ \;
find "$PKGDIR"/"$LIBDIR" -maxdepth 1 -name '*.a' \
    -exec cp -av {} "$PKGDIR"/"$GCC_LIBDIR"/ \;

abinfo "Removing libraries from libdir ..."
find "$PKGDIR"/"$LIBDIR" -maxdepth 1 -type f -exec rm -v {} \;
find "$PKGDIR"/"$LIBDIR" -maxdepth 1 -type l -exec unlink {} \;

abinfo "Removing files in gcc-runtime..."
RTLIBS="asan lsan ubsan tsan vtv itm quadmath atomic gcc_s stdc++ \
        m2cor m2iso m2log m2min m2pim \
        gfortran gomp gomp-plugin-host_nonshm objc go cilkrts gdruntime \
        gphobos gccjit"

for i in ${RTLIBS[@]}; do
    if [ -e "$PKGDIR"/"$GCC_LIBDIR"/lib${i}.so ]; then
        abinfo "Removing lib${i} ..."
        rm -v "$PKGDIR"/"$GCC_LIBDIR"/lib${i}.so*
    fi
done

abinfo "Removing locale and texinfo documentation from gcc-runtime ..."
for file in \
	"$PKGDIR"/usr/share/locale/de/LC_MESSAGES/libstdc++.mo \
	"$PKGDIR"/usr/share/locale/fr/LC_MESSAGES/libstdc++.mo \
	"$PKGDIR"/usr/share/info/libgomp.info \
	"$PKGDIR"/usr/share/info/libquadmath.info \
	"$PKGDIR"/usr/share/info/libitm.info \
	"$PKGDIR"/usr/share/info/libgccjit.info ; do
	if [ -e "$file" ] ; then
		rm -v "$file"
	fi
done

# Note: We don't ship gccgo with Afterglow.
#
# FIXME: No gccgo support for MIPS yet.
if ! ab_match_archgroup retro && \
   ! ab_match_arch loongson3 && \
   ! ab_match_arch loongarch64 && \
   ! ab_match_arch loongarch64_nosimd; then
    abinfo "Preparing go and gofmt for alternatives ..."
    mv -v "$PKGDIR"/usr/bin/go{-${PKGVER%%.*},-gnu-${PKGVER%%.*}}
    mv -v "$PKGDIR"/usr/bin/gofmt{-${PKGVER%%.*},-gnu-${PKGVER%%.*}}
else
    abinfo "Generating wrapper script for platforms that do not unsupport gccgo ..."
    echo "#! /bin/bash" > "$PKGDIR"/usr/bin/go-gnu-${PKGVER%%.*}
    echo "echo GNU Go is not available on this architecture. ; exit 1" >> "$PKGDIR"/usr/bin/go-gnu-${PKGVER%%.*}
    cp -v "$PKGDIR"/usr/bin/go{,fmt}-gnu-${PKGVER%%.*}
    chmod -v 755 "$PKGDIR"/usr/bin/go{,fmt}-gnu-${PKGVER%%.*}
fi

case "$ARCH" in
	amd64|i486)
		common_vendor="pc"
		;;
	*)
		common_vendor="unknown"
		;;
esac
abinfo "Creating compatibility symlinks to ${HOST/aosc/unknown} ..."
ln -sv ${HOST} \
    "$PKGDIR"/usr/lib/gcc/${HOST/aosc/$common_vendor}
ln -sv ${HOST} \
    "$PKGDIR"/"$GCC_INCDIR"/c++/${HOST/aosc/$common_vendor}
for i in "$PKGDIR"/usr/bin/${HOST}*; do
    EXEC_NAME=$(basename $i)
    ln -sv $EXEC_NAME \
        "$PKGDIR"/usr/bin/${EXEC_NAME/aosc/$common_vendor}
done

abinfo "Creating compatibility symlinks to vendor-less (Debian-like) triples ..."
ln -sv ${HOST} \
    "$PKGDIR"/usr/lib/gcc/${HOST/-aosc/}
ln -sv ${HOST} \
    "$PKGDIR"/"$GCC_INCDIR"/c++/${HOST/-aosc/}
for i in "$PKGDIR"/usr/bin/${HOST}*; do
    EXEC_NAME=$(basename $i)
    ln -sv $EXEC_NAME \
        "$PKGDIR"/usr/bin/${EXEC_NAME/-aosc/}
done

abinfo "Moving documentation and locale data to versioned paths ..."
# NOTE Section 1 pages already have versioned suffixes.
# NOTE Gettext translation catalogs already have versioned suffixes.
for i in "$PKGDIR"/usr/share/man/man7/*; do
    mv -v "$i" "${i%%.7}-$MAJOR.7"
done
for i in "$PKGDIR"/usr/share/info/!(dir); do
    mv -v "$i" "${i%%.info}-$MAJOR.info"
done
