#!/bin/bash

# This is to avoid $ARGV0 issues when used with zsh
# Reference: https://github.com/neovim/neovim/blob/master/scripts/genappimage.sh
# Reference: https://github.com/neovim/neovim/issues/9341
unset ARGV0

# Load bundled libraries.  Avoid adding trailing `:` to $LD_LIBRARY_PATH when
# its current value is empty, at least in one case it causes massive delays for
# sshfuse.
# Reference: https://q2a.vifm.info/2199/misc-feedback
if [ -z "$LD_LIBRARY_PATH" ]; then
    export LD_LIBRARY_PATH="$APPDIR/usr/lib"
else
    export LD_LIBRARY_PATH="$APPDIR/usr/lib:$LD_LIBRARY_PATH"
fi

# Tell Vifm it's used from an AppImage and what its root directory is.
export VIFM_APPDIR_ROOT=$APPDIR

# Make ncurses look into the bundled terminfo.  Using $TERMINFO_DIRS is better
# than $TERMINFO as it allows preserving any already specified paths.  See the
# "ENVIRONMENT" section of `man ncurses` for more details including priority of
# the lookup.
if [ -z "$TERMINFO_DIRS" ]; then
    export TERMINFO_DIRS="$APPDIR/usr/share/terminfo"
else
    export TERMINFO_DIRS="$TERMINFO_DIRS:$APPDIR/usr/share/terminfo"
fi

# Expose bundled vifm-pause, other helpers and the bundled binary.
PATH="$APPDIR/usr/bin:$PATH"

exec "$APPDIR/usr/bin/vifm" ${@+"$@"}
