#!/bin/sh
# Portable GradeCapital launcher. It locates the instance from the launcher
# file itself, so the current working directory and the instance location do
# not matter. Moving the whole directory keeps the launcher working.
set -u

self=$0
case $self in
    /*) ;;
    */*) self=$(CDPATH= cd -- "$(dirname -- "$self")" && pwd -P)/$(basename -- "$self") ;;
    *) self=$(command -v -- "$self" 2>/dev/null || printf '%s' "$self") ;;
esac

# Resolve symbolic links, including a link to a launcher in another directory.
while [ -L "$self" ]; do
    directory=$(CDPATH= cd -- "$(dirname -- "$self")" && pwd -P) || exit 1
    target=$(readlink -- "$self") || exit 1
    case $target in
        /*) self=$target ;;
        *) self=$directory/$target ;;
    esac
done

root=$(CDPATH= cd -- "$(dirname -- "$self")" && pwd -P) || exit 1
app=$root/app/GradeCapital.Desktop

if [ ! -x "$app" ]; then
    message="GradeCapital: app/GradeCapital.Desktop was not found next to this launcher."
    printf '%s\n' "$message" >&2
    if command -v xmessage >/dev/null 2>&1; then
        xmessage -center "$message" >/dev/null 2>&1 &
    fi
    exit 1
fi

exec "$app" "$@"
