#!/bin/bash

if [ "$1" = "--term" ]; then
    shift

    kitty_args=" "
    # shellcheck disable=SC2249
    case $1 in
      -*)
        while [ "$#" -gt 1 ] && [ "$1" != "--" ]; do
          kitty_args="${kitty_args}$1"
          shift
        done
        [ "$1" != "--" ] || shift
        ;;
    esac

    title="${1##*/}"
    title="${title:-kitty}"
    app="kitty ${kitty_args-*} --class ${title}"
else
    title="${1##*/}"
fi

# shellcheck disable=SC2312
if [ "$(uname)" = "Linux" ]; then
    exec systemd-run --quiet --user --scope --slice app.slice --unit "launch-${title}-$(date '+%s%N')" "${args-}" -- "${app}" "$@"
else
    exec ${app} "$@"
fi
