#!/usr/bin/env bash
##makedesc: Install lite-xl v2.1.7

set -eo pipefail

if [ "$(uname -s)" = "Darwin" ]; then
    echo "ERROR: lite-xl portable install is not supported on macOS. Use brew install --cask lite-xl or build from source." >&2
    exit 1
fi

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

install() {
    echo
    echo "==============================================="
    echo "Installing lite-xl v2.1.7"
    echo "==============================================="
    echo

    # https://github.com/lite-xl/lite-xl/releases
    # https://lite-xl.com/setup/getting-started/

    tar_url="https://github.com/lite-xl/lite-xl/releases/download/v2.1.7/lite-xl-v2.1.7-addons-linux-x86_64-portable.tar.gz"
    lpm_url="https://github.com/lite-xl/lite-xl-plugin-manager/releases/download/latest/lpm.x86_64-linux"
    tar_filepath="$HOME/install/lite-xl-v2.1.7.tar.gz"
    untar_dir="$HOME/install"

    rm -rf \
        "$HOME/.local/bin/lpm" \
        "$HOME/install/lite-xl" \
        "$HOME/.local/bin/lite-xl" \
        "$HOME/.local/share/lite-xl"

    mkdir -p \
        "$untar_dir" \
        "$HOME/.local/bin" \
        "$HOME/.local/share/lite-xl"

    if installed wget; then
        wget -q --show-progress "$tar_url" -O "$tar_filepath"
        wget -q --show-progress "$lpm_url" -O "$HOME/.local/bin/lpm"
    else
        curl -fsSL "$tar_url" -o "$tar_filepath"
        curl -fsSL "$lpm_url" -o "$HOME/.local/bin/lpm"
    fi

    chmod +x "$HOME/.local/bin/lpm"
    tar -xzf "$tar_filepath" -C "$untar_dir"
    cp -f "$untar_dir/lite-xl/lite-xl" "$HOME/.local/bin/"
    cp -fr "$untar_dir"/lite-xl/data/* "$HOME/.local/share/lite-xl/"

    cat << EOF > "$HOME/.local/share/applications/org.lite_xl.lite_xl.desktop"
[Desktop Entry]
Type=Application
Name=Lite XL
Comment=A lightweight text editor written in Lua
Exec=$HOME/.local/bin/lite-xl %F
Icon=lite-xl
Terminal=false
StartupWMClass=lite-xl
Categories=Development;IDE;
MimeType=text/plain;
EOF

    if command -v update-desktop-database >/dev/null 2>&1; then
        sudo update-desktop-database
    fi
    if command -v xdg-desktop-menu >/dev/null 2>&1; then
        xdg-desktop-menu forceupdate
    fi

    echo
    echo "Finish!"
    echo
}

remove() {
    if [ "$(uname -s)" = "Darwin" ]; then
        if installed brew; then
            brew uninstall --cask lite-xl 2>/dev/null || true
        fi
        return
    fi

    echo
    echo "==============================================="
    echo "Removing lite-xl"
    echo "==============================================="
    echo

    rm -rf \
        "$HOME/.local/bin/lite-xl" \
        "$HOME/.local/bin/lpm" \
        "$HOME/.local/share/applications/org.lite_xl.lite_xl.desktop" \
        "$HOME/.local/share/lite-xl" \
        "$HOME/.config/lite-xl"

    if command -v update-desktop-database >/dev/null 2>&1; then
        sudo update-desktop-database
    fi
    if command -v xdg-desktop-menu >/dev/null 2>&1; then
        xdg-desktop-menu forceupdate
    fi

    echo
    echo "Finish!"
    echo
}

case "$1" in
    r|remove|d|delete|p|purge) remove ;;
    *) install ;;
esac
