#!/bin/bash

SCRIPT_FILE_DIR=`dirname $0`
SCRIPT_FILE_DIR=`cd $SCRIPT_FILE_DIR; pwd`
CHECKOUT_DIR="$SCRIPT_FILE_DIR/checkout/core-plot"
GENERATED_PROJECT_DIR="$SCRIPT_FILE_DIR/CorePlot-CocoaTouch"
GENERATED_SOURCE_DIR="$GENERATED_PROJECT_DIR/Sources"
MAKE_SCRIPT="$SCRIPT_FILE_DIR/../../../make-fmwk.sh"
CORE_PLOT_VERSION="0.2.2"

# Get the source code if not already checked out
echo "Checking out source code..."
if [ ! -d "$CHECKOUT_DIR" ]; then
    hg clone -u alpharelease_0.2.2 https://core-plot.googlecode.com/hg/ "$CHECKOUT_DIR"
fi

# Check if the source code could be checked out
if [ ! -d "$CHECKOUT_DIR" ]; then
    echo "Source code checkout failure"
    exit 1
fi

# Cleanup generated project directory before creating it again
echo "Copying source files..."
if [ -d "$GENERATED_SOURCE_DIR" ]; then
    rm -rf "$GENERATED_SOURCE_DIR"
fi
mkdir -p "$GENERATED_SOURCE_DIR"

