#!/usr/bin/env bash
##makedesc: Install flameshot (latest)

set -eo pipefail

installed() { command -v "$1" >/dev/null 2>&1; }

install() {
    if [ "$(uname -s)" = "Darwin" ]; then
        if installed brew; then
            brew install --cask flameshot
        else
            echo "ERROR: Install Homebrew first." >&2
            exit 1
        fi
        return
    fi

    echo
    echo "==============================================="
    echo "Installing flameshot"
    echo "==============================================="
    echo

    # https://github.com/flameshot-org/flameshot?tab=readme-ov-file#compilation
    # sudo apt install -y --autoremove flameshot
    # sudo systemctl restart flameshot

    dir="$HOME/install"
    clone_dir="$dir/flameshot"
    mkdir -p "$dir"

    sudo apt install -y \
        g++ \
        cmake \
        build-essential \
        qtbase5-dev \
        qttools5-dev-tools \
        libqt5svg5-dev \
        qttools5-dev \
        libqt5dbus5 \
        libqt5network5 \
        libqt5core5a \
        libqt5widgets5 \
        libqt5gui5 \
        libqt5svg5

    if [ ! -d "$clone_dir" ]; then
        git clone --depth=1 --single-branch https://github.com/flameshot-org/flameshot.git "$clone_dir"
    fi

    # Directory where build files will be placed, may be relative
    export BUILD_DIR=build

    # Directory prefix where flameshot will be installed.
    export CMAKE_INSTALL_PREFIX=/opt/flameshot

    cd "$clone_dir"
    git pull
    cmake -S . -B "$BUILD_DIR"
    cmake --build "$BUILD_DIR"

    echo
    echo "Finish!"
    # /opt/flameshot/flameshot -v
    echo
}

case "$1" in
    *) install ;;
esac
