#!/bin/bash
###############################################################################
# Include common functions...
# -----------------------------------------------------------------------------
TOP=$(dirname $0)
source "${TOP}/common-functions.sh"
source "${TOP}/config.sh"

VERSION="$1"
LIB_FW="${LIB_NAME}.xcframework"

[[ -z "$VERSION" ]] && FAILURE "Version to download is not specified."

${TOP}/clone.sh $VERSION

# Initial cleanup
[[ -d "${TOP}/${LIB_FW}" ]] && $RM -rf "${TOP}/${LIB_FW}"

PUSH_DIR "$SDK_DIR"

PREPARE_FILE="prepare.sh"
PODSPEC_FILE="${LIB_NAME}.podspec"

[[ ! -f "$PREPARE_FILE" ]] && FAILURE "Cloned repository doesn't contain 'preapre.sh' script."
[[ ! -f "$PODSPEC_FILE" ]] && FAILURE "Cloned repository doesn't contain '${LIB_NAME}.podspec'."

# Determine exact version and hash

SPEC_DATA=( `grep prepare.sh $PODSPEC_FILE` )
SPEC_VER=${SPEC_DATA[1]}
SPEC_SUM=${SPEC_DATA[2]}

[[ -z "$SPEC_VER" ]] && FAILURE "Failed to extract library version from ${LIB_NAME}.podspec"
[[ -z "$SPEC_SUM" ]] && FAILURE "Failed to extract artifact hash from ${LIB_NAME}.podspec"
[[ $SPEC_VER != $VERSION ]] && FAILURE "Version extracted from podspec doesn't match required version $VERSION"

LOG_LINE
LOG "Preparing $LIB_NAME module"
LOG "   Version: $SPEC_VER"
LOG "  Checksum: $SPEC_SUM"
LOG_LINE

./${PREPARE_FILE} $SPEC_VER $SPEC_SUM

$MV "$LIB_NAME.xcframework" ..

POP_DIR

EXIT_SUCCESS -l
