#!/bin/bash

DOCKER_IMAGE=python:3.9-slim-bullseye

echo "\n------ Checking for docker image --------"
docker pull "$DOCKER_IMAGE"
#HOST_URL=$(echo $1 | cut -d'/' -f 1-3)
#PROJECT_NAME=$(echo $1 | cut -d'/' -f 5- | cut -d'.' -f 1)
echo "Pages URL:" $2
echo "PROJECT NAME:" $3
echo "TAG NAME:" $4

echo "------ Get the previous GitLab Pages content --------"
set +e
curl --fail "$2/content.zip" -o "content.zip"
if [ -e "content.zip" ]; then unzip "content.zip"; rm "content.zip"; fi
set -e

echo "------ Add/update content --------"
mkdir -p "public/$3"
cp -r -f "$2_$3.docx" public/$3

docker run --rm -u $(id -u):$(id -g) "$DOCKER_IMAGE" $1 "$3_$4" "$4/$3_$4.docx" "$5"
#echo "<a href="${CI_COMMIT_REF_NAME}/${CI_PROJECT_NAME}_${CI_COMMIT_TAG}.docx" target="_blank">$CI_COMMIT_REF_NAME</a>" >> "public/index.html"


echo "------ Zip the content and publish the zip again ------"
zip -r "content.zip" "public"
mv "content.zip" "public/"
 
exit 0

#updateIndex.py 
#$CI_PAGES_URL
#${CI_PROJECT_NAME}
#$CI_COMMIT_REF_NAME
#index.htm
