#!/usr/bin/env bash

function get_latest_release {
	curl --silent "https://api.github.com/repos/$1/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")'
}

function update_obsidian {
    local release=$(get_latest_release "obsidianmd/obsidian-releases")
    local filename="Obsidian-${release}.AppImage"
    local dest="${HOME}/.bin/appimages/${filename}"
    if [ ! -f "${dest}" ]; then
        rm "${HOME}/.bin/appimages/Obsidian-*.AppImage"
        wget -q -O "${dest}" "https://github.com/obsidianmd/obsidian-releases/releases/download/${release}/${filename}"
        chmod +x "${dest}"
        cd "${HOME}/.bin"
        ln -sf "${dest}" "${HOME}/.bin/obsidian"
    fi
}

function update_languagetool {
    cd "${HOME}/vm/docker/languagetool"
    docker compose pull
    docker compose down
    docker compose up -d
    cd -
}

function main {

    # Mise à jour système
    sudo apt -y update && \
    sudo apt -y upgrade 

    # Mise à jour service languagetool
    update_languagetool

    update_obsidian
    
    # Mise à jour de l'environnement de neovim
    # TODO

    # Mise à jour de zoom
    # TODO

    exit 0

}

main
