#!/bin/bash
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2020-present Fewtarius

. /etc/profile

ui_state() {
  clear >/dev/console 2>&1
  systemctl ${1} ${UI_SERVICE}
}

if [ "${UI_SERVICE}" = "weston.service" ]
then
  if [ -f "${*}" ]
  then
    RUN=$(echo ${*} | sed 's# #\\ #g')
    weston-terminal --command="${RUN}"
  else
    weston-terminal --command="${*}"
  fi
else
  ui_state stop
  clear >/dev/console 2>&1
  $* >/dev/console 2>&1
  if [ "$?" == 0 ]
  then
    ui_state start
    exit 0
  else
    "$*" >/dev/console 2>&1
    ui_state start
    exit 0
  fi
fi