#!/usr/bin/env bash
set -e
sleep 5


echo ">>>> Deploy"
WORKINGDIR=$(pwd)

echo ">>> Files"

if test -d "Files"; then
    echo "Found files to copy"
    cd Files

    for dir in *; do
        echo "Copying files for $dir"
        FILE_PATH="/var/lib/docker/volumes/partition/"

        echo "Copying directory '$dir' to $FILE_PATH"
        rsync --rsync-path="sudo rsync" --progress -aiv $dir drone@$HOST_IP:$FILE_PATH

        ssh drone@$HOST_IP "sudo chown -R alexlebens:alexlebens $FILE_PATH/$dir"
        # ssh drone@$HOST_IP "sudo chmod -R 600 $FILE_PATH/$dir"
    done

    echo "Finished copying files"
    cd $WORKINGDIR
else
    echo "No files to copy"
fi


echo ">>> Compose"

if ! test -f "./docker-compose.yml"
then
    echo ">> ERROR: No docker-compose.yml in directory"
    continue
fi

echo ">> Deploy"

docker compose --project-name $(echo $PACKAGE | tr "[:upper:]" "[:lower:]") stop
sleep 10
docker compose --project-name $(echo $PACKAGE | tr "[:upper:]" "[:lower:]") up --wait
sleep 1