# Copy the required source files
cd "$CHECKOUT_DIR/framework/Source"
cp CPAnnotation.h "$GENERATED_SOURCE_DIR"
cp CPAnnotation.m "$GENERATED_SOURCE_DIR"
cp CPAnnotationHostLayer.h "$GENERATED_SOURCE_DIR"
cp CPAnnotationHostLayer.m "$GENERATED_SOURCE_DIR"
cp CPAxis.h "$GENERATED_SOURCE_DIR"
cp CPAxis.m "$GENERATED_SOURCE_DIR"
cp CPAxisLabel.h "$GENERATED_SOURCE_DIR"
cp CPAxisLabel.m "$GENERATED_SOURCE_DIR"
cp CPAxisLabelGroup.h "$GENERATED_SOURCE_DIR"
cp CPAxisLabelGroup.m "$GENERATED_SOURCE_DIR"
cp CPAxisSet.h "$GENERATED_SOURCE_DIR"
cp CPAxisSet.m "$GENERATED_SOURCE_DIR"
cp CPAxisTitle.h "$GENERATED_SOURCE_DIR"
cp CPAxisTitle.m "$GENERATED_SOURCE_DIR"
cp CPBarPlot.h "$GENERATED_SOURCE_DIR"
cp CPBarPlot.m "$GENERATED_SOURCE_DIR"
cp CPBorderedLayer.h "$GENERATED_SOURCE_DIR"
cp CPBorderedLayer.m "$GENERATED_SOURCE_DIR"
cp CPColor.h "$GENERATED_SOURCE_DIR"
cp CPColor.m "$GENERATED_SOURCE_DIR"
cp CPColorSpace.h "$GENERATED_SOURCE_DIR"
cp CPColorSpace.m "$GENERATED_SOURCE_DIR"
cp CPConstrainedPosition.h "$GENERATED_SOURCE_DIR"
cp CPConstrainedPosition.m "$GENERATED_SOURCE_DIR"
cp CPDarkGradientTheme.h "$GENERATED_SOURCE_DIR"
cp CPDarkGradientTheme.m "$GENERATED_SOURCE_DIR"
cp CPDefinitions.h "$GENERATED_SOURCE_DIR"
cp CPDefinitions.m "$GENERATED_SOURCE_DIR"
cp CPExceptions.h "$GENERATED_SOURCE_DIR"
cp CPExceptions.m "$GENERATED_SOURCE_DIR"
cp CPFill.h "$GENERATED_SOURCE_DIR"
cp CPFill.m "$GENERATED_SOURCE_DIR"
cp CPGradient.h "$GENERATED_SOURCE_DIR"
cp CPGradient.m "$GENERATED_SOURCE_DIR"
cp CPGraph.h "$GENERATED_SOURCE_DIR"
cp CPGraph.m "$GENERATED_SOURCE_DIR"
cp CPGridLineGroup.h "$GENERATED_SOURCE_DIR"
cp CPGridLineGroup.m "$GENERATED_SOURCE_DIR"
cp CPGridLines.h "$GENERATED_SOURCE_DIR"
cp CPGridLines.m "$GENERATED_SOURCE_DIR"
cp CPImage.h "$GENERATED_SOURCE_DIR"
cp CPImage.m "$GENERATED_SOURCE_DIR"
cp CPLayer.h "$GENERATED_SOURCE_DIR"
cp CPLayer.m "$GENERATED_SOURCE_DIR"
cp CPLayerAnnotation.h "$GENERATED_SOURCE_DIR"
cp CPLayerAnnotation.m "$GENERATED_SOURCE_DIR"
cp CPLayoutManager.h "$GENERATED_SOURCE_DIR"
cp CPLayoutManager.m "$GENERATED_SOURCE_DIR"
cp CPLimitBand.h "$GENERATED_SOURCE_DIR"
cp CPLimitBand.m "$GENERATED_SOURCE_DIR"
cp CPLineStyle.h "$GENERATED_SOURCE_DIR"
cp CPLineStyle.m "$GENERATED_SOURCE_DIR"
cp CPMutableNumericData+TypeConversion.h "$GENERATED_SOURCE_DIR"
cp CPMutableNumericData+TypeConversion.m "$GENERATED_SOURCE_DIR"
cp CPMutableNumericData.h "$GENERATED_SOURCE_DIR"
cp CPMutableNumericData.m "$GENERATED_SOURCE_DIR"
cp CPNumericData+TypeConversion.h "$GENERATED_SOURCE_DIR"
cp CPNumericData+TypeConversion.m "$GENERATED_SOURCE_DIR"
cp CPNumericData.h "$GENERATED_SOURCE_DIR"
cp CPNumericData.m "$GENERATED_SOURCE_DIR"
cp CPNumericDataType.h "$GENERATED_SOURCE_DIR"
cp CPNumericDataType.m "$GENERATED_SOURCE_DIR"
cp CPPathExtensions.h "$GENERATED_SOURCE_DIR"
cp CPPathExtensions.m "$GENERATED_SOURCE_DIR"
cp CPPieChart.h "$GENERATED_SOURCE_DIR"
cp CPPieChart.m "$GENERATED_SOURCE_DIR"
cp CPPlainBlackTheme.h "$GENERATED_SOURCE_DIR"
cp CPPlainBlackTheme.m "$GENERATED_SOURCE_DIR"
cp CPPlainWhiteTheme.h "$GENERATED_SOURCE_DIR"
cp CPPlainWhiteTheme.m "$GENERATED_SOURCE_DIR"
cp CPPlot.h "$GENERATED_SOURCE_DIR"
cp CPPlot.m "$GENERATED_SOURCE_DIR"
cp CPPlotArea.h "$GENERATED_SOURCE_DIR"
cp CPPlotArea.m "$GENERATED_SOURCE_DIR"
cp CPPlotAreaFrame.h "$GENERATED_SOURCE_DIR"
cp CPPlotAreaFrame.m "$GENERATED_SOURCE_DIR"
cp CPPlotGroup.h "$GENERATED_SOURCE_DIR"
cp CPPlotGroup.m "$GENERATED_SOURCE_DIR"
cp CPPlotRange.h "$GENERATED_SOURCE_DIR"
cp CPPlotRange.m "$GENERATED_SOURCE_DIR"
cp CPPlotSpace.h "$GENERATED_SOURCE_DIR"
cp CPPlotSpace.m "$GENERATED_SOURCE_DIR"
cp CPPlotSpaceAnnotation.h "$GENERATED_SOURCE_DIR"
cp CPPlotSpaceAnnotation.m "$GENERATED_SOURCE_DIR"
cp CPPlotSymbol.h "$GENERATED_SOURCE_DIR"
cp CPPlotSymbol.m "$GENERATED_SOURCE_DIR"
cp CPPolarPlotSpace.h "$GENERATED_SOURCE_DIR"
cp CPPolarPlotSpace.m "$GENERATED_SOURCE_DIR"
cp CPResponder.h "$GENERATED_SOURCE_DIR"
cp CPScatterPlot.h "$GENERATED_SOURCE_DIR"
cp CPScatterPlot.m "$GENERATED_SOURCE_DIR"
cp CPSlateTheme.h "$GENERATED_SOURCE_DIR"
cp CPSlateTheme.m "$GENERATED_SOURCE_DIR"
cp CPStocksTheme.h "$GENERATED_SOURCE_DIR"
cp CPStocksTheme.m "$GENERATED_SOURCE_DIR"
cp CPTextLayer.h "$GENERATED_SOURCE_DIR"
cp CPTextLayer.m "$GENERATED_SOURCE_DIR"
cp CPTextStyle.h "$GENERATED_SOURCE_DIR"
cp CPTextStyle.m "$GENERATED_SOURCE_DIR"
cp CPTheme.h "$GENERATED_SOURCE_DIR"
cp CPTheme.m "$GENERATED_SOURCE_DIR"
cp CPTimeFormatter.h "$GENERATED_SOURCE_DIR"
cp CPTimeFormatter.m "$GENERATED_SOURCE_DIR"
cp CPTradingRangePlot.h "$GENERATED_SOURCE_DIR"
cp CPTradingRangePlot.m "$GENERATED_SOURCE_DIR"
cp CPUtilities.h "$GENERATED_SOURCE_DIR"
cp CPUtilities.m "$GENERATED_SOURCE_DIR"
cp CPXYAxis.h "$GENERATED_SOURCE_DIR"
cp CPXYAxis.m "$GENERATED_SOURCE_DIR"
cp CPXYAxisSet.h "$GENERATED_SOURCE_DIR"
cp CPXYAxisSet.m "$GENERATED_SOURCE_DIR"
cp CPXYGraph.h "$GENERATED_SOURCE_DIR"
cp CPXYGraph.m "$GENERATED_SOURCE_DIR"
cp CPXYPlotSpace.h "$GENERATED_SOURCE_DIR"
cp CPXYPlotSpace.m "$GENERATED_SOURCE_DIR"
cp CPXYTheme.h "$GENERATED_SOURCE_DIR"
cp CPXYTheme.m "$GENERATED_SOURCE_DIR"
cp NSDecimalNumberExtensions.h "$GENERATED_SOURCE_DIR"
cp NSDecimalNumberExtensions.m "$GENERATED_SOURCE_DIR"
cp NSExceptionExtensions.h "$GENERATED_SOURCE_DIR"
cp NSExceptionExtensions.m "$GENERATED_SOURCE_DIR"
cp NSNumberExtensions.h "$GENERATED_SOURCE_DIR"
cp NSNumberExtensions.m "$GENERATED_SOURCE_DIR"
cp _CPFillColor.h "$GENERATED_SOURCE_DIR"
cp _CPFillColor.m "$GENERATED_SOURCE_DIR"
cp _CPFillGradient.h "$GENERATED_SOURCE_DIR"
cp _CPFillGradient.m "$GENERATED_SOURCE_DIR"
cp _CPFillImage.h "$GENERATED_SOURCE_DIR"
cp _CPFillImage.m "$GENERATED_SOURCE_DIR"

