File appimage.AppRun of Package celestia

#!/bin/bash

HERE="$(dirname "$(readlink -f "${0}")")"

[[ -z $APPIMAGE ]] && export APPIMAGE="$HERE"
[[ -z $APPDIR ]] && export APPDIR="$HERE"
[[ -z $ARGV0 ]] && export ARGV0="$0"

_app="celestia"
_debug=""
#_debug="echo"
_general_home="$(dirname "$APPIMAGE")/${_app}-@MAIN_VERSION@.home"
_extract_dir="${APPIMAGE}.data"

# backups (used in appimage.xdg-open)
export _HOME_BAK="$HOME"
export _PATH_BAK="$PATH"
export _SESSION_MANAGER_BAK="$SESSION_MANAGER"
export _XDG_DATA_DIRS_BAK="$XDG_DATA_DIRS"
export _XDG_DATA_HOME_BAK="$XDG_DATA_HOME"

#
# taken from https://github.com/AppImage/AppImageKit/blob/master/src/AppRun.c
#
export GSETTINGS_SCHEMA_DIR="${HERE}/usr/share/glib-2.0/schemas:$GSETTINGS_SCHEMA_DIR"
export GST_PLUGIN_SYSTEM_PATH="${HERE}/usr/lib/gstreamer:$GST_PLUGIN_SYSTEM_PATH"
export GST_PLUGIN_SYSTEM_PATH_1_0="${HERE}/usr/lib/gstreamer-1.0:$GST_PLUGIN_SYSTEM_PATH_1_0"
export ICU_DATA="${HERE}/usr/share/icu"
TMP_LD_LIBRARY_PATH="%s/usr/lib:%s/usr/lib64:%s/lib:%s/lib64:%s/usr/lib64/pulseaudio"
export LD_LIBRARY_PATH="${TMP_LD_LIBRARY_PATH//\%s/$HERE}:$LD_LIBRARY_PATH"
TMP_PATH="%s/usr/bin:%s/usr/sbin:%s/usr/games:%s/bin:%s/sbin:%s/usr/lib64/libexec/kf5"
export PATH="${TMP_PATH//\%s/$HERE}:$PATH"
export PERLLIB="${HERE}/usr/share/perl5:${HERE}/usr/lib/perl5:$PERLLIB"
export PYTHONDONTWRITEBYTECODE=1
export PYTHONHOME="${HERE}/usr"
export PYTHONPATH="${HERE}/usr/share/pyshared:$PYTHONPATH"
TMP_QT_PLUGIN_PATH="%s/usr/lib64/qt5/plugins:%s/usr/lib/qt5/plugins"
TMP_QT_PLUGIN_PATH+=":%s/usr/lib64/qt6/plugins:%s/usr/lib/qt6/plugins"
export QT_PLUGIN_PATH="${TMP_QT_PLUGIN_PATH//\%s/$HERE}:$QT_PLUGIN_PATH"
export XDG_DATA_DIRS="${HERE}/usr/share:/usr/local/share:/usr/share:$XDG_DATA_DIRS"

#
# additional settings
#
# get rid of '(app:...): Gtk-WARNING **: ...: Unable to locate theme engine in module_path: "murrine",'
export GTK_PATH64="${HERE}/usr/lib64/gtk-2.0:$GTK_PATH64"
# get rid of 'Qt: Session management error: None of the authentication protocols specified are supported'
unset SESSION_MANAGER

r_err ()
{
    echo "error: $1"
    exit 1
}

