# Example
# ./myscript.sh /Users/pavelmasuk/workspace/react/test12/node_modules/react-native-document-reader-api/ release 4.2.93456 4.2.159 beta 0.1.8
#1) path to module
#2) ios api branch(beta or release)
#3) android api version
#4) ios api version
#5) module branch(beta or release, if beta, module name will be changed to react-native-document-reader-api-beta in package.json)
#6) module version
Base_path=$1
Branch_ios=$2
Version_android=$3
Version_ios=$4
Destination_type=$5
React_module_version=$6
PODS_URL_RELEASE='https://pods.regulaforensics.com/DocumentReader/'
PODS_URL_BETA='https://pods.regulaforensics.com/DocumentReaderBeta/'
if [ $Branch_ios == 'Beta' ]; then
    STATE_IOS='Beta'
    URL_IOS=$PODS_URL_BETA
else
	if [ $Branch_ios == 'Release' ]; then
		STATE_IOS=''
		URL_IOS=$PODS_URL_RELEASE
	else
		exit 1
	fi
fi
if [ $Destination_type == 'Beta' ]; then
    DEST_TYPE='-beta'
else
	if [ $Branch_ios == 'Release' ]; then
		DEST_TYPE=''
	else
		exit 1
	fi
fi 
PKG_NAME='react-native-document-reader-api'$DEST_TYPE

cp -R build.gradle "$Base_path/android/"
sed -i -e "s/version_place_holder/$Version_android/" "$Base_path/android/build.gradle"
rm -fr "$Base_path/android/build.gradle-e"

/usr/local/bin/wget -O DocumentReader.zip $URL_IOS$Version_ios/DocumentReader$STATE_IOS-$Version_ios.zip
cp DocumentReader.zip "$Base_path/ios/"
rm DocumentReader.zip
cd "$Base_path/ios/"
rm -fr DocumentReader.framework
unzip -P pcp9100 DocumentReader.zip
rm -fr __MACOSX
rm DocumentReader.zip

cd "$Base_path"
sed -i -e "s/react-native-document-reader-api-beta/react-native-document-reader-api/g" package.json
sed -i -e "s/react-native-document-reader-api/$PKG_NAME/g" package.json
rm -fr package.json-e
/usr/local/bin/jq --arg PKG_NAME "$PKG_NAME" '.name = $PKG_NAME' package.json > tmp.$$.json && mv tmp.$$.json package.json
/usr/local/bin/jq --arg React_module_version "$React_module_version" '.version = $React_module_version' package.json > tmp.$$.json && mv tmp.$$.json package.json

/usr/local/bin/npm publish