#!/bin/bash

# See https://stackoverflow.com/questions/24112727/relative-paths-based-on-file-location-instead-of-current-working-directory
WD=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$WD"

# Get env variables
source ../.env

REGISTRY_ID=409
EMAIL_ASSETS_PATH="src/assets/icons/email"
ECOGESTURE_ASSETS_PATH="src/assets/icons/visu/ecogesture"

# Clear folder
rm -rf ../$IMAGE_FOLDER

# Create folder
mkdir -p ../$IMAGE_FOLDER ../$IMAGE_FOLDER/ecogesture

# 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
find *-email/$EMAIL_ASSETS_PATH -type f -name "*.svg" | while read -r svg_file; do
    filename="$(basename "$svg_file" .svg)"
    inkscape -h 200 -o ../$IMAGE_FOLDER/$filename.png *-email/$EMAIL_ASSETS_PATH/$filename.svg
done
rm *-email/$EMAIL_ASSETS_PATH/*.svg

# 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
find *-ecogesture/$ECOGESTURE_ASSETS_PATH -type f -name "*.svg" | while read -r svg_file; do
    filename="$(basename "$svg_file" .svg)"
    inkscape -h 200 -o ../$IMAGE_FOLDER/ecogesture/$filename.png *-ecogesture/$ECOGESTURE_ASSETS_PATH/$filename.svg
done
rm *-ecogesture/$ECOGESTURE_ASSETS_PATH/*.svg

# Cleanup
rm -rf email.tar.gz ecogesture.tar.gz *-email *-ecogesture
