#!/bin/sh

set -e

# Try to make sure there aren't any tools missing. Don't try very hard.

which cc > /dev/null \
    || (echo "Couldn't find a C compiler. Install one and try again."; exit 1)

if [ `uname` = "Darwin" ]; then
    which help2man > /dev/null \
    || (echo "help2man is required for crosstool-ng. You can install it with Homebrew via:"; echo "brew install help2man"; exit 1)
    which gsed > /dev/null \
    || (echo "GNU sed is required for crosstool-ng. You can install it with Homebrew via:"; echo "brew install gnu-sed"; exit 1)
    which gobjcopy > /dev/null \
    || (echo "GNU binutils is required for crosstool-ng. You can install it with Homebrew via:"; echo "brew install binutils"; exit 1)
    which gawk > /dev/null \
    || (echo "GNU awk is required for crosstool-ng. You can install it with Homebrew via:"; echo "brew install gawk"; exit 1)
    which wget > /dev/null \
    || (echo "GNU wget is required for crosstool-ng. You can install it with Homebrew via:"; echo "brew install wget"; exit 1)
fi

for tool in git make c++ gperf help2man flex bison wget; do
    which "$tool" > /dev/null \
    || (echo "Couldn't find $tool. Install it and/or add it to your PATH and \
try again."; exit 1)
done

for gnutool in make awk sed; do
    if which "g$gnutool" > /dev/null; then
        true # we definitely have the GNU version
    elif "$gnutool" -v 2>&1 | grep -q GNU; then
        true # we even more definitely have the GNU version
    else
        echo "It doesn't look like you have the GNU version of $gnutool installed."
        echo "crosstool-ng is cranky. It requires the GNU versions of make, awk, and sed."
        exit 1
    fi
done

# Get the build and install directories.

if [ -z "$OCCROSS_INSTALL_DIR" ]; then
    OCCROSS_INSTALL_DIR="/opt/occross"
fi

if [ -z "$OCCROSS_BUILD_DIR" ]; then
    OCCROSS_BUILD_DIR="$HOME/occross_build"
    echo "\$OCCROSS_BUILD_DIR not set, using $OCCROSS_BUILD_DIR"
fi

mkdir -p "$OCCROSS_BUILD_DIR"
mkdir -p "$OCCROSS_INSTALL_DIR"

if [ $( ls -a "$OCCROSS_INSTALL_DIR" | wc -l ) -ne 2 ]; then
    echo "WARNING: $OCCROSS_INSTALL_DIR not empty! There might be problems!"
fi

cd "$OCCROSS_BUILD_DIR"

rm -f ?ase_sensitivity_test
touch case_sensitivity_test Case_sensitivity_test
if [ ! `ls ?ase_sensitivity_test | wc -l` -eq 2 ]; then
    echo "You are using a case-insensitive filesystem. crosstool-ng requires a case-"
    echo "sensitive one."
    if [ `uname` = "Darwin" ]; then
        echo "The easiest way to fix this is to create a case sensitive disk image using Disk"
        echo "Utility and set OCCROSS_BUILD_DIR to a path on that disk."
        echo "Make sure it's at least 8GB in size."
    else
        echo "If you are on Cygwin, you will need to change the registry value:"
        echo "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\obcaseinsensitive"
        echo "to 0 and reboot your machine."
    fi
    exit 1
fi

# Get and build crosstool-ng
export PATH="$OCCROSS_BUILD_DIR/bin:$PATH"
if [ -x "$OCCROSS_BUILD_DIR/bin/ct-ng" ]; then
    echo "crosstool-ng appears to already be working."
else
    if [ ! -d crosstool-ng ]; then
        git clone https://github.com/crosstool-ng/crosstool-ng.git
    fi
    cd crosstool-ng
    git checkout f7f70b67c44727d9eea48997c837e87c5e63ca33
    if [ `uname` = "Darwin" ]; then
        patch -p1 <<EOF
