# Since optenv32 resides in a non standard path, some hacky patches are required.
abinfo "Applying optenv32 hacks ..."
for file in \
 $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h)
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/opt/32&@g' \
      -e 's@/usr@/opt/32@g' $file.orig > $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/opt/32/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
  touch $file.orig
done
sed -i 's@\./fixinc\.sh@-c true@' "$SRCDIR"/gcc/Makefile.in

# Non standard CFLAGS causes libiberty fail to bootstrap:
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=13334
echo "Setting -O2 for libiberty ..."
sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" "$SRCDIR"/{libiberty,gcc}/configure

abinfo "Setting version string ..."
echo $PKGVER > "$SRCDIR"/gcc/BASE-VER
echo 'AOSC OS, optenv(32)' > "$SRCDIR"/gcc/DEV-PHASE

abinfo "Applying patches ..."
if [ -d "$SRCDIR"/autobuild/patches ]; then
    ab_apply_patches \
        "$SRCDIR"/autobuild/patches/*.{patch,diff} \
        "$SRCDIR"/autobuild/patches/*.{patch,diff}."${CROSS:-$ARCH}"
    ab_reverse_patches \
        "$SRCDIR"/autobuild/patches/*.r{patch,diff} \
        "$SRCDIR"/autobuild/patches/*.r{patch,diff}."${CROSS:-$ARCH}"
    touch "$SRCDIR"/.patch
fi
