#!/bin/sh
#
# Installs optional Xcode helper components for crossclang
#
# You should run this as root, i.e.,: sudo ./install
#

cd "$(dirname $0)"
pwd=$(pwd)

developerPath=$(xcode-select -p)
if [[ -z "$developerPath" ]]; then
    echo "Error: Could not resolve XCode Developer path" >&2
    exit 1
fi
sdksPath="$developerPath/Platforms/MacOSX.platform/Developer/SDKs"
if [[ ! -d "$sdksPath" ]]; then
    echo "Error: Could not find SDKs path: $sdksPath" >&2
    exit 1
fi
toolchainsPath="/Library/Developer/Toolchains"
echo "Toolchains path: $toolchainsPath"
if [[ ! -d "$toolchainsPath" ]]; then
    mkdir -pv "$toolchainsPath"
fi

for toolchain in $(ls -1 | grep '\.xctoolchain$'); do
    echo Installing "$toolchain" ... 
    ( cd "$toolchainsPath" ; ln -sf "$pwd/$toolchain" )
done
echo

echo "SDKs path: $sdksPath"
for sdk in $(ls -1 | grep '\.sdk$'); do
    echo Installing "$sdk" ... 
    ( cd "$sdksPath" ; ln -sf "$pwd/$sdk" )
done

if [ $? -eq 0 ]; then
    echo Done. Now please restart XCode and run \"Clean Build Folder\".
else
    echo There were errors. Try running with sudo.
fi
