1#!/bin/sh
2# Copyright (C) 2016 O.S. Systems Software LTDA.
3# Copyright (C) 2016 Freescale Semiconductor
4
5export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
6
7usage() {
8	cat <<EOF
9	$0 [<openvt arguments>] [-- <weston options>]
10EOF
11}
12
13## Module support
14modules_dir=@DATADIR@/weston-start
15
16# Add weston extra argument
17add_weston_argument() {
18	weston_args="$weston_args $1"
19}
20
21# Add openvt extra argument
22add_openvt_argument() {
23	openvt_args="$openvt_args $1"
24}
25
26if [ -n "$WAYLAND_DISPLAY" ]; then
27	echo "ERROR: A Wayland compositor is already running, nested Weston instance is not supported yet."
28	exit 1
29fi
30
31if [ -n "$WESTON_USER" ]; then
32	if [ -z "$WESTON_TTY" ]; then
33		echo "ERROR: If you have WESTON_USER variable set, you also need WESTON_TTY."
34		exit 1
35	fi
36	if [ -z "$WESTON_GROUP" ]; then
37		# no explicit WESTON_GROUP given, therefore use WESTON_USER
38		export WESTON_GROUP="${WESTON_USER}"
39	fi
40	weston_args_user="-u $WESTON_USER -t $WESTON_TTY"
41fi
42
43if [ -n "$DISPLAY" ]; then
44	launcher="weston"
45else
46	launcher="weston-launch $weston_args_user --"
47fi
48
49openvt_args="-s"
50while [ -n "$1" ]; do
51	if [ "$1" = "--" ]; then
52		shift
53		break
54	fi
55	openvt_args="$openvt_args $1"
56	shift
57done
58
59weston_args=$*
60
61# Load and run modules
62if [ -d "$modules_dir" ]; then
63	for m in "$modules_dir"/*; do
64		# Skip backup files
65		if [ "`echo $m | sed -e 's/\~$//'`" != "$m" ]; then
66			continue
67		fi
68
69		# process module
70		. $m
71	done
72fi
73
74if test -z "$XDG_RUNTIME_DIR"; then
75	export XDG_RUNTIME_DIR=/run/user/`id -u ${WESTON_USER}`
76	if ! test -d "$XDG_RUNTIME_DIR"; then
77		mkdir --parents $XDG_RUNTIME_DIR
78		chmod 0700 $XDG_RUNTIME_DIR
79	fi
80	if [ -n "$WESTON_USER" ]
81	then
82		chown $WESTON_USER:$WESTON_GROUP $XDG_RUNTIME_DIR
83	fi
84fi
85
86exec openvt $openvt_args -- $launcher $weston_args --log=@LOCALSTATEDIR@/log/weston.log
87