#!/bin/sh

NOSON_CLI=/usr/local/lib/noson/noson-cli
NOSON_GUI=/usr/local/lib/noson/noson-gui
PLUGINS_DIR=/usr/local/lib/noson

# Check for an instance of AppImage
if [ -n "$APPDIR" ] && [ -d "$APPDIR$PLUGINS_DIR" ]; then
  NOSON_CLI="$APPDIR$NOSON_CLI"
  NOSON_GUI="$APPDIR$NOSON_GUI"
  PLUGINS_DIR="$APPDIR$PLUGINS_DIR"
fi

# Launch the CLI else the GUI
[ "--cli" = "$1" ] && shift && exec "$NOSON_CLI" $@

# Migrate the GUI settings
if [ -f "$HOME/.config/janbar/noson.conf" ]; then
  mv "$HOME/.config/janbar/noson.conf" "$HOME/.config/janbar/io.github.janbar.noson.conf"
  [ -d "$HOME/.local/share/janbar/noson" ] && mv "$HOME/.local/share/janbar/noson" "$HOME/.local/share/janbar/io.github.janbar.noson"
  [ -d "$HOME/.cache/janbar/noson" ] && mv "$HOME/.cache/janbar/noson" "$HOME/.cache/janbar/io.github.janbar.noson"
fi

# Launch the GUI
export QML2_IMPORT_PATH="$PLUGINS_DIR"
exec "$NOSON_GUI" $@
