#!/bin/sh

cd ios/

VERSION=$1
if type /usr/libexec/PlistBuddy &> /dev/null; then
  CURRENT_VERSION=$(/usr/libexec/PlistBuddy -c "Print :MGLSemanticVersionString" GoongSDK.framework/Info.plist)
else
  CURRENT_VERSION=$(cat .framework_version)
fi

if [ "$VERSION" == "$CURRENT_VERSION" ]; then
  echo "The newest version is already installed. Exiting."
  exit 0
fi

echo "Downloading GoongSDK iOS $VERSION, this may take a minute."

if ! which curl > /dev/null; then echo "curl command not found. Please install curl"; exit 1; fi;
if ! which unzip > /dev/null; then echo "unzip command not found. Please install unzip"; exit 1; fi;

if [ -d ./GoongSDK.framework ]; then
    echo "Old GoongSDK.framework found. Removing it and installing a $VERSION"
    rm -rf ./GoongSDK.framework
fi

curl -L https://github.com/goong-io/Goong-iOS-SDK/releases/download/$VERSION/GoongSDK.framework.zip -o temp.zip
unzip -o temp.zip -d temp
mv temp/dynamic/GoongSDK.framework ./GoongSDK.framework
rm -r temp
rm temp.zip

if ! [ -d ./GoongSDK.framework ]; then
  echo "GoongSDK.framework not found. Please reinstall react-native-GoongSDK-gl"; exit 1;
fi;

echo "$VERSION" > .framework_version