diff --git a/scripts/build/debug/300-gdb.sh b/scripts/build/debug/300-gdb.sh
index ee4753e..86c26e5 100644
--- a/scripts/build/debug/300-gdb.sh
+++ b/scripts/build/debug/300-gdb.sh
@@ -92,7 +92,7 @@ do_debug_gdb_build() {
             cross_extra_config+=("--disable-nls")
         fi
 
-        CC_for_gdb="\${CT_HOST}-gcc \${CT_CFLAGS_FOR_HOST} \${CT_LDFLAGS_FOR_HOST}"
+        CC_for_gdb="\${CT_HOST}-gcc \${CT_CFLAGS_FOR_HOST} \${CT_LDFLAGS_FOR_HOST} -Qunused-arguments"
         LD_for_gdb="\${CT_HOST}-ld \${CT_LDFLAGS_FOR_HOST}"
         if [ "\${CT_GDB_CROSS_STATIC}" = "y" ]; then
             CC_for_gdb+=" -static"
EOF
    fi
    ./bootstrap
    ./configure --prefix="$OCCROSS_BUILD_DIR"
    make
    make install
    cd ..
fi

# Build the cross tools themselves
rm -rf build-be
mkdir -p build-be
cd build-be
cat > .config <<EOF
#
# Automatically generated file; DO NOT EDIT.
# Crosstool-NG Configuration
#
CT_CONFIGURE_has_static_link=y
CT_CONFIGURE_has_make_3_81_or_newer=y
CT_CONFIGURE_has_xz=y
CT_CONFIGURE_has_cvs=y
CT_MODULES=y

#
# Paths and misc options
#

#
# crosstool-NG behavior
#
# CT_OBSOLETE is not set
# CT_EXPERIMENTAL is not set
# CT_DEBUG_CT is not set

#
# Paths
#
CT_LOCAL_TARBALLS_DIR=""
CT_WORK_DIR="\${CT_TOP_DIR}/.build"
CT_PREFIX_DIR="${OCCROSS_INSTALL_DIR}/\${CT_TARGET}"
CT_INSTALL_DIR="\${CT_PREFIX_DIR}"
CT_RM_RF_PREFIX_DIR=y
CT_REMOVE_DOCS=y
CT_INSTALL_DIR_RO=n
CT_STRIP_HOST_TOOLCHAIN_EXECUTABLES=y
# CT_STRIP_TARGET_TOOLCHAIN_EXECUTABLES is not set

#
# Downloading
#
# CT_FORBID_DOWNLOAD is not set
# CT_FORCE_DOWNLOAD is not set
CT_CONNECT_TIMEOUT=10
# CT_ONLY_DOWNLOAD is not set
# CT_USE_MIRROR is not set

#
# Extracting
#
# CT_FORCE_EXTRACT is not set
CT_OVERIDE_CONFIG_GUESS_SUB=y
# CT_ONLY_EXTRACT is not set
CT_PATCH_BUNDLED=y
# CT_PATCH_LOCAL is not set
# CT_PATCH_BUNDLED_LOCAL is not set
# CT_PATCH_LOCAL_BUNDLED is not set
# CT_PATCH_BUNDLED_FALLBACK_LOCAL is not set
# CT_PATCH_LOCAL_FALLBACK_BUNDLED is not set
# CT_PATCH_NONE is not set
CT_PATCH_ORDER="bundled"

#
# Build behavior
#
CT_PARALLEL_JOBS=0
CT_LOAD=""
CT_USE_PIPES=n
CT_EXTRA_CFLAGS_FOR_BUILD=""
CT_EXTRA_LDFLAGS_FOR_BUILD=""
CT_EXTRA_CFLAGS_FOR_HOST=""
CT_EXTRA_LDFLAGS_FOR_HOST=""
# CT_CONFIG_SHELL_SH is not set
# CT_CONFIG_SHELL_ASH is not set
CT_CONFIG_SHELL_BASH=y
# CT_CONFIG_SHELL_CUSTOM is not set
CT_CONFIG_SHELL="\${bash}"

#
# Logging
#
# CT_LOG_ERROR is not set
# CT_LOG_WARN is not set
CT_LOG_INFO=y
# CT_LOG_EXTRA is not set
# CT_LOG_ALL is not set
# CT_LOG_DEBUG is not set
CT_LOG_LEVEL_MAX="INFO"
# CT_LOG_SEE_TOOLS_WARN is not set
CT_LOG_PROGRESS_BAR=y
CT_LOG_TO_FILE=y
CT_LOG_FILE_COMPRESS=y

#
# Target options
#
CT_ARCH="arm"
CT_ARCH_SUPPORTS_BOTH_MMU=y
CT_ARCH_SUPPORTS_BOTH_ENDIAN=y
CT_ARCH_SUPPORTS_32=y
CT_ARCH_SUPPORTS_64=y
CT_ARCH_SUPPORTS_WITH_ARCH=y
CT_ARCH_SUPPORTS_WITH_CPU=y
CT_ARCH_SUPPORTS_WITH_TUNE=y
CT_ARCH_SUPPORTS_WITH_FLOAT=y
CT_ARCH_SUPPORTS_WITH_FPU=y
CT_ARCH_SUPPORTS_SOFTFP=y
CT_ARCH_DEFAULT_HAS_MMU=y
CT_ARCH_DEFAULT_LE=y
CT_ARCH_DEFAULT_32=y
CT_ARCH_ARCH="armv7-a"
CT_ARCH_CPU=""
CT_ARCH_TUNE=""
CT_ARCH_FPU=""
CT_ARCH_BE=y
# CT_ARCH_LE is not set
CT_ARCH_32=y
# CT_ARCH_64 is not set
CT_ARCH_BITNESS=32
CT_ARCH_FLOAT_HW=y
# CT_ARCH_FLOAT_SW is not set
CT_TARGET_CFLAGS=""
CT_TARGET_LDFLAGS=""
# CT_ARCH_alpha is not set
CT_ARCH_arm=y
# CT_ARCH_avr is not set
# CT_ARCH_m68k is not set
# CT_ARCH_mips is not set
# CT_ARCH_nios2 is not set
# CT_ARCH_powerpc is not set
# CT_ARCH_s390 is not set
# CT_ARCH_sh is not set
# CT_ARCH_sparc is not set
# CT_ARCH_x86 is not set
# CT_ARCH_xtensa is not set
CT_ARCH_alpha_AVAILABLE=y
CT_ARCH_arm_AVAILABLE=y
CT_ARCH_avr_AVAILABLE=y
CT_ARCH_m68k_AVAILABLE=y
CT_ARCH_microblaze_AVAILABLE=y
CT_ARCH_mips_AVAILABLE=y
CT_ARCH_nios2_AVAILABLE=y
CT_ARCH_powerpc_AVAILABLE=y
CT_ARCH_s390_AVAILABLE=y
CT_ARCH_sh_AVAILABLE=y
CT_ARCH_sparc_AVAILABLE=y
CT_ARCH_x86_AVAILABLE=y
CT_ARCH_xtensa_AVAILABLE=y
CT_ARCH_SUFFIX=""

#
# Generic target options
#
# CT_MULTILIB is not set
CT_ARCH_USE_MMU=y
CT_ARCH_ENDIAN="big"

#
# Target optimisations
#
CT_ARCH_EXCLUSIVE_WITH_CPU=y
# CT_ARCH_FLOAT_AUTO is not set
# CT_ARCH_FLOAT_SOFTFP is not set
CT_ARCH_FLOAT="hard"
# CT_ARCH_ALPHA_EV4 is not set
# CT_ARCH_ALPHA_EV45 is not set
# CT_ARCH_ALPHA_EV5 is not set
# CT_ARCH_ALPHA_EV56 is not set
# CT_ARCH_ALPHA_EV6 is not set
# CT_ARCH_ALPHA_EV67 is not set

#
# arm other options
#
CT_ARCH_ARM_MODE="arm"
CT_ARCH_ARM_MODE_ARM=y
# CT_ARCH_ARM_MODE_THUMB is not set
# CT_ARCH_ARM_INTERWORKING is not set
CT_ARCH_ARM_EABI_FORCE=y
CT_ARCH_ARM_EABI=y
# CT_ARCH_ARM_TUPLE_USE_EABIHF is not set

#
# Toolchain options
#

#
# General toolchain options
#
CT_FORCE_SYSROOT=y
CT_USE_SYSROOT=y
CT_SYSROOT_NAME="sysroot"
CT_SYSROOT_DIR_PREFIX=""
# CT_STATIC_TOOLCHAIN is not set
CT_TOOLCHAIN_PKGVERSION=""
CT_TOOLCHAIN_BUGURL=""

#
# Tuple completion and aliasing
#
CT_TARGET_VENDOR="oc_arm"
CT_TARGET_ALIAS_SED_EXPR=""
CT_TARGET_ALIAS=""

#
# Toolchain type
#
CT_CROSS=y
# CT_CANADIAN is not set
CT_TOOLCHAIN_TYPE="cross"

#
# Build system
#
CT_BUILD=""
CT_BUILD_PREFIX=""
CT_BUILD_SUFFIX=""

#
# Misc options
#
# CT_TOOLCHAIN_ENABLE_NLS is not set

#
# Operating System
#
CT_BARE_METAL=y
CT_KERNEL="bare-metal"
CT_KERNEL_bare_metal=y
# CT_KERNEL_linux is not set
CT_KERNEL_bare_metal_AVAILABLE=y
CT_KERNEL_linux_AVAILABLE=y
CT_KERNEL_windows_AVAILABLE=y

#
# Common kernel options
#

#
# Binary utilities
#
CT_ARCH_BINFMT_ELF=y
CT_BINUTILS="binutils"
CT_BINUTILS_binutils=y

#
# GNU binutils
#
CT_BINUTILS_VERSION="2.26"
# CT_CC_BINUTILS_SHOW_LINARO is not set
CT_BINUTILS_V_2_26=y
# CT_BINUTILS_V_2_25_1 is not set
# CT_BINUTILS_V_2_24 is not set
# CT_BINUTILS_V_2_23_2 is not set
CT_BINUTILS_2_26_or_later=y
CT_BINUTILS_2_25_1_or_later=y
CT_BINUTILS_2_25_or_later=y
CT_BINUTILS_2_24_or_later=y
CT_BINUTILS_2_23_2_or_later=y
CT_BINUTILS_HAS_HASH_STYLE=y
CT_BINUTILS_HAS_GOLD=y
CT_BINUTILS_GOLD_SUPPORTS_ARCH=y
CT_BINUTILS_GOLD_SUPPORT=y
CT_BINUTILS_HAS_PLUGINS=y
CT_BINUTILS_HAS_PKGVERSION_BUGURL=y
CT_BINUTILS_LINKER_LD=y
# CT_BINUTILS_LINKER_GOLD is not set
# CT_BINUTILS_LINKER_LD_GOLD is not set
# CT_BINUTILS_LINKER_GOLD_LD is not set
CT_BINUTILS_LINKERS_LIST="ld"
CT_BINUTILS_LINKER_DEFAULT="bfd"
# CT_BINUTILS_PLUGINS is not set
CT_BINUTILS_EXTRA_CONFIG_ARRAY=""

#
# binutils other options
#

#
# C-library
#
CT_LIBC="newlib"
CT_LIBC_VERSION="2.3.0.20160226"
CT_LIBC_newlib=y
# CT_LIBC_none is not set
CT_LIBC_avr_libc_AVAILABLE=y
CT_LIBC_glibc_AVAILABLE=y
CT_THREADS="none"
CT_LIBC_mingw_AVAILABLE=y
CT_LIBC_musl_AVAILABLE=y
CT_LIBC_newlib_AVAILABLE=y
# CT_CC_NEWLIB_SHOW_LINARO is not set
CT_LIBC_NEWLIB_V_2_3_0=y
# CT_LIBC_NEWLIB_V_2_2_0 is not set
# CT_LIBC_NEWLIB_V_2_1_0 is not set
# CT_LIBC_NEWLIB_V_2_0_0 is not set
# CT_LIBC_NEWLIB_V_1_20_0 is not set
# CT_LIBC_NEWLIB_V_1_19_0 is not set
# CT_LIBC_NEWLIB_V_1_18_0 is not set
# CT_LIBC_NEWLIB_V_1_17_0 is not set
CT_LIBC_NEWLIB_2_3=y
CT_LIBC_NEWLIB_2_3_or_later=y
CT_LIBC_NEWLIB_2_2_or_later=y
CT_LIBC_NEWLIB_2_1_or_later=y
CT_LIBC_NEWLIB_2_0_or_later=y
CT_LIBC_NEWLIB_TARGET_CFLAGS=""
CT_LIBC_none_AVAILABLE=y
CT_LIBC_uClibc_AVAILABLE=y
CT_LIBC_SUPPORT_THREADS_NONE=y
CT_LIBC_PROVIDES_CXA_ATEXIT=y

#
# Common C library options
#
CT_THREADS_NONE=y

#
# newlib other options
#
CT_LIBC_NEWLIB_IO_C99FMT=y
CT_LIBC_NEWLIB_IO_LL=y
CT_LIBC_NEWLIB_IO_FLOAT=y
# CT_LIBC_NEWLIB_IO_LDBL is not set
CT_LIBC_NEWLIB_DISABLE_SUPPLIED_SYSCALLS=y
CT_LIBC_NEWLIB_ENABLE_TARGET_OPTSPACE=y
# CT_LIBC_NEWLIB_NANO_MALLOC is not set
# CT_LIBC_NEWLIB_NANO_FORMATTED_IO is not set
CT_LIBC_NEWLIB_EXTRA_CONFIG_ARRAY=""

#
# C compiler
#
CT_CC="gcc"
CT_CC_CORE_PASS_2_NEEDED=y
CT_CC_gcc=y
CT_CC_GCC_VERSION="6.2.0"
# CT_CC_GCC_SHOW_LINARO is not set
CT_CC_GCC_V_6_2_0=y
# CT_CC_GCC_V_5_4_0 is not set
# CT_CC_GCC_V_4_9_4 is not set
# CT_CC_GCC_V_4_8_5 is not set
CT_CC_GCC_4_8_or_later=y
CT_CC_GCC_4_9_or_later=y
CT_CC_GCC_5_or_later=y
CT_CC_GCC_6=y
CT_CC_GCC_6_or_later=y
CT_CC_GCC_HAS_GRAPHITE=y
CT_CC_GCC_USE_GRAPHITE=y
CT_CC_GCC_HAS_LTO=y
# CT_CC_GCC_USE_LTO is not set
CT_CC_GCC_HAS_PKGVERSION_BUGURL=y
CT_CC_GCC_HAS_BUILD_ID=y
CT_CC_GCC_HAS_LNK_HASH_STYLE=y
CT_CC_GCC_USE_GMP_MPFR=y
CT_CC_GCC_USE_MPC=y
CT_CC_GCC_HAS_LIBQUADMATH=y
CT_CC_GCC_HAS_LIBSANITIZER=y
CT_CC_GCC_HAS_LIBMPX=y
# CT_CC_LANG_FORTRAN is not set
CT_CC_GCC_ENABLE_CXX_FLAGS=""
CT_CC_GCC_CORE_EXTRA_CONFIG_ARRAY=""
CT_CC_GCC_EXTRA_CONFIG_ARRAY=""
# CT_CC_GCC_TARGET_FINAL is not set
# CT_CC_GCC_STATIC_LIBSTDCXX is not set
# CT_CC_GCC_SYSTEM_ZLIB is not set

#
# Optimisation features
#

#
# Settings for libraries running on target
#
CT_CC_GCC_ENABLE_TARGET_OPTSPACE=y
# CT_CC_GCC_LIBMUDFLAP is not set
# CT_CC_GCC_LIBSSP is not set
# CT_CC_GCC_LIBQUADMATH is not set

#
# Misc. obscure options.
#
CT_CC_CXA_ATEXIT=y
# CT_CC_GCC_DISABLE_PCH is not set
# CT_CC_GCC_LDBL_128 is not set
# CT_CC_GCC_BUILD_ID is not set
CT_CC_GCC_LNK_HASH_STYLE_DEFAULT=y
# CT_CC_GCC_LNK_HASH_STYLE_SYSV is not set
# CT_CC_GCC_LNK_HASH_STYLE_GNU is not set
# CT_CC_GCC_LNK_HASH_STYLE_BOTH is not set
CT_CC_GCC_LNK_HASH_STYLE=""
CT_CC_GCC_DEC_FLOAT_AUTO=y
# CT_CC_GCC_DEC_FLOAT_BID is not set
# CT_CC_GCC_DEC_FLOAT_DPD is not set
# CT_CC_GCC_DEC_FLOATS_NO is not set
CT_CC_SUPPORT_CXX=y
CT_CC_SUPPORT_FORTRAN=y
CT_CC_SUPPORT_JAVA=y
CT_CC_SUPPORT_ADA=y
CT_CC_SUPPORT_OBJC=y
CT_CC_SUPPORT_OBJCXX=y
CT_CC_SUPPORT_GOLANG=y

#
# Additional supported languages:
#
CT_CC_LANG_CXX=y

#
# Debug facilities
#
CT_DEBUG_gdb=y
CT_GDB_CROSS=y
# CT_GDB_CROSS_STATIC is not set
# CT_GDB_CROSS_SIM is not set
CT_GDB_CROSS_PYTHON=y
CT_GDB_CROSS_EXTRA_CONFIG_ARRAY=""

#
# In bare-metal, you'll need to   
#

#
# provide your own gdbserver stub.
#

#
# gdb version
#
CT_GDB_VERSION="7.11"
# CT_DEBUG_GDB_SHOW_LINARO is not set
CT_GDB_V_7_11=y
# CT_GDB_V_7_10_1 is not set
# CT_GDB_V_7_10 is not set
# CT_GDB_V_7_9_1 is not set
# CT_GDB_V_7_9 is not set
# CT_GDB_V_7_8_2 is not set
# CT_GDB_V_7_8_1 is not set
# CT_GDB_V_7_8 is not set
# CT_GDB_V_7_7_1 is not set
# CT_GDB_V_7_7 is not set
# CT_GDB_V_7_6_1 is not set
# CT_GDB_V_7_5_1 is not set
# CT_GDB_V_7_4_1 is not set
# CT_GDB_V_7_4 is not set
# CT_GDB_V_7_3_1 is not set
# CT_GDB_V_7_3a is not set
# CT_GDB_V_7_2a is not set
# CT_GDB_V_7_1a is not set
# CT_GDB_V_7_0_1a is not set
# CT_GDB_V_7_0a is not set
# CT_GDB_V_6_8a is not set
CT_GDB_7_2_or_later=y
CT_GDB_7_0_or_later=y
CT_GDB_HAS_PKGVERSION_BUGURL=y
CT_GDB_HAS_PYTHON=y
CT_GDB_INSTALL_GDBINIT=y
# CT_DEBUG_ltrace is not set
# CT_DEBUG_strace is not set

#
# Companion libraries
#
CT_COMPLIBS_NEEDED=y
CT_GMP_NEEDED=y
CT_MPFR_NEEDED=y
CT_ISL_NEEDED=y
CT_MPC_NEEDED=y
CT_EXPAT_NEEDED=y
CT_NCURSES_NEEDED=y
CT_COMPLIBS=y
CT_GMP=y
CT_MPFR=y
CT_ISL=y
CT_MPC=y
CT_EXPAT=y
CT_NCURSES=y
CT_GMP_V_6_1_0=y
# CT_GMP_V_6_0_0 is not set
# CT_GMP_V_5_1_3 is not set
# CT_GMP_V_5_1_1 is not set
# CT_GMP_V_5_0_2 is not set
# CT_GMP_V_5_0_1 is not set
# CT_GMP_V_4_3_2 is not set
# CT_GMP_V_4_3_1 is not set
# CT_GMP_V_4_3_0 is not set
CT_GMP_5_0_2_or_later=y
CT_GMP_VERSION="6.1.0"
CT_MPFR_V_3_1_3=y
# CT_MPFR_V_3_1_2 is not set
# CT_MPFR_V_3_1_0 is not set
# CT_MPFR_V_3_0_1 is not set
# CT_MPFR_V_3_0_0 is not set
# CT_MPFR_V_2_4_2 is not set
# CT_MPFR_V_2_4_1 is not set
# CT_MPFR_V_2_4_0 is not set
CT_MPFR_VERSION="3.1.3"
CT_ISL_V_0_14=y
# CT_ISL_V_0_12_2 is not set
CT_ISL_V_0_14_or_later=y
CT_ISL_V_0_12_or_later=y
CT_ISL_VERSION="0.14"
CT_MPC_V_1_0_3=y
# CT_MPC_V_1_0_2 is not set
# CT_MPC_V_1_0_1 is not set
# CT_MPC_V_1_0 is not set
# CT_MPC_V_0_9 is not set
# CT_MPC_V_0_8_2 is not set
# CT_MPC_V_0_8_1 is not set
# CT_MPC_V_0_7 is not set
CT_MPC_VERSION="1.0.3"
CT_EXPAT_V_2_1_0=y
CT_EXPAT_VERSION="2.1.0"
CT_NCURSES_V_6_0=y
CT_NCURSES_VERSION="6.0"

#
# Companion libraries common options
#
# CT_COMPLIBS_CHECK is not set

#
# Companion tools
#

#
# READ HELP before you say 'Y' below !!!
#
# CT_COMP_TOOLS is not set
EOF
ct-ng oldconfig < /dev/null
ct-ng build
cd ..

rm -rf build-le
mkdir -p build-le
cd build-le
sed -e "s/CT_ARCH_BE=y/CT_ARCH_LE=y/" -e "s/CT_ARCH_LE is not set/CT_ARCH_BE is not set/" < ../build-be/.config > .config
mkdir -p .build
# don't use ln -s because of MinGW
cp -r ../build-be/.build/tarballs .build/
ct-ng oldconfig < /dev/null
ct-ng build
cd ..

echo "-----"
echo "All done!"
