#!/bin/bash

hotfixerVersion=$(</hotfixerversion.txt)
IFS='.' read -ra ADDR <<< "$hotfixerVersion"
majorHotfixer=${ADDR[@]:0:1}
minorHotfixer=${ADDR[@]:1:1}
patchHotfixer=${ADDR[@]:2:1}

hotfixCompatibility=$(jq '.compatibility_version' /$1.conf  | tr -d '"')
numVersion=$(echo $hotfixCompatibility)

if [[ "$numVersion" == "null" ]]
then
    echo "Current version $majorHotfixer.$minorHotfixer.$patchHotfixer, required version not difined in file config!"
    echo "invalid $1" >> /validVersion.txt

else
    IFS='.' read -ra ADDR <<< "$hotfixCompatibility"
    major=${ADDR[@]:0:1}
    minor=${ADDR[@]:1:1}
    patch=${ADDR[@]:2:1}

    if [[ $majorHotfixer -gt $major ||  $majorHotfixer -eq $major ]]; then
            if [[ $majorHotfixer -gt $major ]]; then
                    echo "valid $1" >> /validVersion.txt
            else
                    if [[ $minorHotfixer -gt $minor || $minorHotfixer -eq $minor ]]; then
                            if  [[  $minorHotfixer -gt $minor ]]; then
                                    echo "valid $1" >> /validVersion.txt
                            else
                                    if [[  $patchHotfixer -gt $patch || $patchHotfixer -eq $patch  ]]; then
                                            echo "valid $1" >> /validVersion.txt
                                    else
                                            echo "Current version $majorHotfixer.$minorHotfixer.$patchHotfixer, required version $major.$minor.$patch"
                                            echo "invalid $1" >> /validVersion.txt
                                    fi
                            fi
                    else
                            echo "Current version $majorHotfixer.$minorHotfixer.$patchHotfixer, required version $major.$minor.$patch"
                            echo "invalid $1" >> /validVersion.txt
                    fi
            fi
    else
      echo "Current version $majorHotfixer.$minorHotfixer.$patchHotfixer, required version $major.$minor.$patch"
      echo "invalid $1" >> /validVersion.txt
    fi
fi
