#!/bin/sh
#
# AbiWord 2.8.6 launcher for Kindle Scribe -- STRICTLY BUNDLE-LOCAL.

export LC_ALL="en_US.UTF-8"

APPDIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)"
export APPDIR

LOG="${APPDIR}/run.log"
log() { echo "[abiword-launch] $*"; }

HIDE_STATUS_BAR="no"
STATUS_HIDDEN="no"

cleanup() {
    if [ "${STATUS_HIDDEN}" = "yes" ]; then
        lipc-set-prop com.lab126.pillow disableEnablePillow enable 2>/dev/null
    fi
}
trap cleanup EXIT INT TERM HUP

exec_ok() {
    mp="$(awk -v p="${APPDIR}" '$2==substr(p,1,length($2)) && length($2)>maxlen {maxlen=length($2); m=$2; o=$4} END{print m" "o}' /proc/mounts)"
    point="${mp% *}"; opts="${mp#* }"
    case ",${opts}," in
        *,noexec,*)
            log "NOTE: the bundle filesystem (${point}) is mounted noexec."
            return 1 ;;
        *) log "exec check: ${point} ok (${opts})"; return 0 ;;
    esac
}

setup_env() {
    export LD_LIBRARY_PATH="${APPDIR}/lib:${LD_LIBRARY_PATH}"
    export ABIWORD_DATADIR="${APPDIR}/share/abiword-2.8"
    export ABIWORD_PLUGINSDIR="${APPDIR}/plugins"

    export GTK_PATH="${APPDIR}/lib/gtk-2.0"
    export GTK_DATA_PREFIX="${APPDIR}"
    export GTK_EXE_PREFIX="${APPDIR}"
    export GDK_PIXBUF_MODULEDIR="${APPDIR}/lib/gtk-2.0/2.10.0/loaders"
    export GDK_PIXBUF_MODULE_FILE="${APPDIR}/lib/gtk-2.0/2.10.0/loaders.cache"
    export GTK_IM_MODULE_FILE="${APPDIR}/lib/gtk-2.0/2.10.0/immodules.cache"
    export PANGO_RC_FILE="${APPDIR}/etc/pango/pangorc"
    export FONTCONFIG_PATH="${APPDIR}/etc/fonts"
    export FONTCONFIG_FILE="${APPDIR}/etc/fonts/fonts.conf"
    export XDG_DATA_DIRS="${APPDIR}/share:/usr/share"

    export XAUTHORITY="${XAUTHORITY:-${HOME}/.Xauthority}"
    export HOME="${APPDIR}"

    # --- GTK Theme, E-Ink Flicker, & High-DPI Scaling Fixes ---
    export GTK2_RC_FILES="${APPDIR}/.gtkrc-2.0"
    echo "gtk-icon-theme-name = \"gnome\"" > "${GTK2_RC_FILES}"
    echo "gtk-fallback-icon-theme = \"gnome\"" >> "${GTK2_RC_FILES}"
    echo "gtk-cursor-blink = 0" >> "${GTK2_RC_FILES}"
    echo "gtk-icon-sizes = \"gtk-menu=32,32:gtk-small-toolbar=48,48:gtk-large-toolbar=48,48:gtk-button=48,48:gtk-dnd=64,64:gtk-dialog=64,64\"" >> "${GTK2_RC_FILES}"
    # ----------------------------------------------------------

    export XDG_CACHE_HOME="${APPDIR}/.cache"
    export XDG_CONFIG_HOME="${APPDIR}/.config"
    mkdir -p "${XDG_CACHE_HOME}/fontconfig" "${XDG_CONFIG_HOME}"

    export DISPLAY=":0"
}

{
    log "=== AbiWord launch $(date 2>/dev/null) ==="
    log "APPDIR=${APPDIR}"

    exec_ok || exit 1

    if [ ! -x "${APPDIR}/abiword" ]; then
        log "FATAL: ${APPDIR}/abiword is missing or not executable."
        exit 1
    fi

    setup_env

    if [ "${HIDE_STATUS_BAR}" = "yes" ]; then
        if lipc-set-prop com.lab126.pillow disableEnablePillow disable 2>/dev/null; then
            STATUS_HIDDEN="yes"
            usleep 300000
        fi
    fi

    log "launching ./abiword on DISPLAY=${DISPLAY}"
    cd "${APPDIR}" || exit 1
    ./abiword "$@"
    RC=$?
    log "abiword exited rc=${RC}"
    exit "${RC}"
} 2>&1 | tee "${LOG}"