#!/bin/bash

# The script downloads and unzip ocStore.
# Should be executed from the project root folder.

set -e

OCSTORE_VERSION=v3.0.3.7
CACHE_DIR=.cache

echo "👽 Downloading ocStore ${OCSTORE_VERSION}..."

DOWNLOAD_URL="https://github.com/ocStore/ocStore/archive/refs/tags/$OCSTORE_VERSION.zip"

FILE_PATH="$CACHE_DIR/$OCSTORE_VERSION.zip"

if [ ! -f "$FILE_PATH" ]; then
    mkdir -p "$CACHE_DIR"
    wget -P "$CACHE_DIR" "$DOWNLOAD_URL"
else
    echo "😎 Get file from cache: $FILE_PATH"
fi

echo "🗃 Unzipping..."
rm -rf src/*

unzip -q "$FILE_PATH" -d ./src
SOURCE_DIR=src/ocStore-${OCSTORE_VERSION//v/}

rsync -a $SOURCE_DIR/* ./src
rm -rf $SOURCE_DIR

chmod -R 775 src

echo "✅ Completed."
