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

IFS=$'\n'
abinfo "Collecting binary names ..."
bins=($(dpkg -L gcc-${GCC_MAJOR} | grep /usr/bin/))
abinfo "Collecitng static libraries ..."
STATIC_LIBS=($(find "$GCC_DIR" -maxdepth 1 -mindepth 1 -type f -name 'lib*.a' -printf "%P\n"))
unset IFS

abinfo "Installing symlinks to binaries ..."
install -d "$PKGDIR"/usr/bin
for bin in "${bins[@]}" ; do
	basename="$(basename $bin)"
	binname="${basename%%-$GCC_MAJOR}"
	if [ "$basename" = "$binname" ] ; then # non-versioned
		continue
	fi
	ln -svf "$basename" "$PKGDIR"/usr/bin/"$binname"
done

abinfo "Installing symlinks to static libraries ..."
mkdir -pv "$PKGDIR"/usr/lib
for static_lib in "${STATIC_LIBS[@]}" ; do
	ln -svf "${GCC_DIR##/usr/lib/}"/"$static_lib" "$PKGDIR"/usr/lib/"$static_lib"
done

# libgcc_s.so is a linker script which instructs the linker to:
# 1) link to libgcc_s.so.1, and at the same time
# 2) search for other symbols in libgcc.a.
abinfo "Installing symlink to libgcc_s.so ..."
ln -svf "${GCC_DIR##/usr/lib/}"/libgcc_s.so "$PKGDIR"/usr/lib/libgcc_s.so

if [ -e "${GCC_DIR}"/include/backtrace-supported.h ] && \
	[ -e "${GCC_DIR}"/include/backtrace.h ] ; then
	abinfo "Linking libbacktrace headers ..."
	mkdir -pv "$PKGDIR"/usr/include
	ln -svf ../"${GCC_DIR##/usr/}"/include/backtrace-supported.h \
		"$PKGDIR"/usr/include/backtrace-supported.h
	ln -svf ../"${GCC_DIR##/usr/}"/include/backtrace.h \
		"$PKGDIR"/usr/include/backtrace.h
fi

if [ -e "${GCC_DIR}"/include/c++ ] ; then
	abinfo "Linking C++ headers ..."
	mkdir -pv "$PKGDIR"/usr/include/c++
	ln -svf ../../"${GCC_DIR##/usr/}"/include/c++ \
		"$PKGDIR"/usr/include/c++/"$GCC_MAJOR"
fi

if [ -e "${GCC_DIR}"/include/libiberty ] ; then
	abinfo "Linking libiberty headers ..."
	ln -svf ../"${GCC_DIR##/usr/}"/include/libiberty \
		"$PKGDIR"/usr/include/libiberty
fi

abinfo "Symlinking LTO Plugin ..."
mkdir -pv "$PKGDIR"/usr/lib/bfd-plugins
ln -svf "../${GCC_DIR##/usr/lib/}/liblto_plugin.so" \
	"$PKGDIR"/usr/lib/bfd-plugins/liblto_plugin.so

abinfo "Installing symlinks to info pages ..."
mkdir -pv "$PKGDIR"/usr/share/info
for info in \
	cpp-"${GCC_MAJOR}".info.xz \
	cppinternals-"${GCC_MAJOR}".info.xz \
	gcc-"${GCC_MAJOR}".info.xz \
	gccgo-"${GCC_MAJOR}".info.xz \
	gccinstall-"${GCC_MAJOR}".info.xz \
	gccint-"${GCC_MAJOR}".info.xz \
	gdc-"${GCC_MAJOR}".info.xz \
	gfortran-"${GCC_MAJOR}".info.xz \
	gnat-style-"${GCC_MAJOR}".info.xz \
	gnat_rm-"${GCC_MAJOR}".info.xz \
	gnat_ugn-"${GCC_MAJOR}".info.xz \
	m2-"${GCC_MAJOR}".info.xz
do
	linkname="${info/-$GCC_MAJOR/}"
	ln -sv "$info" "$PKGDIR"/usr/share/info/"$linkname"
done

abinfo "Installing symlinks to manual pages ..."
mkdir -pv "$PKGDIR"/usr/share/man/man1
mkdir -pv "$PKGDIR"/usr/share/man/man7

for man in \
	man1/cpp-"${GCC_MAJOR}".1.xz \
	man1/g++-"${GCC_MAJOR}".1.xz \
	man1/gcc-"${GCC_MAJOR}".1.xz \
	man1/gccgo-"${GCC_MAJOR}".1.xz \
	man1/gcov-"${GCC_MAJOR}".1.xz \
	man1/gcov-dump-"${GCC_MAJOR}".1.xz \
	man1/gcov-tool-"${GCC_MAJOR}".1.xz \
	man1/gdc-"${GCC_MAJOR}".1.xz \
	man1/gfortran-"${GCC_MAJOR}".1.xz \
	man1/gm2-"${GCC_MAJOR}".1.xz \
	man1/go-"${GCC_MAJOR}".1.xz \
	man1/gofmt-"${GCC_MAJOR}".1.xz \
	man1/lto-dump-"${GCC_MAJOR}".1.xz \
	man7/fsf-funding-"${GCC_MAJOR}".7.xz \
	man7/gfdl-"${GCC_MAJOR}".7.xz \
	man7/gpl-"${GCC_MAJOR}".7.xz
do
	linkname="${man/-${GCC_MAJOR}/}"
	ln -sv "${man##*/}" "$PKGDIR"/usr/share/man/"$linkname"
done
