#!/bin/bash

cd "$(dirname "$0")"
export LD_LIBRARY_PATH=usr/lib

# Handles desktop integration:
# 1. Copies app icons to ~/.local/share/icons
# 2. Ensures icon theme index exists
# 3. Creates a .desktop file pointing to this AppImage
# 4. Skips if a system-wide installation (/usr/share/applications) is detected
install_integration() {
    if [[ -z "$APPIMAGE" ]]; then
        return
    fi

    REAL_HOME=$(getent passwd "$USER" | cut -d: -f6)
    
    if [ -z "$REAL_HOME" ]; then
        REAL_HOME="$HOME"
    fi

    APP_NAME="hiddify"
    TARGET_DESKTOP="$REAL_HOME/.local/share/applications/hiddify.desktop"
    LOCAL_HICOLOR="$REAL_HOME/.local/share/icons/hicolor"
    
    if [ ! -f "$LOCAL_HICOLOR/index.theme" ]; then
        mkdir -p "$LOCAL_HICOLOR"
        if [ -f "/usr/share/icons/hicolor/index.theme" ]; then
            cp "/usr/share/icons/hicolor/index.theme" "$LOCAL_HICOLOR/"
        else
            echo -e "[Icon Theme]\nName=Hicolor\nDirectories=128x128/apps,256x256/apps" > "$LOCAL_HICOLOR/index.theme"
        fi
    fi

    if [[ -f "./hiddify.png" ]]; then

        mkdir -p "$LOCAL_HICOLOR/128x128/apps"
        cp "./hiddify.png" "$LOCAL_HICOLOR/128x128/apps/hiddify.png"
        
        mkdir -p "$LOCAL_HICOLOR/256x256/apps"
        cp "./hiddify.png" "$LOCAL_HICOLOR/256x256/apps/hiddify.png"
        
        gtk-update-icon-cache -f -t "$LOCAL_HICOLOR" 2>/dev/null || true
    fi

    if [ -f "/usr/share/applications/hiddify.desktop" ]; then
        return
    fi

    mkdir -p "$(dirname "$TARGET_DESKTOP")"

    cat > "$TARGET_DESKTOP" <<EOF
[Desktop Entry]
Type=Application
Name=Hiddify
Exec="$APPIMAGE"
Icon=hiddify
Terminal=false
StartupWMClass=app.hiddify.com
EOF

    chmod +x "$TARGET_DESKTOP"
    update-desktop-database "$REAL_HOME/.local/share/applications" 2>/dev/null || true
}

# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} ...
start Hiddify or HiddifyCli, when no parameter is given, Hiddify is executed.
    -v              show version
EOF
}
show_version() {
    printf "Hiddify version "
    jq .version <./data/flutter_assets/version.json
}
# Initialize variables:
service=0 #declare -i service
OPTIND=1

# Resetting OPTIND is necessary if getopts was used previously in the script.
# It is a good idea to make OPTIND local if you process options in a function.

# if no arg is provided, execute hiddify app
if [[ $# == 0 ]];then 
    install_integration
    exec ./hiddify
else

# processing arguments

    case $1 in
        HiddifyCli)  
            exec ./HiddifyCli ${@:3}
            exit 0
            ;;
        h)
            show_help
            exit 0
            ;;
        v)  show_version
            exit 0
            ;;
        *)
            show_help >&2
            exit 1
            ;;
    esac



fi
