#!/bin/bash


##########################
# Extract Libraries
##########################

export SOURCE_PATH="$PROJECT_DIR/../FrameworksPomfort"
export ARCHIVE_LIST="LicenseHandling"

sh "$PROJECT_DIR/../BuildServer/build-tools/releaseScripts/extract.sh"


##########################
# Third party framework
##########################
export DESTINATION_PATH="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/Contents/Frameworks/"
export SOURCE_PATH="$PROJECT_DIR/../FrameworksThirdParty"
export ARCHIVE_LIST="LittleCMS.framework
OgreKit.framework
Sparkle.framework
BWToolkitFramework.framework
ISminiController.dylib
ISminiSDKController.dylib
LutBox.dylib
NetLutBox.dylib
LittleCMS.framework
libavutil.dylib
libavcodec.dylib
libswscale.dylib
libswresample.dylib
libavformat.dylib
libx264.dylib
libwebsockets.dylib
HockeySDK.framework
libCameraImageCore.so
libcudart.dylib
libArriRawSDK.dylib
libtlshook.dylib
ORSSerial.framework
libGRMustache7-MacOS.dylib
libcodexhdedecoder.dylib
SocketRocket.framework"

sh "$PROJECT_DIR/../BuildServer/build-tools/releaseScripts/extractAndCopy.sh"

##########################
# Qt framework for ISMini
# Copy all the stuff from the Archive to Frameworks/QtForIS
##########################
export DESTINATION_PATH="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/Contents/Frameworks/"
export SOURCE_PATH="$PROJECT_DIR/../FrameworksThirdParty/"
export ARCHIVE_LIST="QtForIS"

sh "$PROJECT_DIR/../BuildServer/build-tools/releaseScripts/extractAndCopy.sh"

#make sure the dylibs and Frameworks have correct access rights to be loaded by other users of the mac
find "$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME" \( -name '*.dylib' -or -name '*.so' \) -exec chmod a+rx {} \;

find "$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME" -name '*.framework' -print0 |
while IFS= read -r -d '' ONE_FRAMEWORK; do
FRAMEWORK_NAME_WITH_EXT=`basename "$ONE_FRAMEWORK"`
FRAMEWORK_NAME="${FRAMEWORK_NAME_WITH_EXT%.*}"

#change permissions of symbolic link
chmod -h a+rx "$ONE_FRAMEWORK/$FRAMEWORK_NAME"
#change permissions of the file the link points to
chmod a+rx "$ONE_FRAMEWORK/$FRAMEWORK_NAME"
#change permissions of all the other files in the framework
chmod -R a+r "$ONE_FRAMEWORK"
done

#make sure we can read all directories and open files in them
#that means that the r and x flags have to be set on all directories
find "$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME" -type d -exec chmod a+rx {} \;

##########################
# Install pre-built Pomfort Frameworks in zips
##########################
export DESTINATION_PATH="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/Contents/Frameworks/"
export SOURCE_PATH="$PROJECT_DIR/../FrameworksPomfort"
export ARCHIVE_LIST="EuphonixHIDDriver.framework
PomfortTangentWaveHIDDriver.framework
PomfortARRIRAWDecoder.framework"

sh "$PROJECT_DIR/../BuildServer/build-tools/releaseScripts/extractAndCopy.sh"


##########################
# Install locally built Pomfort Frameworks (no zips)
##########################
export DESTINATION_PATH="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/Contents/Frameworks/"
export SOURCE_PATH="$PROJECT_DIR/../FrameworksPomfort"
export ARCHIVE_LIST="PomfortLogging.framework
PomfortExtensions.framework
PomfortUtilities.framework
PomfortData.framework
PomfortUserInterface.framework
PomfortWildcards.framework
PomfortQuickies64.framework
PomfortAncMetadata.framework
PomfortVideoDevice.framework
PomfortAJAVideoDevice.framework
PomfortDeckLinkVideoDevice.framework
PomfortMetadataFoundation.framework
PomfortGrading.framework
PomfortLockitClient.framework
PomfortAlexaSXT.framework"

source "$PROJECT_DIR/../BuildServer/build-tools/releaseScripts/copyDependencies.sh"



##########################
# Install locally built Debug Frameworks (only in debug)
##########################
if [ "$CONFIGURATION" == "${CONFIGURATION/Release/}" ]; then
    export DESTINATION_PATH="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/Contents/Frameworks/"
    export SOURCE_PATH="$PROJECT_DIR/../FrameworksPomfort"
    export ARCHIVE_LIST="PomfortFakeExternalDeviceSupport.framework"

    source "$PROJECT_DIR/../BuildServer/build-tools/releaseScripts/copyDependencies.sh"
fi



##########################
# Install Tools
##########################
export DESTINATION_PATH="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/Contents/Tools/"
export SOURCE_PATH="$PROJECT_DIR/../BinariesPomfort"
export ARCHIVE_LIST="amlCreator PfnHDLinkServer.app videohubctrl"

sh "$PROJECT_DIR/../BuildServer/build-tools/releaseScripts/extractAndCopy.sh"


##########################
# Install filters
##########################
export DESTINATION_PATH="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/Contents/Filters/"
export SOURCE_PATH="$PROJECT_DIR/../FiltersPomfort"

export ARCHIVE_LIST="PomfortAlexaLog2Look.plugin
PomfortCDL.plugin
PomfortClipping.plugin
Pomfort3DLUT.plugin
PomfortSLog2Video.plugin
Pomfort1DLUT.plugin
PomfortChannelSwap.plugin
PomfortSecondaries.plugin
PomfortV210Decode.plugin
PomfortLensCorrect.plugin"

sh "$PROJECT_DIR/../BuildServer/build-tools/releaseScripts/extractAndCopy.sh"




