#!/bin/bash

# Get env variables
. ../.env

REGISTRY_ID=409
EMAIL_ASSETS_PATH="src/assets/icons/email"
ECOGESTURE_ASSETS_PATH="src/assets/icons/visu/ecogesture"
pwd=$(pwd)

# Fetch and convert email assets
curl "https://forge.grandlyon.com/api/v4/projects/${REGISTRY_ID}/repository/archive?path=${EMAIL_ASSETS_PATH}" --output email.tar.gz
tar -xf email.tar.gz
cd *-email/$EMAIL_ASSETS_PATH && for file in *.svg; do inkscape -h 200 --export-type="png" $file; done && rm *.svg

cd $pwd
# Fetch and convert ecogesture assets
curl "https://forge.grandlyon.com/api/v4/projects/${REGISTRY_ID}/repository/archive?path=${ECOGESTURE_ASSETS_PATH}" --output ecogesture.tar.gz
tar -xf ecogesture.tar.gz
cd *-ecogesture/$ECOGESTURE_ASSETS_PATH && for file in *.svg; do inkscape -h 200 --export-type="png" $file; done && rm *.svg

# Cleanup
cd $pwd
rm -rf ../${IMAGE_FOLDER}/*

# Copy assets in IMAGE_FOLDER
mv *-email/$EMAIL_ASSETS_PATH/* ../${IMAGE_FOLDER}
mv *-ecogesture/$ECOGESTURE_ASSETS_PATH ../${IMAGE_FOLDER}/ecogesture
rm -rf email.tar.gz ecogesture.tar.gz *-email *-ecogesture
