#!/bin/zsh
#
# Copyright 2000-2005 Omni Development, Inc.  All rights reserved.
#
# This software may only be used and reproduced according to the
# terms in the file OmniSourceLicense.html, which should be
# distributed with this project and can also be found at
# <http://www.omnigroup.com/developer/sourcecode/sourcelicense/>.
#
# $Header: svn+ssh://source.omnigroup.com/Source/svn/Omni/tags/OmniSourceRelease/2008-09-06/OmniGroup/Scripts/Build-Xcode2.2 89481 2007-08-02 00:53:33Z kc $

# Backwards compatibility -- if we are in the OmniGroup directory, cd up one directory
if [ "$PWD:t" = "OmniGroup" ]; then
  cd ..
  echo "Now in: $PWD"
fi

PUBLIC=/Users/Shared

ProjectName="$1"
shift

if [ ! -f /usr/bin/xcodebuild ]; then
    echo "***********************************"
    echo "Xcode 2.2 is not installed, exiting"
    echo "***********************************"
    exit 1
fi

if [ -z "$OMNI_PRODUCT_DIRECTORY" ]; then
    # Read the product and build directories from Xcode's defaults
    productDirectory=$(defaults read com.apple.Xcode PBXProductDirectory | sed 's/\$(USER)/'$USER'/g')
    if [ -z "$productDirectory" ]; then
        productDirectory="$PUBLIC/$USER/Products"
        echo "Warning: PBXProductDirectory not defined in Xcode, using $productDirectory"
    fi

    buildDirectory=$(defaults read com.apple.Xcode PBXIntermediatesDirectory | sed 's/\$(USER)/'$USER'/g')
    if [ -z "$buildDirectory" ]; then
        buildDirectory="$productDirectory/Builds"
        echo "Warning: PBXIntermediatesDirectory not defined in Xcode, using $buildDirectory"
    fi
else
    # The user has explicitly set an OMNI_PRODUCT_DIRECTORY environment variable for us to use
    productDirectory="$OMNI_PRODUCT_DIRECTORY"
    buildDirectory="$productDirectory/Builds"
fi

mkdir -p "$productDirectory"

# This is what we'll pass to xcodebuild
BUILD_ACTION=$*
echo BUILD_ACTION=$BUILD_ACTION

if [ "$BUILD_ACTION" = "install" ]; then
    BUILD_CONFIGURATION=Release
    export LD_SEG_ADDR_TABLE="$PWD/OmniGroup/Scripts/SegAddrTable.txt"
    export LD_PREBIND="YES"
    if [ -z "$OMNI_INSTALLED_PRODUCTS" ]; then
        OMNI_INSTALLED_PRODUCTS=`dirname $productDirectory`/InstalledProducts
    fi

    echo "Making installed product files writable"
    chmod -R u+w "$OMNI_INSTALLED_PRODUCTS"
    OMNI_FRAMEWORK_TARGET_PATH="@executable_path/../Frameworks"
elif [ "$BUILD_ACTION" = "coverage" ]; then
    if [ -z "$BUILD_CONFIGURATION" ]; then
        BUILD_CONFIGURATION=Coverage
    fi
    OMNI_INSTALLED_PRODUCTS=""
    OMNI_FRAMEWORK_TARGET_PATH="$productDirectory/$BUILD_CONFIGURATION"

    # xcodebuild doesn't allow 'coverage' as an argument
    BUILD_ACTION=
elif [ "$BUILD_ACTION" = "" ] || [ "$BUILD_ACTION" = "clean" ]; then
    if [ -z "$BUILD_CONFIGURATION" ]; then
        BUILD_CONFIGURATION=Debug
    fi
    OMNI_INSTALLED_PRODUCTS=""
    OMNI_FRAMEWORK_TARGET_PATH="$productDirectory/$BUILD_CONFIGURATION"
else
    echo "Error handling arguments: " $BUILD_ACTION
    echo "Don't know which build style to use"
    exit 1
fi

echo "#"
echo "#"  `date`
echo "#"  Starting build of $ProjectName
echo "#"  BUILD_CONFIGURATION=$BUILD_CONFIGURATION
echo "#"


./OmniGroup/Scripts/projectList $ProjectName | sed 's/^\.\///' | while read project tag
do
    directory="${project%\(*\)}"
    if [ "$directory" != "$project" ]; then
        spec=`echo "${(M)project%\(*\)}" | sed 's/(\(.*\))/\1/'`
        target=$(echo $spec | cut -f1 -d: | perl -pe 's/\s+//mg')
        style=$(echo $spec | cut -sf2 -d: | perl -pe 's/\s+//mg')
    else
        target=''
        style=''
    fi
            
    if [ "$target" = "nobuild" ]; then
        echo "Skipping $project..."
    else
    (
        if [ -z "$style" ]; then
            pbxopts=( '-configuration' "$BUILD_CONFIGURATION" )
        else
            pbxopts=( '-configuration' "$BUILD_CONFIGURATION-$style" )
        fi

        if [ -n "$target" ]; then
                pbxopts=($pbxopts '-target' "$target")
        fi

        cd $directory

        projectName="`echo *.xcodeproj(N)`"
        pbxopts=($pbxopts '-project' "$projectName" )

        if [ "$projectName" = "" ]; then
            echo "***********************************"
            echo "$directory has no Xcode 2.2 project"
            echo "***********************************"
            exit 1
        else
            echo "Building ${projectName%.xcodeproj} (${project})"
            (
                set -x
                xcodebuild DSTROOT="/" SYMROOT="$productDirectory" OBJROOT="$buildDirectory" OMNI_INSTALLED_PRODUCTS="$OMNI_INSTALLED_PRODUCTS" OMNI_FRAMEWORK_TARGET_PATH="$OMNI_FRAMEWORK_TARGET_PATH" $pbxopts $BUILD_ACTION
            )
        fi
    ) || (
        echo "Build failed in ${projectName%.xcodeproj} (${project})"
        exit 1
    ) || exit 1
    fi
    echo
done
