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


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

echo ">>> Apply"

echo ">> Storage"

PATH_FAILURE=true
PATHS=$(yq '.volumes[].driver_opts.device | select(. > "/var*")' docker-compose.yml)

for dir in $PATHS; do
    if ! ssh -q drone@$HOST_IP "sudo test -d $dir"; then
        echo "Missing path: $dir"
        ssh -q drone@$HOST_IP "sudo test -d $dir"

        ssh drone@$HOST_IP "sudo mkdir -p $dir"
        ssh drone@$HOST_IP "sudo chown alexlebens:alexlebens $dir"
        
        if ! ssh -q drone@$HOST_IP "sudo test -d $dir"; then
            echo "Failed to create path: $dir"
            ssh -q drone@$HOST_IP "sudo test -d $dir"
        else
            echo "Path created: $dir"
            PATH_FAILURE=false
        fi
    else
        echo "Path found: $dir"
        PATH_FAILURE=false
    fi
done


echo ">>> Checks"

echo ">> DNS"

URL_CHECK=true
URL_FAILURE=false

if [ -n "$URL" ]; then 
    for i in $URL; do
        if [ "$(dig +short -t a $i)" ]; then
            echo "URL found: $i"
            URL_CHECK=false
        else
            echo "URL not found: $i"
            URL_FAILURE=true
        fi
    done
else
    echo "No URL set"
    URL_CHECK=false
fi


echo ">> Uptime"

cd UptimeKuma

UPTIME_CHECK=true
UPTIME_FAILURE=false

TOKEN=$(curl -X 'POST' "${UPTIMEKUMA_URL}/login/access-token" -H 'accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -d "grant_type=&username=${UPTIMEKUMA_NAME}&password=${UPTIMEKUMA_PASSWORD}&scope=&client_id=&client_secret=" | jq -r ".access_token")
MONITORS=$(curl -X 'GET' "${UPTIMEKUMA_URL}/monitors" -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" -s | jq --arg PACKAGE $PACKAGE '[ .monitors[] | select( .name | contains($PACKAGE))]')

echo "Found the following monitors:"
echo "$MONITORS" | jq -r '.[].name'

for file in *; do
    IFS_HOLD=$IFS
    IFS=$'\n'

    for type in $(echo "$MONITORS" | jq -r '.[].name'); do
        MONITOR_NAME=$(cat $file | jq -r '.name')

        echo "Comparing '$MONITOR_NAME' and '$type'"
        if [ "$MONITOR_NAME" == $type ]; then
            echo "Found monitor for $file"
            UPTIME_CHECK=false
            continue 2
        fi
    done

    IFS=$IFS_HOLD

    echo "Missing monitor for $file"
    RESPONSE=$(curl -X POST "${UPTIMEKUMA_URL}/monitors" -H 'accept: application/json' -H "Authorization: Bearer ${TOKEN}" -H 'Content-Type: application/json' -d "$(cat $file)" -s)
    RESPONSE_MESSAGE=$(echo $RESPONSE | jq -r '.msg')

    if [ "$RESPONSE_MESSAGE" == "Added Successfully." ]; then
        echo "Created monitor for $file"
        UPTIME_CHECK=false
    else
        echo "Failed creating monitor for $file: $RESPONSE_MESSAGE"
        echo "$RESPONSE"
        UPTIME_FAILURE=true
    fi
done
cd $WORKINGDIR


echo ">>> Exit"

if $URL_CHECK || $URL_FAILURE || $UPTIME_CHECK || $UPTIME_FAILURE; then
    echo "Exiting with Errors"
    echo "URL_CHECK: $URL_CHECK || URL_FAILURE: $URL_FAILURE || UPTIME_CHECK: $UPTIME_CHECK || UPTIME_FAILURE: $UPTIME_FAILURE"
fi