#!/usr/bin/env bash

set -e

cat << 'EOF'
copyright © 2026 ronscf.
this file is part of Music Player 3.

Music Player 3 is free software: you can redistribute it and/or modify it under the terms of
the gnu general public license as published by the free software foundation,
either version 3 of the license, or (at your option) any later version.

Music Player 3 is distributed in the hope that it will be useful, but without any
warranty; without even the implied warranty of merchantability or
fitness for a particular purpose. see the gnu general public license for more details.

you should have received a copy of the gnu general public license along with
Music Player 3. if not, see <https://www.gnu.org/licenses/>.
--------------------------------------------------------------------------------

################################ MUSIC PLAYER 3 ################################
--------------------------------------------------------------------------------

Release: 2026-09-28 | v1.1.2 - add translation to 15 languages

update:
$ ./musicplayer3.sh --update

uninstall:
$ ./musicplayer3.sh --uninstall

EOF

bin_dir="$HOME/.local/bin"
app_dir="$HOME/.local/share/applications"
icon_dir="$HOME/.local/share/icons/hicolor/scalable/apps"
data_dir="$HOME/.local/share/musicplayer3"
schema_dir="$HOME/.local/share/glib-2.0/schemas"
cache_dir="$HOME/.cache/musicplayer3"
app_id="org.codeberg.ronscf.musicplayer3"

refresh_desktop() {
    command -v update-desktop-database &> /dev/null && update-desktop-database "$app_dir"
    command -v xdg-desktop-menu &> /dev/null && xdg-desktop-menu forceupdate --mode user
    command -v gtk-update-icon-cache &> /dev/null && gtk-update-icon-cache -f -t "$HOME/.local/share/icons/hicolor" &> /dev/null
    command -v glib-compile-schemas &> /dev/null && glib-compile-schemas "$schema_dir" &> /dev/null
    command -v kbuildsycoca6 &> /dev/null && kbuildsycoca6 &> /dev/null || \
        { command -v kbuildsycoca5 &> /dev/null && kbuildsycoca5 &> /dev/null; }
    touch "$HOME/.local/share/icons/hicolor"
}

uninstall() {
    echo "uninstalling Music Player 3..."
    rm -f "$bin_dir/musicplayer3" "$app_dir/$app_id.desktop" "$icon_dir/$app_id.svg" "$schema_dir/$app_id.gschema.xml"
    rm -rf "$cache_dir" "$data_dir"
    rm -f "$HOME/.local/share/locale"/*/LC_MESSAGES/musicplayer3.mo
    refresh_desktop
    echo "uninstallation of Music Player 3 complete."
    exit 0
}


build_and_install() {
    build_dir=$(mktemp -d)
    echo "downloading repository..."
    git clone --depth 1 https://codeberg.org/ronscf/musicplayer3.git "$build_dir"
    cd "$build_dir"
    
    echo "compiling and installing translations..."
    if [ -d "po" ]; then
        for po_file in po/*.po; do
            lang=$(basename "$po_file" .po)
            lang_dir="$HOME/.local/share/locale/$lang/LC_MESSAGES"
            mkdir -p "$lang_dir"
            msgfmt -o "$lang_dir/musicplayer3.mo" "$po_file"
        done
    fi
    
    make
    
    echo "installing binary and resources..."
    mkdir -p "$bin_dir" "$app_dir" "$icon_dir" "$schema_dir"
    
    cp build/musicplayer3 "$bin_dir/"
    [ -f "app/assets/img/icon.svg" ] && cp app/assets/img/icon.svg "$icon_dir/$app_id.svg"
    [ -f "data/$app_id.gschema.xml" ] && cp "data/$app_id.gschema.xml" "$schema_dir/"

    cat <<EOF > "$app_dir/$app_id.desktop"
[Desktop Entry]
Version=1.0.2
Name=Music Player 3
Comment=lightweight mp3 player
Exec=$bin_dir/musicplayer3
Icon=$app_id
Terminal=false
Type=Application
Categories=AudioVideo;Audio;Player;
EOF

    refresh_desktop
    cd "$HOME"
    rm -rf "$build_dir"
}

if [ "$1" = "--uninstall" ]; then
    uninstall
elif [ "$1" = "--update" ]; then
    echo "update of Music Player 3 v1.1.2 ..."
    build_and_install
    echo "update complete."
    exit 0
fi

if [ -f "$bin_dir/musicplayer3" ]; then
    echo "Music Player 3 is already installed."
    echo "run './musicplayer3.sh --update' to update to the latest version."
    exit 0
fi

echo "checking distribution for dependencies..."
if [ -f /etc/os-release ]; then
    . /etc/os-release
    distro_name="${NAME:-$ID}"
    distro_name_lc="${distro_name,,}"

    echo "=> detected distribution: $distro_name_lc"
    
    case "$ID" in
        void)
            echo "=> run the following command in another terminal to install dependencies:"
            echo "sudo xbps-install -S gcc make pkg-config git gtk+3-devel alsa-lib-devel mpg123-devel libid3tag-devel glib-devel gettext"
            echo "=> recommended audio bridge: sudo xbps-install alsa-pipewire"
            ;;
        debian|ubuntu|linuxmint|pop|elementary|sparky|kali|zorin)
            echo "=> run the following command in another terminal to install dependencies:"
            echo "sudo apt update && sudo apt install gcc make pkg-config git libgtk-3-dev libasound2-dev libmpg123-dev libid3tag0-dev libglib2.0-dev gettext"
            echo "=> recommended audio bridge: sudo apt install pipewire-alsa"
            ;;
        arch|manjaro|endeavouros|artix)
            echo "=> upgrade your distro to avoid problems: sudo pacman -Syu"
            echo "=> run the following command in another terminal to install dependencies:"
            echo "sudo pacman -S --needed gcc make pkgconf git gtk3 alsa-lib mpg123 libid3tag gettext"
            echo "=> recommended audio bridge: sudo pacman -S pipewire-alsa"
            ;;
        fedora|rhel|centos|rocky|almalinux)
            echo "=> run the following command in another terminal to install dependencies:"
            echo "sudo dnf install gcc make pkgconf-pkg-config git gtk3-devel alsa-lib-devel mpg123-devel libid3tag-devel glib2-devel gettext"
            echo "=> recommended audio bridge: sudo dnf install pipewire-alsa"
            ;;
        *)
            echo "distribution not automatically detected. make sure to install: git, make, gcc, pkg-config, gtk3, alsa-lib, libmpg123, libid3tag."
            ;;
    esac
else
    echo "could not determine the distribution."
fi

echo ""
read -p "press [enter] when dependencies are installed to start building (or ctrl+c to abort)..."

build_and_install

if [[ ":$PATH:" != *":$bin_dir:"* ]]; then
    echo "adding $bin_dir to PATH in ~/.bashrc..."
    echo "export PATH=\"\$HOME/.local/bin:\$PATH\"" >> "$HOME/.bashrc"
fi

echo "installation of Music Player 3 v1.1.2 finished."
echo "you should see the app icon in your desktop environment."
