#!/usr/bin/env bash

HYPRSOLUS_PATH="/usr/src/hyprsolus"
[[ ! -d "$HYPRSOLUS_PATH" ]] && exit 1

# create cache folder for Hyprland metadata
cache="$HOME/.cache/hyprsolus"
[[ ! -d "$cache" ]] && mkdir -p "$cache" >/dev/null

check () {
    >&2 echo "Checking for Hyprland upgrades..."
    # ensure that Hyprland is even running
    if ! pidof hyprland
    then
        >&2 echo "Could not find Hyprland process. Is it even running?"
        exit 255
    fi
    # get the releases from the origin
    origin=$(curl -sL \
        https://api.github.com/repos/hyprwm/Hyprland/releases)
    exit_code=$?
    # check if the cURL was successful
    if [[ $exit_code -ne 0 ]]
    then
        >&2 echo "Failed to contact GitHub API. Check Internet connection and \
            try again."
        exit 254
    fi
    # latest tag release in origin and current Hyprland version
    origin_vers=$(echo "$origin" | jq -r ".[0].tag_name")
    hyprland_vers=$(hyprctl version | sed -nr 's/Tag: (.*),.*/\1/p')

    [[ ! -e "$cache/metadata.json" ]] && echo "$origin" \
        | jq -r ".[] | select(.tag_name == \"$hyprland_vers\")" \
        >"$cache/metadata.json"

    if [[ "$origin_vers" != "$hyprland_vers" ]]
    then
        echo "$origin" | jq -r ".[0]" >"$cache/release-metadata.json"
        notify-send -a "Hyprsolus" "Hyprland Update Available" \
            "A new version of Hyprland is available ($origin_vers)."
    else
        >&2 echo "No upgrade available. Ensuring most recent metadata..."
        [[ -f "$cache/release-metadata.json" ]] && \
            mv "$cache/release-metadata.json" "$cache/metadata.json"
    fi
}

info () {
    while test $# -ne 0
    do
        case $1 in
            --local) shift ; release=0 ;;
            --json) shift ; json=0 ;;
            --) shift ; glow_args="$*" ; break ;;
            *) echo "$1"
        esac
    done

    # Initialise defaults if no flags given
    release=${release:-1}
    json=${json:-1}

    if [[ -e "$cache/release-metadata.json" && $release -eq 1 ]]
    then
        [[ ! -s "$cache/release-metadata.json" ]] && exit 2
        [[ $json -eq 0 ]] && cat "$cache/release-metadata.json" && exit 0
        jq -r ".body" < "$cache/release-metadata.json" | glow "$glow_args" -
    elif [[ -e "$cache/metadata.json" ]]
    then
        [[ ! -s "$cache/metadata.json" ]] && exit 2
        [[ $json -eq 0 ]] && cat "$cache/metadata.json" && exit 0
        jq -r ".body" "$cache/metadata.json" | glow "$glow_args" -
    else
        exit 1
    fi
}

update () {
  cd "$HYPRSOLUS_PATH"

  # Fetch new content from remote
  git fetch origin --all
  git pull origin main

  echo "(hyprsolus) Updating additional scripts if needed."
  sudo cp -u "${HYPRSOLUS_PATH}/hyprsolusctl" "$HYPRSOLUSCTL_PATH"
  sudo cp -ru "${HYPRSOLUS_PATH}/systemd" "$HOME/.config/systemd/"
  systemctl --user daemon-reload

  echo "(hyprsolus) Updating the dependencies if needed."
  sudo sh "${HYPRSOLUS_PATH}/requirements.sh"

}

upgrade () {
  while test $# -ne 0
  do
    case $1 in
      --ensure-integrity) shift ; eopkg_check=0 ;;
    esac
  done

  eopkg_check=${eopkg_check:-1}
  if [[ $eopkg_check -eq 0 ]]
  then
    echo -n "Check package intergity..."
    while read -r line
    do
      check=$(eopkg check --no-color "$line" | grep -o "OK")
      if [[ $check != OK ]]
      then
        >&2 echo -e "failed.\n[FATAL] The following package is corrupt: $line."\
          "Aborting upgrade."
        exit 3
      fi
    done < <(cat depends.txt)
    echo "ok."
  fi

  # TODO: Implement calls to make build system
}

case $1 in
    check) check ;;
    update) update ;;
    upgrade) shift ; upgrade "$@" ;;
    info) shift ; info "$@" ;;
    *) exit 1 ;;
esac
exit 0