cd "$CHECKOUT_DIR/framework/iPhoneOnly"
cp CPGraphHostingView.h "$GENERATED_SOURCE_DIR"
cp CPGraphHostingView.m "$GENERATED_SOURCE_DIR"
cp CPPlatformSpecificCategories.h "$GENERATED_SOURCE_DIR"
cp CPPlatformSpecificCategories.m "$GENERATED_SOURCE_DIR"
cp CPPlatformSpecificDefines.h "$GENERATED_SOURCE_DIR"
cp CPPlatformSpecificDefines.m "$GENERATED_SOURCE_DIR"
cp CPPlatformSpecificFunctions.h "$GENERATED_SOURCE_DIR"
cp CPPlatformSpecificFunctions.m "$GENERATED_SOURCE_DIR"
cp CPTextStylePlatformSpecific.h "$GENERATED_SOURCE_DIR"
cp CPTextStylePlatformSpecific.m "$GENERATED_SOURCE_DIR"

cd "$CHECKOUT_DIR/framework/TestResources"
cp CorePlotProbes.d "$GENERATED_SOURCE_DIR"

cd "$CHECKOUT_DIR/framework/SDKBuild"
cp cp.sed "$GENERATED_SOURCE_DIR"
cp build.sh "$GENERATED_SOURCE_DIR"

# Build the framework
echo "Creating frameworks..."
cd "$GENERATED_PROJECT_DIR"
"$MAKE_SCRIPT" -u "$CORE_PLOT_VERSION" Release
"$MAKE_SCRIPT" -u "$CORE_PLOT_VERSION" Debug
"$MAKE_SCRIPT" -u "$CORE_PLOT_VERSION" -s Debug