app_extract_data ()
{
    local _ai_dir _cel_data
    _ai_dir="$(dirname "$APPIMAGE")"
    [[ -w $_ai_dir ]] || r_err "dir '$_ai_dir' is not writable."
    [[ -d $_extract_dir ]] && r_err "dir '$_extract_dir' already exists."
    _cel_data="${HERE}/usr/share/celestia"
    [[ -d $_cel_data ]] || r_err "dir '$_cel_data' does not exists."
    [[ -n $(ls -A "$_cel_data") ]] || r_err "dir '$_cel_data' is empty."
    $_debug mkdir -m 0755 "$_extract_dir" || r_err "cannot create dir '$_extract_dir'."
    $_debug cp -a "${_cel_data}"/* "$_extract_dir"/ || r_err "cannot copy embedded data."
    echo "embedded data extracted to '$_extract_dir'"
    echo "start Celestia e.g. with"
    printf "%q %q\n" "CELESTIA_DATA_DIR=$_extract_dir" "$APPIMAGE"
    exit 0
}

app_create_general_home ()
{
    local _up_dir
    [[ -d $_general_home ]] && {
        echo "general home dir '$_general_home' already exists."
        exit 0
    }
    _up_dir="$(dirname "$_general_home")"
    [[ -w $_up_dir ]] || { echo "parent dir '$_up_dir' is not writable." ; exit 1 ; }
    $_debug mkdir -p -m 0700 "$_general_home" || {
        echo "general home dir '$_general_home' could not be created."
        exit 1
    }
    echo "general home dir '$_general_home' created."
    exit 0
}

app_info ()
{
    if command -v less >/dev/null 2>&1 ; then
        less "${HERE}/.info"
    elif command -v more >/dev/null 2>&1 ; then
        more -d "${HERE}/.info"
    else
        cat "${HERE}/.info"
    fi
    exit 0
}

app_help ()
{
cat << EOF

Celestia @COMP_VERSION@ - Real-time visual space simulation

Usage: $ARGV0 [OPTION]...
       CELESTIA_DATA_DIR=<datadir> $ARGV0 [OPTION]...

Celestia options:
  --conf <conf>
    Set the configuraton file.
  --dir <datadir>
    Set the data directory.
    The same can also be achieved with
    CELESTIA_DATA_DIR=<datadir> $ARGV0
  --extrasdir <extrasdir>
    Add an extras directory. This option may be specified multiple times.
  --fullscreen
    Start in fullscreen mode.
  -l, --log <logpath>
    Set the path to the log file.
  -s, --nosplash
    Skip the splash screen.
  -u, --url <url>
    Set the start cel:// URL or startup script path.
  --help
    Displays help on commandline options.

AppImage options:
  --celestia-create-general-home
    Create a general home directory depending on the main version
    $_general_home
  --celestia-extract-data
    Extract the embedded data in the directory
    $_extract_dir
  --celestia-info
    Show information about the Celestia packages used to create this AppImage.
  -h, --celestia-help
    Show this help and exit.

EOF
    exit 0
}

_args=()
while [ $# -ne 0 ] ; do
    arg="$1"
    case $arg in
        --celestia-create-general-home)
            app_create_general_home
            shift
            ;;
        --celestia-extract-data)
            app_extract_data
            shift
            ;;
        --celestia-info)
            app_info
            shift
            ;;
        -h|--celestia-help)
            app_help
            shift
            ;;
        --dir)
            _dir="$2"
            [[ -n $_dir ]] || { echo "data dir missing." ;  exit 1 ; }
            [[ -d $_dir ]] || { echo "data dir '$_dir' not found." ; exit 1 ; }
            [[ -r $_dir ]] || { echo "data dir '$_dir' not readable." ; exit 1 ; }
            CELESTIA_DATA_DIR="$_dir"
            shift
            shift
            ;;
        *)
            # save it in an array for later use
            _args+=("$1")
            shift
            ;;
    esac
done
# restore positional parameters
set -- "${_args[@]}"

# export general home directory
[[ -d $_general_home && -w $_general_home ]] && {
    export HOME="$_general_home"
    export XDG_DATA_HOME="$HOME/.local/share"
}

[[ -z $CELESTIA_DATA_DIR ]] && export CELESTIA_DATA_DIR="$APPDIR/usr/share/celestia"
echo "Setting \$HOME to $HOME"
echo "Setting \$CELESTIA_DATA_DIR to $CELESTIA_DATA_DIR"

$_debug cd "${HERE}/usr"
$_debug exec "${HERE}/usr/bin/celestia" --dir "$CELESTIA_DATA_DIR" $CELESTIA_CONF "$@"
openSUSE Build Service is sponsored by